Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

$setDirty function should provided by the controller of ng-model directive #10038

Closed
tobyee opened this issue Nov 13, 2014 · 3 comments
Closed

Comments

@tobyee
Copy link
Contributor

tobyee commented Nov 13, 2014

I wan to show the error message when the user submit an invalid and pristine form.
I use the following css selector to style the message, since that I don't wan to show message if the form is pristine.

.ng-dirty.ng-invalid {
   border-color:red;
   xxx
 }

So I do some check in the form submit callbak (ngSubmit), if the form is invalid, I wan to manually set the error control to dirty state, so that the css selector which is .ng-dirty.ng-invalid work.

<form name="theForm" ng-submit="doSubmit()">xxx</form>
$scope.doSubmit = function(){
    if($scope.theForm.$invalid){
        Object.keys($scope.theForm.$error).forEach(function (key) {
            $scope.theForm.$error[key].forEach(function (control) {
                control.$setDirty(); // no $setDirty function
            });
        });
        return;
    }

    // ajax submit
    // ...
}

There is $setDiry function in FormController, but I can't use it, because the function only add ng-dirty class to form element, if I use selector .ng-dirty .ng-invalid insteaded, it will apply to too much input element that all the error message come out if any input change.

I found the $setPristine function in input directive, I think it should provide $setDirty too.

@tobyee tobyee changed the title $setDirty function should provided by the controller of input directive $setDirty function should provided by the controller of ng-model directive Nov 13, 2014
@caitp caitp added this to the Backlog milestone Nov 13, 2014
@caitp
Copy link
Contributor

caitp commented Nov 13, 2014

@Narretz do you have an opinion on this? I'm still leaning towards "don't make ngModelController more complicated", so...

@Narretz
Copy link
Contributor

Narretz commented Nov 13, 2014

Looking at the code for $setPristine, it doesn't look very complicated, so $setDirty would probably be similar, except that it needs to set the parentForm to $dirty, too. If it's just that I think it's okay to have it. Consistency with FormController is also nice to have.

@Narretz
Copy link
Contributor

Narretz commented Nov 13, 2014

See also 9ad7d74

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants