Is there any way to validate an input in angular JS without using form?

4

I have input that I want to validate via AngularJS. But this input is not inside a form. So when trying to access the form's validation information, I was not successful:

<div class="form-group">
    <input name="nick" type="text" class="form-control" ng-model="username.nick" ng-maxlength="10">
    <span class="help-block" ng-show="nick.$error.maxlength">Máximo permitido é 10</span>
</div>

But if I do it that way, it works:

<form name="formUser">
    <div class="form-group">
        <input name="nick" type="text" class="form-control" ng-model="username.nick" ng-maxlength="10">
        <span class="help-block" ng-show="formUser.nick.$error.maxlength">Máximo permitido é 10</span>
    </div>
</form>

In this specific case, I would like to use validation similar to the second example, but without a form.

Is there any way to validate angularity without using the form?

    
asked by anonymous 11.08.2016 / 19:54

1 answer

5

What you can do is to use the ng-form directive. This way, it could look like this:

<div class="form-group" ng-form="nick">
    <input name="nick" type="text" class="form-control" ng-model="username.nick" ng-maxlength="10">
    <span class="help-block" ng-show="nick.$error.maxlength">Máximo permitido é 10</span>
</div>

This policy has all the capabilities of form with name , however it is most commonly used in sub groups. I've seen quite a form wizard that has several steps. There, instead of using multiple forms, they use ng-form in steps that need validation.

Example

    
11.08.2016 / 20:04