-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Not able to reset the invalid values in forms by deleting the model #2743
Comments
As part of our effort to clean out old issues, this issue is being automatically closed since it has been inactivite for over two months. Please try the newest versions of Angular ( Thanks! |
This issue still persists. |
@angadsalaria |
The simplest work around will be to clear out email input field data from the DOM (I know it's not AngularJS way or 'anti-pattern'): jQuery("input[type='email']").val(""); |
@Narretz |
I have the same problem. |
👍 since there are |
Someone in a different issue(#3885) mentioned setting the value to '', but it only works once, to work around that I did this:
|
Here is a another way if you have a lot of inputs in the form and you don't want to reset them one by one: angular.module('myApp')
.controller('FormController', function($scope) {
$scope.model = {};
$scope.reset = function() {
$scope.myForm.$setPristine();
angular.forEach($scope.myForm, (value, key) => {
// filter out angular properties
if (key.indexOf('$') === 0) {
return;
}
// This clears the invalid/valid inputs
$scope.model[key] = null;
});
};
}); |
agree with @loic using angular 1.5.9 now and I spent the last hour struggling to find a way to reset invalid fields of my form! |
Finally, the real good solution is to update directly the model, but in deep with empty strings to be sure that all the fields will be updated. |
use ng-model-options="{ allowInvalid: true}" instead of clear all form fileds . Below is the working solution for reset form with invalid form data.
|
@ViswanadhamK I am using the same form as you mentioned but after reset UI still showing the value which I filled wrong. |
@deeppatidar Could you please send your code. |
I have an idea. You can wrap the inputs in a |
Just ran into this issue. I have a model that's being reset, which makes the input model become |
If you leave values that does not pass validation, it seems that the values are not pushed to the model. Issue: #1412
The extension of this problem is that if you delete the model, the input fields containing the invalid values will not be cleared because the field and model are now decoupled and won't be reattached before it contains a valid value again/is emptied as far as I can tell.
With the email field, it seems that it works if you set the model to an empty string, but you cannot do that with a numbers field (aka. you cannot return the numbers field through the model to an empty state from an invalid value).
Another side effect this has is that the form is flagged as pristine until a valid value is entered in the numbers field... This is not the behaviour of the email field though, so it might be another issue entirely.
The text was updated successfully, but these errors were encountered: