Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was 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

Description

@tobyee

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions