-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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 Rule & allowEmpty #8925
Comments
The second param of allowEmpty() can be a callable. You can put your custom logic for deciding whether a field can be empty in the callable and return a boolean. |
This only works for the first time not when a Form invalidates and re-renders the view. |
Please provide example codes and steps to reproduce the issue. |
The main problem is that the rules are used both for presenting the form and for validating the posted data (before vs after). I bet @wilsmex can provide a step to step example, otherwise I will provide one of the tests I ran with "depending fields" and leaving one of them blank. |
Yeah, I have also been affected by this but a change on the behavior would likely cause a lot of pain. Maybe a flag for hinting the validator that it should run the validation logic regardless of the field being present or empty? |
That was also my initial proposal when you and me talked about it, some boolean flag to do that until we can find a more clean approach in the next major or sth.
I don't think there a use case to allow validation when it is not present at all (you can easily enforce that yourself by merging with default values, e.g. empty strings). |
Would it makes sense to consider deprecating the concept of 'empty', and instead make allowEmpty() a helper method that attaches validation rules? Then we could avoid this state where rules don't run when values are empty, and empty checking becomes just another validation rule. |
See the following code: $validator
->allowEmpty('value')
->integer('value'); In this case, users would expect that Whatever is the implementation to solve this issue, we would need to introduce a new option or new method to specify how |
@chinpei215 Good point. If $validator
->integer('value')
->allowEmpty('value'); |
Reading source code, the Aside from that, how does So the |
I would still rather consider this a bug in the current 3.x implementation as this use case, even though rare, cannot be done with the current tooling, and despite the initial false positive on first post will then often times surprisingly fail on the 2nd post (when "empty" data has been provided via post). |
What about using $validator->add('fieldName', 'ruleName', [
'rule' => 'custom',
'on' => 'empty', // Same as 'on' => ['create', 'update', 'empty']
]); |
@chinpei215 I didnt play it through, but it could work. If it accounts for the above use cases, sure. |
Sorry. My approach might not work 😞 |
It's been a while since this has been talked about. I'm just wondering if any progress on this has been made as of yet? |
I finally understood the reason why 2.x validation has the Anyway, this is a kind of regression in 3.x. So I think we need to discuss how to fix this defect. Do you have any ideas? |
@chinpei215 Earlier in the thread I had proposed that |
Thank you for your replay, but I am not sure how it works. |
Currently allowEmpty() is a 'state' on a field. When set to true, empty values are allowed and no validators are run. Instead we could move to a model where I wouldn't overload the current |
Thank you for explanation. How does it work when |
Lets assume our new behavior is offered by a new method called $validator
->acceptEmpty('user_id', true)
->integer('user_id); In this scenario $validator
->acceptEmpty('user_id', false)
->custom('user_id', 'myRule'); The rule generated by |
Thank you. What should people do when they want to judge whether they allow empty or not? Probably, for now, custom rules are not called if field is left empty. |
I'm having a particular situation, and I think that's related to this. I've an entity that has two fields "linked", the user may complete only one, but one of them must be filled mandatory. // this was my first attempt
$validator->allowEmptyString('field_a', function ($context) {
return !empty($context['data']['field_b']);
});
// ... and a similar validation rule on field_b checking for field_a While this works for validating the entity before saving, it does not work when the form is built for a new entity. Both fields are empty, so both rules determines that the other field can't be empty, as a result, both inputs on the form has the required attribute. My second attempt was allow empty on both fields and add a custom validation rule to do the check (that one of them is filled), but then is when I find out that if both are empty, validation rules on them aren't applied at all. Which has some sense, since I'm telling the framework that those field are allowed to be empty, but, in the other hand, a validation may be checking also the context data, so there should be a way of telling validator to apply some rules no matter the field is allowed to be empty. Right now I've solved the issue by applying the validations rules of my first attempt and adding Do we have some way of detecting when validatior is used by the entity context to provide info to the form helper, and when is used to actually validate data? (I'm using CakePHP 3.7.4) |
No there doesn't exist a way to do this. You could not use allowEmptyString and handle the empty checks in your validation rules or application rules instead. |
Yep, after posting my comment I've checked out other issues about validation and I saw this #12484 (comment) |
This issue is stale because it has been open for 120 days with no activity. Remove the |
This is a (multiple allowed):
What you did
I need to have my custom validation rules determine whether or not a field should be required or not. This is currently impossible as:
allowEmpty
the custom validation rule will not fire if the field is left emptyExpected Behavior
When I create a custom validation rule, it should run it's logic and return true/false whether or not the field is left empty or not.
Actual Behavior
When a custom validation rule is in place, a field cannot be left blank and forces data before the custom validation rules execute.
See http://stackoverflow.com/questions/37176984/cakephp-3-x-custom-validation-field-required
The text was updated successfully, but these errors were encountered: