0

I have a CustomFormRequest in which I want to use a custom rule. This is the rules() method in the FormRequest.

public function rules()
{
    return [
            'name' => 'customrule'
    ];
}

The Validorclass

class CustomValidator extends Illuminate\Validation\Validator{
protected function customrule( $attribute, $value ) {
    return false;
}

I have a CustomServiceProvider with the following boot() method

public function boot()
{
   \Validator::resolver(function($translator, $data, $rules, $messages)
    {
         return new CustomValidator($translator, $data, $rules, $messages);
    });
}

The CustomServiceProvider is listed in the app.php file.

Controller hat is using the formrequest

 public function store(CustomFormRequest $request)
{
    $input = $request->all();
    dd("request succeeded");

This rule is not recognized by my FormRequest (or at least not executed), since the request always succeeds. How can I fix this?

JorenV
  • 352
  • 3
  • 16
  • You could make your own bind to register all new custom rules. I have described all the steps here: http://stackoverflow.com/questions/28417977/custom-validator-in-laravel-5/28425173#28425173 – manix Mar 24 '15 at 16:50
  • I changed my code to implement your solution, but it doesn't seem to work :/ – JorenV Mar 24 '15 at 17:07
  • If it does not work you need to show us the error – manix Mar 24 '15 at 17:12
  • I return a hardcoded false in the function, but code in the controller that uses the FormRequest is still executing. This means that the function is not called right? – JorenV Mar 24 '15 at 17:13
  • Show us the controller part that where you call the validation class – manix Mar 24 '15 at 17:18
  • I don't call validation anywhere, I am using the FormRequests. – JorenV Mar 24 '15 at 17:23
  • Well, you need to call your custom form request like this: `public function store(CustomFormRequest $request)` as parameter – manix Mar 24 '15 at 17:31
  • I do that. Sorry if The naming is sometimes unclear – JorenV Mar 24 '15 at 17:37
  • This seems that you are missing something small. Because the code should work – manix Mar 24 '15 at 17:53

1 Answers1

1

Do you intend to make many custom rules? If not (I don't believe this scales well), here is what I use. Not that beautiful of a solution, but it's short and works:

Inside your request file add this:

use Illuminate\Validation\Factory;

class YourRequest extends Request {
...

  public function __construct(Factory $factory)
  {
      $factory->extendImplicit('customrule', function ($attribute, $value, $parameters) {
          //$value is what the user typed in the form or what came from POST
          // do some logic here, if the input is correct, return true else return false e.g.:

          if($value == 'what_is_expected')
             return true;
          else
             return false.

      },
          'Custom rule failed error message!'
      );
  }
}
hfingler
  • 1,931
  • 4
  • 29
  • 36
  • This does the trick. 'someBooleanFunction' usus the function created in the CustomValidator (is this normal behaviour?). It seems a bit weird that is uses that function. – JorenV Mar 24 '15 at 21:28
  • JorenV I didn't get what you said, what do you mean CustomValidator function? Is someBooleanFunction a real function? O.o – hfingler Mar 24 '15 at 21:57
  • apparantly not, it seems I don't understand customvalidation enough.I can comment out the 'return booleanfunction', and it still works. – JorenV Mar 24 '15 at 22:07
  • JorenV It doesn't exist. What do you intend to validate? Put your validation logic in there instead of the function. The `$value` parameter is what the user typed, so check if it is how it is supposed to be, if it is, return true, if not return false. I'll edit the answer. – hfingler Mar 24 '15 at 22:09
  • I mean that, if I write this: $factory->extendImplicit('customrule', function ($attribute, $value, $parameters) {}, It uses the customrule function of my CustomValidator – JorenV Mar 24 '15 at 22:11