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 rules and Closures are not called if value is empty #25067

Closed
alfonsobries opened this issue Aug 2, 2018 · 3 comments
Closed

Custom rules and Closures are not called if value is empty #25067

alfonsobries opened this issue Aug 2, 2018 · 3 comments

Comments

@alfonsobries
Copy link
Contributor

alfonsobries commented Aug 2, 2018

  • Laravel Version: 5.7
  • PHP Version: 7.2
  • Database Driver & Version: mysql

Description:

I'm trying to create a custom validation rule or at least use a closure custom validation but I cannot achieve that the function was called when the request value is empty.

Steps To Reproduce:

  1. Create a closure validation rule
  2. Send an empty value for the attribute
  3. The function is ignored

Example:

<?php
$this->validate($request->all(), [
  'users.*.email' => function($attribute, $value, $fail) {
      dd("This is not called when the $value (user[0].email = '') is empty");
      // My custom condition 
      // for example only require the email if the user is admin.
    }
]);

*Notice that in my real case im validating that the value is in another array in the same request, i mean it will be required based on the value of every item, so some rows will be required and some others not, otherwise i will make a condition like:
'users.*.email' => (/*condition*) ? 'required' : ''

@alfonsobries
Copy link
Contributor Author

alfonsobries commented Aug 2, 2018

Btw i create this PR that will be useful to solve this problem:
#25066

@alfonsobries
Copy link
Contributor Author

alfonsobries commented Aug 2, 2018

I find that i need to use the extendImplicit function this solves my problem

Validator::extendImplicit('foo', function ($attribute, $value, $parameters, $validator) {
    return $value == 'foo';
});

Its possible to achieve using validation closures?

@surgiie
Copy link

surgiie commented Oct 31, 2018

@alfonsobries same issue here. Wondering why it doesnt call when the request input is empty.

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

2 participants