-
Notifications
You must be signed in to change notification settings - Fork 27.4k
form.$error.required and form.$invalid validators are not syncing #14734
Comments
This seems like an Angular bug indeed. BTW, it has been broken since |
Oh ok, so that's why thanks for the response |
After further investigation, I'm not sure if this is Angular's fault or your directive's. Here's what happens: function (modelValue, viewValue) {
var check = true;
if (modelValue) {
...
}
return check;
} Now, Angular normally expects a boolean (true or false) to be returned from a validator. Returning Feature or Bug ? I lean towards the latter. @Narretz, thoughts ? BTW, the fix would be to change the first line here, so that result is always a boolean, regardless of what the validator returns: |
Ok, I understand your point but since the $validator is 'available' as it is declared. ngModel.$validators.available = function (modelValue, viewValue) { } It should have been only reflected on the $error.available and not the whole $invalid.. That is from my point of view. I'm aware that this can be solved by doing a little extra conditional in the directive like if(ngModel.$error.required){ But that's another additional manual coding. So as much as possible I would like to avoid it. But thanks for the response, I don't know if it still considered as a bug though |
Not really. The
Not sure what you mean. |
This is what I meant Just check the code below and just take a look on the 2nd to 6th line that I have added. ngModel.$validators.available = function (modelValue, viewValue) {
It's working great so far. You can take a loot at this plunker prevew: http://plnkr.co/edit/NuDGB64IetpcsaVB03T7?p=preview |
I still don't understand. The Previously, you were returning |
Ok, understood. Thanks for the help! |
Regarding your comment in #14734 (comment) @gkalpak about returning |
I think it makes sense to convert the value to boolean. Note that we are already (sort of) doing this, when we determine the overall sync validators validity (here. But we are still setting the validity of the specific validator to Essentially, I am proposing to change this line (which will only affect the behavior of that line) like this: - var result = validator(modelValue, viewValue);
+ var result = !!validator(modelValue, viewValue); |
While writing up tests for the change, I noticed something possibly unexpected. Aside from the expected
Since all of these values are falsey, this change will actually alter their behaviour if used as the return values of validator functions. Of course, we only ever expected Edit: Note: It would appear every value results in |
@BobChao87, do you have a PR or branch I can take a look at? What tests are you referring to exactly? |
Hopefully that resolves any questions about what I was trying to describe. The changes are all in the negative tests. |
Hi, Because $error is object will have error if it is invalid. while debugging Thanks, On Tue, Sep 27, 2016 at 1:56 PM, BobChao87 [email protected] wrote:
|
@BobChao87, I see. This seems to be an unexpected side effect of an unintended use. Internally, the
AFAICT, this last "feature" is intended for internal use only and specifically used with |
Hmmm, okay, sounds good to me. I'll clean up the code and write up a breaking change note and get that in in the next couple of days. I agree, should be a very small cross-section of users that will be affected by this change. |
Change synchronous validators to convert the return to boolean value. Prevent unexpected behavior when returning `undefined`. Closes angular#14734 BREAKING CHANGE: Previously, only a literal `false` return would resolve as the synchronous validator failing. Now, all traditionally false JavaScript values are treated as failing the validator, as one would naturally expect. To migrate, make sure your synchronous validators are returning either a literal `true` or a literal `false` value. For most code, we expect this to already be the case. Only a very small subset of projects will affected. Namely, anyone using `undefined` or any falsy value as a return will now see their validation failing, whereas previously falsy values other than `undefined` would have been seen as passing and `undefined` would have been seen as pending.
Change synchronous validators to convert the return to boolean value. Prevent unexpected behavior when returning `undefined`. Closes angular#14734 BREAKING CHANGE: Previously, only a literal `false` return would resolve as the synchronous validator failing. Now, all traditionally false JavaScript values are treated as failing the validator, as one would naturally expect. Specifically, the values `0` (the number zero), `null`, `NaN` and `''` (the empty string) used to considered valid (passing) and they are now considered invalid (failing). The value `undefined` was treated similarly to a pending asynchronous validator, causing the validation to be pending. `undefined` is also now considered invalid. To migrate, make sure your synchronous validators are returning either a literal `true` or a literal `false` value. For most code, we expect this to already be the case. Only a very small subset of projects will be affected. Namely, anyone using `undefined` or any falsy value as a return will now see their validation failing, whereas previously falsy values other than `undefined` would have been seen as passing and `undefined` would have been seen as pending.
Change synchronous validators to convert the return to boolean value. Prevent unexpected behavior when returning `undefined`. Closes angular#14734 BREAKING CHANGE: Previously, only a literal `false` return would resolve as the synchronous validator failing. Now, all traditionally false JavaScript values are treated as failing the validator, as one would naturally expect. Specifically, the values `0` (the number zero), `null`, `NaN` and `''` (the empty string) used to considered valid (passing) and they are now considered invalid (failing). The value `undefined` was treated similarly to a pending asynchronous validator, causing the validation to be pending. `undefined` is also now considered invalid. To migrate, make sure your synchronous validators are returning either a literal `true` or a literal `false` value. For most code, we expect this to already be the case. Only a very small subset of projects will be affected. Namely, anyone using `undefined` or any falsy value as a return will now see their validation failing, whereas previously falsy values other than `undefined` would have been seen as passing and `undefined` would have been seen as pending.
Change synchronous validators to convert the return to boolean value. Prevent unexpected behavior when returning `undefined`. Closes angular#14734 BREAKING CHANGE: Previously, only a literal `false` return would resolve as the synchronous validator failing. Now, all traditionally false JavaScript values are treated as failing the validator, as one would naturally expect. Specifically, the values `0` (the number zero), `null`, `NaN` and `''` (the empty string) used to considered valid (passing) and they are now considered invalid (failing). The value `undefined` was treated similarly to a pending asynchronous validator, causing the validation to be pending. `undefined` is also now considered invalid. To migrate, make sure your synchronous validators are returning either a literal `true` or a literal `false` value. For most code, we expect this to already be the case. Only a very small subset of projects will be affected. Namely, anyone using `undefined` or any falsy value as a return will now see their validation failing, whereas previously falsy values other than `undefined` would have been seen as passing and `undefined` would have been seen as pending.
Refactor ngModelSpec to use internal helper function `valueFn`. Use instead of multiple-defining a function called `curry`. Addresses a quick change mentioned in PR 15208 from Issue angular#14734
Refactor ngModelSpec to use internal helper function `valueFn`. Use instead of multiple-defining a function called `curry`. PR (angular#15231) Addresses a quick change mentioned in PR 15208 from Issue angular#14734
Refactor ngModelSpec to use internal helper function `valueFn`. Use instead of multiple-defining a function called `curry`. PR (angular#15231) Addresses a quick change mentioned in PR 15208 from Issue angular#14734
Refactor ngModelSpec to use internal helper function `valueFn`. Use instead of multiple-defining a function called `curry`. PR (angular#15231) Addresses a quick change mentioned in PR 15208 from Issue angular#14734
Refactor ngModelSpec to use internal helper function `valueFn`. Use instead of multiple-defining a function called `curry`. PR (angular#15231) Addresses a quick change mentioned in PR 15208 from Issue angular#14734
Refactor ngModelSpec to use internal helper function `valueFn`. Use instead of multiple-defining a function called `curry`. PR (angular#15231) Addresses a quick change mentioned in PR 15208 from Issue angular#14734
Change synchronous validators to convert the return to boolean value. Prevent unexpected behavior when returning `undefined`. Closes angular#14734 Closes angular#15208 BREAKING CHANGE: Previously, only a literal `false` return would resolve as the synchronous validator failing. Now, all traditionally false JavaScript values are treated as failing the validator, as one would naturally expect. Specifically, the values `0` (the number zero), `null`, `NaN` and `''` (the empty string) used to considered valid (passing) and they are now considered invalid (failing). The value `undefined` was treated similarly to a pending asynchronous validator, causing the validation to be pending. `undefined` is also now considered invalid. To migrate, make sure your synchronous validators are returning either a literal `true` or a literal `false` value. For most code, we expect this to already be the case. Only a very small subset of projects will be affected. Namely, anyone using `undefined` or any falsy value as a return will now see their validation failing, whereas previously falsy values other than `undefined` would have been seen as passing and `undefined` would have been seen as pending.
Refactor ngModelSpec to use internal helper function `valueFn`. Use instead of multiple-defining a function called `curry`. PR (angular#15231) Addresses a quick change mentioned in PR 15208 from Issue angular#14734
I was wondering if this is an angular bug. The $invalid is undefined while my $error.required is true when a custom $validators is declared on a directive.
Check this example: http://plnkr.co/edit/NuDGB64IetpcsaVB03T7?p=preview
The text was updated successfully, but these errors were encountered: