Validation messages appearing before sending form

Asked

Viewed 665 times

0

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

How do I do that ?

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>

inserir a descrição da imagem aqui

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 whether the field has been modified and is invalid.
I don’t know what your story is addEmpresa, but if you are receiving the form you can give a form.$setPristine(); that it returns to be a form "virgin" setting all fields as new.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.