Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Validation Rules don't work when used with multiple built-in rules #23750

Closed
apasov opened this issue Mar 30, 2018 · 2 comments
Closed

Comments

@apasov
Copy link

apasov commented Mar 30, 2018

  • Laravel Version: 5.6.14
  • PHP Version: 7.2.3
  • Database Driver & Version: MariaDB 10.2.14

Description:

It only works when combined with single built-in rule. Trying to use chained rules throws an error.

Steps To Reproduce:

This works:

$request->validate([
    'name' => 'string|max:255',
]);

And this works as well:

use App\Rules\CustomValidationRule;

$request->validate([
    'name' => ['string', new CustomValidationRule],
]);

But this fails:

use App\Rules\CustomValidationRule;

$request->validate([
    'name' => ['string|max:255', new CustomValidationRule],
]);

and throws an error:

message: Method Illuminate\Validation\Validator::validateString|max does not exist.
exception: BadMethodCallException
file: vendor/laravel/framework/src/Illuminate/Validation/Validator.php
line: 1146

@ghost
Copy link

ghost commented Mar 30, 2018

You should use the built-in rules as separate strings:

'name' => ['string','max:255', new CustomValidationRule],

@apasov
Copy link
Author

apasov commented Mar 30, 2018

Thanks @GeraldPK
I think this should be mentioned in the docs so I proposed a PR.

taylorotwell added a commit to laravel/docs that referenced this issue Mar 30, 2018
findstar added a commit to laravelkr/docs that referenced this issue Apr 5, 2018
* '5.6' of https://github.com/laravel/docs: (40 commits)
  formatting
  Update authorization.md
  Update authorization.md
  Add a missing word
  formatting
  Fix response status code on invalid signature
  add docs for `waitForRoute`
  more fixes
  clarify
  Link the methods in Higher Order Messages
  each method does not create a new collection
  formatting
  Update collections.md
  random doesn't always return Collection
  tweak formatting
  Update validation.md
  Fixes laravel/framework#23750
  Fixed typo
  add explanation of special environment variables
  add bail to the list of available validation rules
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant