Simone
Simone

Reputation: 2430

AngularJs Server side validation

I am using angularjs to submit a really simple form... I must insert a numeric code in my form. The code must be validate server side, calling an external service. When I get the promise I would like to check either the code was correct or not. If the code is not correct I would like to set a red border in the textbox and show a message... here the html

<input name="code" type="text" style="width:155px;" data-ng-model="Codice" 
    data-ng-required="true" data-ng-pattern="/^(\d){22}$/" data-ng-model-options="{ updateOn: 'mousedown blur' }" />

<span data-ng-show="myform.code.$error.pattern" class="error">Codice non valido</span>

Now I let you see what I do in my controller

if (response.Result == "1")
{
   myform.code.$error.pattern = true;
}

In this way I just see the message but the the border of the textbox remain black... so I have tryed:

if (response.Result == "1")
{
   myform.code.$setValidity('mismatch',false);
}

But in this way, even if I modify the code, the model remain "not valid" so I cannot do a post anymore...

Any solution? Thanx

Upvotes: 0

Views: 464

Answers (1)

Anita
Anita

Reputation: 2400

In your html create one span for custom validation

 <span class="help-block" ng-show="myform.name.$error.ntValidName">Not valid name</span>

And in controller set the validity like this,if not valid then

 myform.name.$setValidity("ntValidName", false);

This way you can create your custom validation

Upvotes: 1

Related Questions