2015-09-01 17 views
6

Ho creato un modulo in convalida ionico-angolare e applicato su di esso.Le convalide non funzionano correttamente.Anche tutti i campi sono vuoti al clic del pulsante di invio chiama la funzione controller. Per favore aiutami a risolvere questo problema.Convalida modulo angolare non funzionante correttamente

codice html

<ion-pane> 
<ion-header-bar class="bar-stable"> 
<h1 class="title">Register</h1> 
</ion-header-bar> 
<ion-content > 
<form name="register" ng-submit="submitDetails(user)" novalidate=""> 
    <div class="list"> 
     <label class="item item-input item-floating-label" style="position:relative;"> 
      <span class="input-label">First Name</span> 
      <input type="text" placeholder="First Name" ng-model="user.firstName" ng-required="true"> 
      <p ng-show="user.firstName.$invalid && !user.firstName.$pristine" class="help-block">You name is required.</p> 
     </label> 
     <label class="item item-input item-floating-label"> 
      <span class="input-label">Email</span> 
      <input type="email" placeholder="Email" ng-model="user.email" ng-required="true"> 
      <p ng-show="user.email.$invalid && !user.email.$pristine" class="help-block">Enter a valid email</p> 
     </label> 
     <label class="item item-input item-floating-label"> 
      <span class="input-label" >Phone no</span> 
      <input type="number" placeholder="Phone No" ng-model="user.phone" ng-minlength="10" ng-maxlength="10" ng-required="true"> 
      <span class="help-block" ng-show="user.phone.$error.required || user.phone.$error.number">Valid phone number is required</span> 
      <span class="help-block" ng-show="((user.phone.$error.minlength || user.phone.$error.maxlength) && user.phone.$dirty) ">phone number should be 10 digits</span> 
     </label> 
     <input type="submit" class="button button-royal" value="register"> 
    </div> 
</form> 
</ion-content> 
</ion-pane> 

Controller cod

chatApp.controller('RegisterCntrl', function($scope, $stateParams) { 
    $scope.user={}; 
    $scope.submitDetails=function(user){ 
     alert("user"+user.firstName); 
    }; 
}); 
+0

Questo 'ng-spettacolo =" user.firstName. $ Invalid' non è valido nome del modulo, è 'register' –

+1

Sì registro è nome del modulo allora cosa devo fare per rendere la convalida di lavoro ?? @evc – Keshav

risposta

6

Questo dovrebbe funzionare

<form name="register_form" ng-submit="submitDetails(user)" novalidate=""> 
    <div class="list"> 
     <label class="item item-input item-floating-label" style="position:relative;"> 
      <span class="input-label">First Name</span> 
      <input type="text" name="user_first_name" placeholder="First Name" ng-model="user.firstName" ng-required="true"> 
      <p ng-show="register_form.user_first_name.$invalid && !register_form.user_first_name.$pristine" class="help-block">You name is required.</p> 
     </label> 
     <!--omitted--> 
     <input type="submit" class="button button-royal" value="register"> 
    </div> 
</form> 

Nome modulo è register_form,

<form name="register_form" ng-submit="submitDetails(user)" novalidate=""> 

nome di input è user_first_name,

<input type="text" name="user_first_name" placeholder="First Name" ng-model="user.firstName" ng-required="true"> 

Così la convalida deve passare attraverso quei campi

<p ng-show="register_form.user_first_name.$invalid && !register_form.user_first_name.$pristine" class="help-block">You name is required.</p> 

Modello stesso non ha $invalid o $pristine proprietà, quindi non ha senso

Per il campo telefonico

<input type="number" name="user_phone" placeholder="Phone No" ng-model="user.phone" ng-minlength="10" ng-maxlength="10" ng-required="true"> 
<span class="help-block" ng-show="register_form.user_phone.$error.required || register_form.user_phone.$error.number">Valid phone number is required</span> 
<span class="help-block" ng-show="((register_form.user_phone.$error.minlength || register_form.user_phone.$error.maxlength) && register_form.user_phone.$dirty) ">phone number should be 10 digits</span> 

Per ulteriori letture, cassa this answer

+0

sì la sua direttiva di lavoro ma ng-minlength e ng-maxlength che ho applicato sul campo numero di telefono non funziona – Keshav

+0

Usa lo stesso approccio, rinominare il nome del modulo, quindi aggiungere il nome di input, quindi usarli all'interno delle condizioni –

+0

L'ho usato come hai spiegato che la validazione funziona ma le direttive (ng-minlength e ng-maxlength) non funzionano. – Keshav

4

Utilizzare l'attributo form name e input name, non ng-model

dare l'input un nome e utilizzarlo con nome forma.

Controllare il primo input nell'esempio seguente.

<label class="item item-input item-floating-label" style="position:relative;"> 
    <span class="input-label">First Name</span> 
    <input name="firstName" type="text" placeholder="First Name" ng-model="user.firstName" ng-required="true"> 
    <p ng-show="register.firstName.$invalid && !register.firstName.$pristine" class="help-block">Your name is required.</p> 
</label> 
Problemi correlati