Validation messages appearing before submitting form

0

I am creating a form with Ionic and want to validate the fields after the submitted form. The way I am doing it works, but when I open the page the fields are already marked as invalid and I want it to happen after the form is submitted if there is any field that has not been filled.

How do I do this?

I'm trying like this.

<form name="Empresa" novalidate ng-submit="addEmpresa(Empresa);">
                      <div class="list">
                            <label class="item item-input" 
                                   ng-class="{'has-errors':Empresa.nomeemp.$invalid, 'no-errors':Empresa.nomeemp.$valid}">
                                <input type="text" 
                                       placeholder="Nome" 
                                       name="nomeemp" 
                                       ng-model="Empresa.nome" 
                                       ng-required="true">
                            </label>
                            <div class="error-container"
                                 ng-messages="Empresa.nomeemp.$error">
                                <div ng-messages-include="templates/form-errors.html"></div>
                            </div>

<button type="submit" class="button button-block button-energized">Cadastrar</button>

</form>

    
asked by anonymous 04.01.2016 / 12:52

1 answer

0

You can also validate if the field has already been modified:

!Empresa.nomeemp.$pristine && Empresa.nomeemp.$invalid

$pristine means that the field has not been modified, in this case it is validating if the field has been modified and is invalid. Home I do not know how its addEmpresa function, but if you are getting form you can give form.$setPristine(); that it returns to being a "virgin" form set all fields as new.

    
17.02.2016 / 13:48