-
Notifications
You must be signed in to change notification settings - Fork 21
Option to validate the error always on non-required fields from custom validation #19
base: master
Are you sure you want to change the base?
Conversation
So what is the use case for this? |
The use case might be if you are validating a field that at certain condition might be either required or not. For example, in our case we have a separate start_month and start_year field. We had a custom validation on the start_year field that if both fields are supplied, we have to validate it that the fields should be valid (either 1-12 for months and 4 digits for years) and some other business validations. This field should be validated if the value is supplied (only if supplied) - this is not a required field. Just noticed, there was an issue raised regarding this. |
It would only say the form was valid if it the field doesn't have a value. I'm trying to wrap my head around whether this is an actual issue that can't be solved with using the second param Maybe your test is just a bad example? I could change your test case by removing |
I think we need a more real-world example that shows why the new feature is required and why you can't just impelement this with |
Hi, apologies for not so clear in my previous example. We have start_month and start_year fields that would be required if and only if the card_type has a value of 5. We have a custom validation on the start_year field to validate these scenarios.
So if the user passes a value of 5 on the card_type but didn't put any value on the start_year (where our custom validation resides), the custom validation catches this scenario and throws an Error. For the current implementation, the form is still valid since the Error was not propagated because start_year is empty and was not flagged as required. Another one is that if the user passes a value on start_month but didn't pass a value on start_year, the custom validation catches this scenario and throws an Error (invalid start date). But since start_year is empty and was not flagged as required, the form is still valid. In the test, it should pass if you make the field as required – this is the current behavior, but in this scenario, the field is only required if certain fields has certain values. I've updated the test to make it a little bit clearer. Please let me know if this is not clear enough or if you have some suggestions/comments about this. Thanks a lot. :) |
+1 for adding this feature |
+1 for adding this feature Useful when you want to use custom validation with dependency fields. |
Added a new property to the fields object so that you could always validate the error thrown from the custom validation by non-required fields.
Added a test also to validate it:
This is based on @dr-nick's fork implementation ....