0

I have an form with 3 input fields, who will validate. Important is to change the error respectively success class when the user does an interaction with the form.

<form class="form-horizontal" name="myForm" novalidate>
  <div class="form-group-sm has-feedback" ng-class="{ 'has-error' : myForm.Name.$invalid && !myForm.Name.$pristine || myForm.Name.$touched }">
    <label class="control-label" for="name-id">Name</label>
    <input type="text" 
                   class="form-control" 
                   placeholder="Name"  
                   name="Name"
                   ng-model="selected.name"
                   ng-pattern="/^[a-zA-Z]+/" 
                   ng-required="true"
            />
    <p class="help-block" ng-show="myForm.Name.$error.required && myForm.Name.$touched">Name is required</p>
    <p class="help-block" ng-show="myForm.Name.$error.pattern">Name must be written with letters</p>
 </div>
...

When the user considered the required field and the pattern then after the interaction with the input field, it should be changes the css class to has-success.

I've tried this:

<div class="form-group-sm has-feedback" ng-class="myForm.Name.$invalid && !myForm.Name.$pristine && myForm.Name.$touched ? 'has-error' : 'has-success'">
...

But it doesn't work correctly. Can anyone help me?


I've found the solution. Here is my code:

<div class="form-group-sm has-feedback" ng-class="{ 'has-error' : myForm.Name.$invalid && !myForm.Name.$pristine && myForm.Name.$touched, 'has-success' : myForm.Name.$valid }">
...
</div>

2 Answers 2

1

Try it this way:

<div class="form-group-sm has-feedback" ng-class="{'has-error': myForm.Name.$invalid && !myForm.Name.$pristine && myForm.Name.$touched}">
Sign up to request clarification or add additional context in comments.

7 Comments

Yeah this is only for has-error but I've found the solution in an other thread. I will edit my question post with the answer :)
@yuro you should extract this condition as a function hasError(myForm) and then apply this as: {'has-error': hasError(myForm), 'has-success': !hasError(myForm)}
How can I define this in a function? Do I have to define the value as like a string in the controller?
@yuro You define it in controller $scope.hasError = function (form) { return form.Name.$invalid && !form.Name.$pristine && form.Name.$touched; };
@yuro You send your form. <form name="myForm"> ... it's available via variable myForm and you can pass it to the controller function.
|
0

I would recomment a directive like this, as it makes your html a lot shorter.

/* Attach this to input fields to replicate the foundation abide behaviour for invalid form inputs
 * Marks the parent with class 'error' and toggles ng-hide on all elements with class error within
 * the parent element of the input */
.directive('ppHasError', [function() {
    return {
        restrict: 'A', //attribute only
        require: '?ngModel',
        link: function(scope, element, attrs, ngModel) {
            var formName = element.parents('form[name]').attr('name');
            if( !ngModel )
                return;
            var parent = element.parent();
            if(parent.is('label'))
                parent = parent.parent();
            var errEl = parent.find('.error');
            scope.$watchGroup([formName + '.' + ngModel.$name + '.$invalid', formName + '.' + ngModel.$name + '.$touched'], function(newValues, oldValues) {
                var isInvalid = newValues[0];
                var touched = newValues[1];
                var isError = isInvalid && touched;
                parent.toggleClass('error', isError);
                errEl.toggleClass('ng-hide', !isError);
            });
        }
    };
}])

which can be used like that:

<div class="medium-10 small-9 columns">
    <label>{{ 'card_number' | translate }}</label>
    <input class="card-number" name="creditcardNumber" ng-model="paymentInfo.creditcardNumber" size="20" placeholder="4111111111111111" type="text"
        pp-has-error required="true">
    <small class="error">
        {{ 'card_number_error' | translate }}
    </small>
</div>

It will set the CSS class 'error' on the parent div and toggle the small.error tag using the ng-hide CSS class.

1 Comment

I will try this and then write my comment here. but thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.