Corner plausibility check after input change

Updated the violin question.

The original is here: https://stackoverflow.com/questions/31874313/angularjs-clean-remote-validation-error-after-change-input

In my form, I have two checks. First local, second - remote.

So this is my example

<form ng-controller="MyCtrl" name="Form">
<div class="form-group">
    <label class="control-label">
        First Name 
    </label>
    <input type="text" class="form-control" name="firstName" ng-model="myModel.firstName" required />
    <span class="error" ng-if="Form.firstName.$dirty && Form.firstName.$invalid" ng-repeat="(e, b) in Form.firstName.$error">{{e}}</span>
</div>
<input type="submit" ng-click="submit(Form)">
</form>

      

Here is the controller

function MyCtrl($scope, $element) {

    $scope.submit = function (form) {

        if (form.$invalid) {
            renderErrors(form);
            return;
        }
        console.log('local validation passed');

        // imitation of remote error
        // send, then data

        if($scope.myModel.firstName === 'Tom')
            renderServerErrors({firstName: ['Already in use']}, form);
        else
            alert('Success');


    }

    /**
     * Errors will appear below each wrong input
     */
    var renderErrors = function(form){

            var field = null;
        for (field in form) {
            if (field[0] != '$') {

                if (form[field].$pristine) {
                    form[field].$dirty = true;
                }
            }
        }

    };

    /**
     * Server errors will appear below each wrong input
     */
    var renderServerErrors = function(err, form){

        var field = null;
        _.each(err, function(errors, key) {
            _.each(errors, function(e) {

                form[key].$dirty = true;
                form[key].$setValidity(e, false);

            });
        });


    }
}

      

http://jsfiddle.net/uwozaof9/6/

If you type "Tom" on the tab, you will no longer submit the form.

And I want to remove server errors from the input error stack when it changes.

Please, help!

+3


source to share


1 answer


You only seem to have set the invalid, but not set the value after fixing it. IF you do it yourself you also need to implement the $ valid parameter if imput is valid.



0


source







All Articles