Angular form validation doesn't work as expected - angularjs

I have troubles understanding how it works... basically when I submit my form, it is validated without checking the required inputs nor anything else.
I've set the 'novalidate' attribute to disable the HTML5 validation, then I've set all my fields as 'required' (same for my mail input with an email validation), and I'm using 'ngMessages' from Angular to check the inputs (is that even necessary??). But I can still submit the form without any field filled in... What am I doing wrong? Could it come from the fact that I use ng-submit and not ng-click?
Any help is appreciated !
https://jsfiddle.net/WebMoutarde/n1mocvco/
<div ng-controller="MyCtrl">
<form name="form" novalidate ng-submit="vm.signup()">
<div class="list">
<label class="item item-input item-stacked-label noborder">
<span class="input-label">Nom</span>
<input type="text" name="nom" ng-model="vm.nom" required>
</label>
<hr style="border-style: solid;margin-left: 16px;margin-right: 16px;border-color: #e8e8e8;"/>
<label class="item item-input item-stacked-label noborder">
<span class="input-label">Prénom</span>
<input type="text" name="prenom" ng-model="vm.prenom" required>
</label>
<hr style="border-style: solid;margin-left: 16px;margin-right: 16px;border-color: #e8e8e8;"/>
<label class="item item-input item-stacked-label noborder">
<span class="input-label">Mail</span>
<input type="email" name="mail" ng-model="vm.mail" required>
</label>
<hr style="border-style: solid;margin-left: 16px;margin-right: 16px;border-color: #e8e8e8;"/>
<label class="item item-input item-stacked-label noborder">
<span class="input-label">Mot de passe</span>
<input type="password" name="password" ng-model="vm.password" required>
</label>
<hr style="border-style: solid;margin-left: 16px;margin-right: 16px;border-color: #e8e8e8;"/>
<label class="item item-input item-stacked-label noborder">
<span class="input-label">Confirmez votre mot de passe</span>
<input type="password" name="passwordCheck" ng-model="vm.passwordCheck" required>
</label>
<hr style="border-style: solid;margin-left: 16px;margin-right: 16px;border-color: #e8e8e8;"/>
</div>
<div ng-messages="form.$error" role="alert">
<p style="text-align: center;" ng-message="required">You must fill in all fields</p>
<p style="text-align: center;" ng-message="email">Your email address is invalid</p>
</div>
<div class="item noborder">
<button class="button button-block button-positive" type="submit" >Créer un compte</button>
</div>
</form>
</div>
I've seen many SO questions about this but I still missing something...

You could disabled form submit button till form gets into valid state by using ng-disabled directive on form button
<button class="button button-block button-positive"
type="submit"
ng-disabled="form.$invalid">
Créer un compte
</button>
Or even better would be verify form has been valid or ng-submit method and then call your signup method of controller like below.
ng-submit="form.$valid && vm.signup()"

Related

AngularJs - Cannot read property '$valid' of undefined

I've reading how to handle whether a forms is valid or invalid, and I can't figure it out why I cannot make my code work properly.
My Html form
<form name="signupForm" ng-submit="submitRegistration(signup)" ng-init="signup = {}" novalidate>
<input type="email" placeholder="Email" name="email" ng-model="signup.email"required></input>
<input type="password" placeholder="Password" name="password" ng-model="signup.password"required></input>
<button type="submit" class="button button-block button-balanced">
Registrarme!
</button>
</form>
My controller
$scope.submitRegistration = function(signupForm){
debugger;
$scope.msg = {};
if ($scope.signupForm.$valid){
//something}else{
console.log('INVALID')
}
I tried $scope.signupForm.$valid and signupForm.$valid... both I got undefined.
How can I do this in the controller side?
EDIT this is my full code (is registration form, I am using Ionic framework)
<ion-view view-title="Registrate!">
<ion-content class="padding">
<h1 class="general-title">BuenjugadorApp</h1>
<button class="button button-block facebook-button icon-left ion-social-facebook" ng-click="facebookSignup()">Registrate con Facebook</button>
<button class="button button-block button-balanced" ng-click="nosotros = true">Registrate con Nosotros</button>
<div class="error-notice" ng-if="msg.error">
<span>¡Ocurrió un error! Vuelve a intentarlo.</span>
</div>
<div class="error-notice" ng-if="msg.required">
<span>¡Todos los campos deben ser completados!</span>
</div>
<div class="error-notice" ng-if="msg.different_passwords">
<span>¡Las contraseñas ingresadas no coinciden!</span>
</div>
<div class="error-notice" ng-if="msg.too_big_password">
<span>¡Las contraseña es demasiado larga, no puede superar los 12 caracteres!.</span>
</div>
<br>
<div >
<form name="signupFormulario" ng-submit="submitRegistration(signupForm)" ng-init="signupForm = {}" novalidate>
<div class="list">
<label class="item item-input item-floating-label">
<span class="input-label">Email</span>
<input type="email" placeholder="Email" name="email" ng-model="signupForm.email"required>
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Nombre</span>
<input type="text" placeholder="Nombre" name="nombre" ng-model="signupForm.nombre"required>
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Apellido</span>
<input type="text" placeholder="Apellido" name="apellido" ng-model="signupForm.apellido"required>
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Contraseña</span>
<input type="password" placeholder="Contraseña" name="password" ng-model="signupForm.password"required>
</label>
<span class="advise">La contraseña debe ser mayor o igual a 6 caracteres.</span>
<label class="item item-input item-floating-label">
<span class="input-label">Repetir Contraseña</span>
<input type="password" placeholder="Repetir Contraseña" name="password" ng-model="signupForm.password_confirmation"required>
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Edad</span>
<input type="number" placeholder="Edad" name="edad" ng-model="signupForm.edad" min="10" max="99" required>
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Teléfono</span>
<input type="number" placeholder="Teléfono" name="telefono" max="999999999999999" ng-model="signupForm.telefono" required>
</label>
<span class="advise">Debes ingresar tu número para que los jugadores puedan contactarse mejor con vos.</span>
<label class="item item-input">
</label>
<label class="item item-input item-select">
<div class="input-label">
Tu Equipo
</div>
<select ng-model="signupForm.equipo2" ng-init="signupForm.equipo2 = {}" required>
<option ng-value="equipo" ng-repeat="equipo in equipos">{{equipo.nombre}}</option>
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">
¿De qué jugás?
</div>
<select name="posicion" ng-model="signupForm.posicion" required>
<option ng-value="posicion.nombre" ng-repeat="posicion in posiciones">{{posicion.nombre}}</option>
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">
¿De dónde sos?
</div>
<select name="lugar" ng-model="signupForm.provincia" ng-change="cambiovalor(signupForm.provincia)" required>
<option ng-value="prov" data-ng-repeat="(prov, ciudadesss) in complejos">{{prov}}</option>
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">
¿Ciudad?
</div>
<select ng-disabled="!signupForm.provincia" ng-model="signupForm.ciudad" name="city" required>
<option ng-value="ciudad" data-ng-repeat="ciudad in ciudades">{{ciudad}}</option>
</select>
</label>
<button type="submit" class="button button-block button-balanced">
Registrarme!
</button>
<div class="centered">
<a ng-href="#/login">Ya tengo una cuenta.</a>
</div>
</div>
</form>
</div>
</ion-content>
</ion-view>

How to disable submit button untill all the fields are filled in a form?

I have a form with a set of fields. My problem is, submit button is disabled initially but the moment any one of the field goes valid or non-empty button is getting enabled. Here is my source code:
<form class="aui newDiscoveryForm" name="newDiscoveryForm" ng-submit="createNewDiscovery(user)" novalidate>
<fieldset class="group">
<div class="field-group">
<label class="label">Product Name</label>
<input class="text" type="text" name="input1" ng-model="user.productName" value="" id="productName" required/>
<p ng-show="newDiscoveryForm.input1.$invalid && !newDiscoveryForm.input1.$pristine" style="color: #880000">Product name is required.</p>
<div class="error"></div>
<span class="result_product" style="color: #880000"></span>
</div>
<div class="field-group">
<input class="text" type="text" name="input2" ng-model="user.endUsers" value="" required/>
<p ng-show="newDiscoveryForm.input2.$invalid && !newDiscoveryForm.input2.$pristine" style="color: #880000">EndUsers required.</p>
<label class="label">Who are end users</label>
<div class="description">[Gamers, Engineers, Commuters, Media, Goverment]</div>
</div>
<div class="field-group">
<label for="licenseKey">What Problem Are They Facing Today</label>
<textarea class="textarea" rows="4" cols="10" name="input3" ng-model="user.problemsArea" id="problemsarea" value="" required></textarea>
<p ng-show="newDiscoveryForm.input3.$invalid && !newDiscoveryForm.input3.$pristine" >ProblemsArea required.</p>
<div class="description">Spend So much in .....</div>
</div>
<div class="field-group">
<label class="label">What kind of product is this</label>
<input class="text" type="text" name="input4" ng-model="user.productKind" id="productkind" value="" required/>
<p ng-show="newDiscoveryForm.input4.$invalid && !newDiscoveryForm.input4.$pristine" >ProductKind required.</p>
<div class="description">[Software, MobileApp, JIRA-Plugin]</div>
</div>
<div class="field-group">
<label for="d-lname">How do you plan to solve the problem</label>
<input class="text long-field" type="text" id="problemSoln" name="input5" ng-model="user.problemSoln" value="" required />
<p ng-show="newDiscoveryForm.input5.$invalid && !newDiscoveryForm.input5.$pristine" >ProblemSolution required.</p>
<div class="error"></div>
<div class="description">[Load Balancing of Personal, Automated Traffic Info]</div>
</div>
<div class="field-group">
<label for="d-lname">Who are your competitors</label>
<input class="text long-field" type="text" id="competitors" name="input6" ng-model="user.competitors" value="" required/>
<p ng-show="newDiscoveryForm.input6.$invalid && !newDiscoveryForm.input6.$pristine" >Competitors required.</p>
<div class="error"></div>
<div class="description">Traditional Commuting Solution</div>
</div>
<div class="field-group">
<label for="d-lname">How do you differntiate from your competitors</label>
<input class="text long-field" type="text" id="differentiator" name="input7" ng-model="user.differentiator" value="" required/>
<p ng-show="newDiscoveryForm.input7.$invalid && !newDiscoveryForm.input7.$pristine" >Differentiator required.</p>
<div class="error"></div>
<div class="description">[Automated, Secure]</div>
</div>
</fieldset>
<div class="buttons-container">
<div class="buttons">
<button class="aui-button aui-button-primary ap-dialog-submit" value="Submit" type="submit"
id="save-button" ng-click = "createNewDiscovery(user)" ng-disabled="newDiscoveryForm.$invalid">Save</button>
<button id="close-button" type="button" class="aui-button aui-button-link ap-dialog-cancel" ng-click = "cancelClick()">Cancel</button>
</div>
</div>
</form>
How can I make sure that submit button is disabled untill all the fields are filled.
I tried almost all the available solutions like make all the fields required, make the submit button as ./. But nothing seems to be working.
You are almost doing it right. To use angular's form validation, you have to use the angular directives for that. For example, use the ng-required instead of the normal required (though it will work, but you should use ng-required for best practices):
<form name="newDiscoveryForm">
<input type="text" name="someName"
ng-model="someModel"
ng-required="true" /> <!-- use ng-required -->
<!-- other inputs -->
<!-- $invalid will evaluate to true if the `ng-required` are not valid -->
<button type="submit"
ng-disabled="newDiscoveryForm.$invalid">
Submit!
</button>
</form>
See this JSFIDDLE

How to validate a combo box in ionic framework

I am creating a mobile app using ionic framework.
I have a form which I have created for my hybrid mobile application..
I need to check whether the user has filled all the fields in the form..
my code...
<ion-view view-title="Request">
<ion-content>
<form novalidate>
<div class="list">
<label class="item item-input item-select">
<div class="input-label">
Request Type:
</div>
<select>
<option selected>--Please select--</option>
<option>Car Pass Ticket</option>
<option>Seminar Pass</option>
<option>Identy Card</option>
</select>
</label>
<label class="item item-input">
<textarea placeholder="Description" name="description" ng-minlength="20" required ></textarea>
</label>
<br/>
<!-- <div class="padding">
<button class="button button-positive" ng-click="submit(description)">
Submit
</button>
</div> -->
<div class="padding">
<button class="button button-positive" ng-disabled="request.$invalid" ng-click="submit(description)">
Submit
</button>
</div>
</div>
</form>
</ion-content>
</ion-view>
Can some one kindly help me to validate the combo-box..
any kind of help is highly appreciated......
This should work
<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>
Form name is register_form,
<form name="register_form" ng-submit="submitDetails(user)" novalidate="">
Input name is user_first_name,
<input type="text" name="user_first_name" placeholder="First Name" ng-model="user.firstName" ng-required="true">
So validation must pass through those fields
<p ng-show="register_form.user_first_name.$invalid && !register_form.user_first_name.$pristine" class="help-block">You name is required.</p>
Model itself doesn't have $invalid or $pristine properties, so it doesn't make sense
For phone field
<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>
Try this:
1) Give name attribute to your form
<form name="myForm" novalidate>
2) declare the request types inside of your scope like this:
$scope.requestType = [
{ code: "carPass", name: "Car Pass Ticket" },
{ code: "seminarPass", name: "Seminar Pass" },
{ code: "identityCard", name: "Identy Card"}
];
3) declare select box like this:
<select name="requestType" ng-model="request" required
ng-options="request.code as request.name for request in requestType" >
<option value="">--Please select--</option>
</select>
4) Inside submit method check for $valid attribute of form.
$scope.submit1 = function(description){
if($scope.myForm.$valid){
// Do your stuff
}else{
// Do your stuff
}
}

Form validation error when using ion-scroll tag

I'm creating an Ionic app and I'm having some issues with form validation when I use < ion-scroll> tag. This is my form:
<form ng-show="!success" name="form" role="form" novalidate ng-submit="register()" show-validation>
<div class="list list-inset">
<label class="item item-input">
<input type="text" class="form-control" name="firstName" placeholder="Nome"
ng-model="registerAccount.firstName" ng-minlength=1 ng-maxlength=50 required maxlength="50">
</label>
<div ng-show="form.firstName.$dirty && form.firstName.$invalid" class="padding-top padding-left assertive">
<p class="help-block"
ng-show="form.firstName.$error.required">
some message
</p>
</div>
.
.
.
<label class="item item-input">
<input type="password" class="form-control" name="password" placeholder="Senha"
ng-model="registerAccount.password" ng-minlength=5 ng-maxlength=50 required>
</label>
<div ng-show="form.password.$dirty && form.password.$invalid" class="padding-top padding-left assertive">
<p class="help-block"
ng-show="form.password.$error.required">
some message
</p>
</div>
<label class="item item-input">
<input type="password" class="form-control" name="confirmPassword" placeholder="Repita a senha"
ng-model="confirmPassword" ng-minlength=5 ng-maxlength=50 required>
</label>
<div ng-show="form.confirmPassword.$dirty && form.confirmPassword.$invalid" class="padding-top padding-left assertive">
<p class="help-block"
ng-show="form.confirmPassword.$error.required">
some message
</p>
<p class="help-block"
ng-show="form.confirmPassword.$error.minlength">
Sua confirmação de senha precisa ter no mínimo 5 caracteres.
</p>
<p class="help-block"
ng-show="form.confirmPassword.$error.maxlength">
Sua confirmação de senha deve ser menor que 50 caracteres.
</p>
</div>
</div>
<div class="form-button">
<button type="submit" class="button button-block button-balanced">Criar conta</button>
</small>
</div>
</form>
This form is working correctly when there is no Ionic's tag, however, when I add a ion-scroll tag, either inside or outside form tag, the validation fails saying that password and confirm password are different. Should I use a specific Ionic tag with form? Which could be the tag I should use?
The problem is because some ionic's components uses child scope, this kind of scope "creates an 'isolate' scope that does not prototypically inherit."
I solved my problem using the recommended best practice of use "." (dot) in my models. So I changed:
<input type="password" ng-model="confirmPassword" required>
to
<input type="password" ng-model="something.confirmPassword" required>

angular isn't respecting all my required fields

In my page below everything is working except for the user.productNumber input at the bottom. What I mean is that all the "required" fields are keeping the "Create Account" button disabled but it's like the productNumber input is being ignored. How can I get this working?
<ion-view title="Sign Up">
<ion-content>
<form name="signUpForm">
<div class="list">
<label class="item item-input item-stacked-label">
<input data-ng-model="user.email" data-ng-pattern="/^[_a-z0-9]+(\.[_a-z0-9]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/" type="email" placeholder="user#mail.net" required>
</label>
<label class="item item-input item-stacked-label">
<input data-ng-model="user.password" type="password" placeholder="password" required>
</label>
<label class="item item-input item-stacked-label">
<input data-ng-model="user.passwordAgain" type="password" placeholder="password (again)" required>
</label>
<li class="item item-checkbox">
<label class="checkbox">
<input data-ng-model="user.newsletter" type="checkbox" checked>
</label>
Weekly Newsletter
</li>
<label class="item item-input item-stacked-label">
<input data-ng-modal="user.productNumber" data-ng-pattern="/^[a-zA-Z0-9-]{8}$/" type="text" placeholder="Product Number" required>
</label>
<button data-ng-click="createAccountClick()" data-ng-disabled="signUpForm.$invalid" class="button button-full button-positive">
Create Account
</button>
</div>
</form>
</ion-content>
</ion-view>
This is because you mispelled data-ng-model with data-ng-modal.
Simply change:
data-ng-modal="user.productNumber"
with
data-ng-model="user.productNumber"

Resources