This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hi guys,
In my project I've to check a custom rule on a model before save. I know I can use Validation Trait in my model but I want to use a custom validator in the form controller.
How should I do that and where can I put it ?
Hi,
What exactly you try to make? You can add validation rules in your model easy. Or do you need to write a custom validator because there is none which helps you? Surely you can also make a validation inside your controller method like this:
$rules = [];
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('yourroute')->withErrors($validator);
}
Regards Crazymodder
Thank you.
Yes, I need to write a custom rule. It's exactly that you wrote, but where put this code ? Which method in the controller ?
For now, I write it in formBeforeCreate
but the model I get in parameters is empty so I can't check anything...
If you need a custom rule thats the wrong code. You have to extend the validator class. I think its good to make a seperate class for that like this:
<?php
class CustomValidator extends Illuminate\Validation\Validator
{
public function validateFoo($attribute, $value, $parameters)
{
return $value == 'foo';
}
}
Than you need to register this extension like this:
Validator::resolver(function($translator, $data, $rules, $messages, $customAttributes) {
return new CustomValidator($translator, $data, $rules, $messages, $customAttributes);
});
Last updated
1-4 of 4