-
Notifications
You must be signed in to change notification settings - Fork 27.5k
ng-required validation not working properly with true/false radio values. #2594
Comments
I have the same problem here. Also the model value of my second radio button (ng-value="false") is undefined instead of false, if I use ng-required. <input type="radio" ng-required="true" ng-model="test" ng-value="true" />
<input type="radio" ng-required="true" ng-model="test" ng-value="false" /> |
@kpko I ended up writing my own directive to work around it. Feel free to use it! https://github.com/michaelmoussa/ng-boolean-radio |
Yes, this is a corner-case. The issue is due to this check: angular.js/src/ng/directive/input.js Line 1226 in e1fe2ac
that was introduced to cater for a check-box use-case: angular.js/test/ng/directive/inputSpec.js Lines 906 to 916 in e1fe2ac
I'm afraid that I don't know what is the best course of action here :-( |
The real question is this: should a |
I believe it should. In a "yes/no" scenario with no default option selected, it makes sense to map a "no" selection to IMO, checking for |
👍 I agree with @michaelmoussa on this. We have a Yes/No In my opinion it's very well known that Now, for the checkbox use-case mentioned, I guess it may also make sense. Couldn't we make a special case for checkbox only ? Something along the lines of:
|
arrrgh, spent way too much time trying to figure out why my form was throwing required. YES, false should be considered as fulfilling the required condition otherwise it makes our model/view extremely confusing. @michaelmoussa solution works for me. Please, please pretty please use it! |
I agree with @PowerKiKi - we should make the checkbox input a special case but that we shouldn't put the burden of this special case on the required directive. We should change the normal required directive to check only for |
Make the `required` directive accept the model value `false` when it is not an <input> element or when the <input> element is not a checkbox Closes angular#3490 angular#2594
`checkboxInputType` and `ngList` directives need to have special logic for whether they are empty or not. Previously this had been hard coded into their own directives or the `ngRequired` directive. This made it difficult to handle these special cases. This change factors out the question of whether an input is empty into a method `$isEmpty` on the `ngModelController`. The `ngRequired` directive now uses this method when testing for validity and directives, such as `checkbox` or `ngList` can override it to apply logic specific to their needs. Closes angular#3490, angular#3658, angular#2594
`checkboxInputType` and `ngList` directives need to have special logic for whether they are empty or not. Previously this had been hard coded into their own directives or the `ngRequired` directive. This made it difficult to handle these special cases. This change factors out the question of whether an input is empty into a method `$isEmpty` on the `ngModelController`. The `ngRequired` directive now uses this method when testing for validity and directives, such as `checkbox` or `ngList` can override it to apply logic specific to their needs. Closes angular#3490, angular#3658, angular#2594
`checkboxInputType` and `ngList` directives need to have special logic for whether they are empty or not. Previously this had been hard coded into their own directives or the `ngRequired` directive. This made it difficult to handle these special cases. This change factors out the question of whether an input is empty into a method `$isEmpty` on the `ngModelController`. The `ngRequired` directive now uses this method when testing for validity and directives, such as `checkbox` or `ngList` can override it to apply logic specific to their needs. Closes angular#3490, angular#3658, angular#2594
`checkboxInputType` and `ngList` directives need to have special logic for whether they are empty or not. Previously this had been hard coded into their own directives or the `ngRequired` directive. This made it difficult to handle these special cases. This change factors out the question of whether an input is empty into a method `$isEmpty` on the `ngModelController`. The `ngRequired` directive now uses this method when testing for validity and directives, such as `checkbox` or `ngList` can override it to apply logic specific to their needs. Closes angular#3490, angular#3658, angular#2594
I think this was fixed by the commits mentioned above, except for setting the initial checked attribute if the model is set to boolean when the input is compiled, and value is used instead of ng-value. I am going to raise this as a separate issue. |
See Fiddle.
I have a use case where radio input data is stored server-side as booleans - true for "yes", false for "no".
If I use
value="true"
andvalue="false"
on my form, the radio control corresponding to the default value is not selected when the page loads (validation, however, works as expected).If I use
ng-value="true"
andng-value="false"
instead, the default value is selected automatically based on the model as expected, but validation doesn't work correctly. The form is only declared valid if "Yes" is selected, but still invalid if "No" is selected.I can't select "no" by default because the business rules say that the inputs should initially be unselected (presumably so the user has to actually go through the whole form and answer the questions instead of just skipping ahead because an option has already been selected for them).
The text was updated successfully, but these errors were encountered: