Angular JS: ng-disabled not disabling button after submit - angularjs

I have a form like below-
<form class="form-horizontal register-form" name="register" ng-submit="registerUser();" novalidate>
<div class="form-group">
<label for="email" class="cols-sm-2 control-label">Your Email</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>
<input type="email" class="form-control" ng-model="user.email" name="email" id="mail" placeholder="Enter your Email" required />
<i ng-show="register.email.$dirty && register.email.$invalid && !user.submit" class="error-icon glyphicon glyphicon-remove"></i>
<i ng-show="register.email.$valid" class="valid-icon glyphicon glyphicon-ok"></i>
</div>
<span class="error" ng-show="register.email.$dirty && !user.submit && register.email.$invalid">Email field is required.</span>
<span class="error" ng-show="register.email.$dirty && register.email.$error.email && register.email.$invalid">Email you entered is invalid.</span>
</div>
<div class="form-group">
<label for="password" class="cols-sm-2 control-label">Password</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock fa-lg" aria-hidden="true"></i></span>
<input type="password" class="form-control" ng-model="user.login_password" name="login_password" id="login_password" placeholder="Enter your Password" required />
<i ng-show="register.login_password.$dirty && register.login_password.$invalid && !user.submit" class="error-icon glyphicon glyphicon-remove" ></i>
<i ng-show="register.login_password.$valid" class="valid-icon glyphicon glyphicon-ok"></i>
</div>
<span class="error" ng-show="register.login_password.$dirty && register.login_password.$error.required && !user.submit">Password field is required.</span>
</div>
<div class="form-group">
<label for="confirm" class="cols-sm-2 control-label">Confirm Password</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock fa-lg" aria-hidden="true"></i></span>
<input type="password" ng-model="user.confirm_password" class="form-control" name="confirm_password" id="confirm_password" placeholder="Confirm your Password" required />
<i ng-show="user.login_password !== user.confirm_password" class="error-icon glyphicon glyphicon-remove"></i>
<i ng-show="register.confirm_password.$valid && user.login_password == user.confirm_password" class="valid-icon glyphicon glyphicon-ok"></i>
</div>
<span class="error" ng-show="register.confirm_password.$dirty && register.confirm_password.$error.required && !user.submit">Confirm password field is required.</span>
<span class="error" ng-show="register.confirm_password.$dirty && user.login_password !== user.confirm_password">Confirm password does not match.</span>
</div>
<div class="form-group ">
{{register.$invalid }} {{ disableButton}} {{register.$invalid == disableButton}}
<button type="submit" class="btn btn-primary login-button" ng-disabled="register.$invalid" id="button-register" data-loading-text="<i class='fa fa-circle-o-notch fa-spin'></i> Registering..."><i class="fa fa-key" aria-hidden="true"></i> Register</button>
</div>
</form>
Here is my conroller-
$scope.registerUser = function() {
$scope.user.created_at = Math.round((new Date()).getTime() / 1000);
$scope.user.updated_at = Math.round((new Date()).getTime() / 1000);
$scope.user.action = 'add';
$("#button-register").button('loading');
$http({
method: 'POST',
url: 'api/user.php/users/',
data: $scope.user ,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data, status, headers, config) {
$("#button-register").button('reset');
$scope.successMsg = 'Congratulation! you are registered successfully.';
$scope.user={};
$scope.user.submit=true;
$scope.disableButton=true;
})
} // End of register function
Everything works fine, but once I submit the form and clear the input fields the submit button is enabled. I tried to print the value of {{register.$invalid }} after submit which is true(ideally if it is true button should be disabled). but still the button is enabled. Can someone point me where I am doing wrong?

I think it has something do with this line :
$("#button-register").button('reset');
Maybe ,it's changing the submit into a whole new button with type reset.And so there is ng-disabled tag after the execution that statement.Try removing this statement and to see if the disable works without it.
Also its a jquery,which can always create a problem when it comes to combining it with angularjs.If that is the case try putting it in
$scope.$apply(function(){
$("#button-register").button('reset');
}

Related

AngularJS validation Error after Submitting and data clearing

I have implemented validation system in forms and it working fine.
Here is my form:
<form name='addContactForm'>
<div class="form-group">
<label for="userid">USER ID :</label><br>
<input ng-model="formModel.userid" class="form-control" name="userid" restrict-input="{type: 'digitsOnly'}" required>
<span style="color:red" ng-show="addContactForm.userid.$dirty && addContactForm.userid.$invalid">
<span ng-show="addContactForm.userid.$error.required"> User is required.</span>
<span ng-show="addContactForm.userid.$error.number">Invalid userID</span>
</div>
<div class="form-group">
<label for="name">NAME :</label><br>
<input ng-model="formModel.name" class="form-control" name="name" required/>
<span style="color:red" ng-show="addContactForm.name.$dirty && addContactForm.name.$invalid">
<span ng-show="addContactForm.name.$error.required"> Name is required.</span>
<span ng-show="addContactForm.name.$error.lettersOnly">Invalid Name</span>
</span>
</div>
<div class="form-group">
<label for="email">Email :</label> <br>
<input type="email" name="email" class="form-control" ng-model="formModel.email" ng-pattern="/[\w\d\.\_]+\#[\w\d]+\.[\w]{3}/" required>
<span style="color:red" ng-show="addContactForm.email.$dirty && addContactForm.email.$invalid">
<span ng-show="addContactForm.email.$error.required">Email is required.</span>
<span ng-show="addContactForm.email.$error.pattern">Invalid email address.</span>
</span>
</div>
<div class="form-group">
<label for="phone">Phone :</label> <br>
<input ng-model="formModel.phone" class="form-control" name="phone" restrict-input="{type: 'digitsOnly'}" required>
<span style="color:red" ng-show="addContactForm.phone.$dirty && addContactForm.phone.$invalid">
<span ng-show="addContactForm.phone.$error.required"> Phone Number is required.</span>
</span>
</div>
<div class="row justify-content-center">
<button class="btn btn-primary" ng-disabled="addContactForm.$invalid" ng-click="PassDataToDisplyThroughUrl()">Submit</button> <br>
</div>
</form>
</div>
Note that, above things are working fine.
my controller is below:
app.controller('formCtrl', ['$scope', '$location', '$http',
function($scope, $location, $http) {
$scope.formModel = {};
$scope.PassDataToDisplyThroughUrl = function() {
var url = 'display/' + $scope.formModel.userid + '/' + $scope.formModel.name
+ '/' + $scope.formModel.email + '/' + $scope.formModel.phone;
$location.path(url);
$http.post('http://127.0.0.1:8000/api/v1/contact/', $scope.formModel)
.then(function(response){
$scope.successCallBack = 'You have successfully saved your contact';
}, function(response){
$scope.errorCallBack = 'Opps! Unable to save your data, please check your network';
});
$scope.formModel = {};
$scope.addContactForm.$setPristine();
};
}]);
After i clicking on submit button -- it clear the form and send to the server. but the problem is, it shows me the validation error later
Can anyone fix me this issue?

AngularJS UI-Router edit page title

I'm working on an AngularJs application with symfony 2.8 backoffice. I want to edit the contact page title. This is the code of the contact page :
<div ng-controller="contactFormCtrl">
<fieldset>
<h1 translate="front.CONTACT">Contact</h1>
<h2 ng-bind-html="post.content"></h2>
<form name="contactForm" id="contactForm" class="form" data-ng-submit="submitForm(contactForm)">
<div class="form-group" ng-class="{'has-error':contactForm.firstName.$dirty && contactForm.firstName.$invalid, 'has-success':contactForm.firstName.$valid}">
<label for="contactFirstName" class="control-label">
<span translate="content.list.fields.FIRSTNAME">First Name</span>
<span class="symbol required"></span>
</label>
<span class="input-icon">
<input name="firstName" id="contactFirstName" ng-model="contact.firstName" type="text" class="form-control" name="firstName" placeholder="[[ 'content.list.fields.FIRSTNAME' | translate ]]" ng-required="true">
<i class="fa fa-user"></i>
</span>
<span class="error text-small block" ng-if="contactForm.firstName.$dirty && contactForm.firstName.$error.required" translate="content.form.messages.FIRSTNAMEREQUIRED">FirstName is required.</span>
</div>
<div class="form-group" ng-class="{'has-error':contactForm.lastName.$dirty && contactForm.lastName.$invalid, 'has-success':contactForm.lastName.$valid}">
<label for="contactLastName" class="control-label">
<span translate="content.list.fields.LASTNAME">Last Name</span>
<span class="symbol required"></span>
</label>
<span class="input-icon">
<input name="lastName" id="contactLastName" ng-model="contact.lastName" type="text" class="form-control" name="lastName" placeholder="[[ 'content.list.fields.LASTNAME' | translate ]]" ng-required="true">
<i class="fa fa-user-o"></i>
</span>
<span class="error text-small block" ng-if="contactForm.lastName.$dirty && contactForm.lastName.$error.required" translate="content.form.messages.LASTNAMEREQUIRED">LastName is required.</span>
</div>
<div class="form-group" ng-class="{'has-error':contactForm.contactEmail.$dirty && contactForm.contactEmail.$invalid, 'has-success':contactForm.contactEmail.$valid}">
<label for="contactEmail" class="control-label">
<span translate="content.list.fields.EMAIL">Email</span>
<span class="symbol required"></span>
</label>
<span class="input-icon">
<input name="contactEmail" id="contactEmail" ng-model="contact.email" type="email" class="form-control" name="email" placeholder="[[ 'content.list.fields.EMAIL' | translate ]]" ng-required="true">
<i class="fa fa-envelope-o"></i>
</span>
<span class="error text-small block" ng-if="contactForm.contactEmail.$dirty && contactForm.contactEmail.$error.required" translate="content.form.messages.EMAILREQUIRED">Email is required.</span>
</div>
<div class="form-group" ng-class="{'has-error':contactForm.contactSubject.$dirty && contactForm.contactSubject.$invalid, 'has-success':contactForm.contactSubject.$valid}">
<label for="contactSubject" class="control-label">
<span translate="content.list.fields.SUBJECT">Subject</span>
<span class="symbol required"></span>
</label>
<span class="input-icon">
<input name="contactSubject" id= "contactSubject" ng-model="contact.subject" type="text" class="form-control" name="contactSubject" placeholder="[[ 'content.list.fields.SUBJECT' | translate ]]" ng-required="true">
<i class="ti-marker-alt"></i>
</span>
<span class="error text-small block" ng-if="contactForm.contactSubject.$dirty && contactForm.contactSubject.$error.required" translate="content.form.messages.SUBJECTREQUIRED">Subject is required.</span>
</div>
<div class="form-group" ng-class="{'has-error':contactForm.contactMessage.$dirty && contactForm.contactMessage.$invalid, 'has-success':contactForm.contactMessage.$valid}">
<label for="contactMessage" class="control-label">
<span translate="content.list.fields.MESSAGE">Message</span>
<span class="symbol required"></span>
</label>
<span class="input-icon">
<textarea name="contactMessage" id="contactMessage" ng-model="contact.message" rows="10" class="form-control" name="contactMessage" placeholder="[[ 'content.list.fields.MESSAGE' | translate ]]" ng-required="true"></textarea>
<i class="fa fa-message-o"></i>
</span>
<span class="error text-small block" ng-if="contactForm.contactMessage.$dirty && contactForm.contactMessage.$error.required" translate="content.form.messages.CONTACTMESSAGEREQUIRED">Message is required.</span>
</div>
<button type="button" ng-click="submitForm(contactForm)" class="btn btn-primary pull-right" ng-disabled="disableSubmit"> <!-- ng-disabled="userForm.$invalid" -->
<i class="fa fa-spinner fa-spin" ng-if="disableSubmit"></i><i class="fa fa-envelope" ng-if="!disableSubmit"></i> <span translate="content.common.SEND">Send</span>
</button>
</form>
when i use the inspector i find this function :
<title ng-bind="pageTitle()" class="ng-binding">E-electricity</title>
A function called pageTitle() to automatically generate a title for each page. i want to edit E-electricity to E-electricity-contact.

Cannot hide the match messages in Angular password form

Hi, I got a problem I want to show the messages only when passwords match or not match but the messages appear all the time any suggestions? And I would like to not be able to click the button if the passwords are different!
#*<form name="form" ng-submit="vm.login()" role="form">*#
<form accept-charset="UTF-8" role="form" id="login-recordar" ng-submit="vm.login()">
#*New password field!*#
<div>
<div class="form-group input-group" ng-class="{ 'has-error': form.password.$dirty && form.password.$error.required }">
<span class="input-group-addon">
<i class="glyphicon glyphicon-lock"></i><label for="password1">New Password</label>
</span>
<input class="form-control" placeholder="Password" name="password" type="password" data-toggle="popover" data-trigger="focus" data-content="Minimum 8 characters long and should contain at least one (small- and capital letter and number)." id="password" value="" required="" autofocus="" onkeypress="capLock(event)" ng-model="mAddEditView.User.Password" ng-pattern="/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})/" />
</div>
<span ng-show="form.password.$dirty && form.password.$error.required" class="help-block">Password is required</span>
<span id="divMayus" style="visibility:hidden">Caps Lock is on!</span>
</div>
#*Confirm Password field!*#
<div>
<div class="form-group input-group" ng-class="{ 'has-error': form.password1.$dirty && form.password1.$error.required }">
<span class="input-group-addon">
<i class="glyphicon glyphicon-lock"></i><label for="password1">Confirm Password</label>
</span>
<input class="form-control" placeholder="Password" name="password1" type="password" data-toggle="popover" data-trigger="focus" data-content="The password should match." id="password1" value="" required="" onkeypress="capLock(event)" ng-model="mAddEditView.User.Password" ng-pattern="/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})/" />
</div>
<span ng-show="form.password1.$dirty && form.password1.$error.required" class="help-block">Password is required</span>
<span ng-show="mAddEditView.User.Password !== mAddEditView.User.Password1" class="help-block"><font style="color:red;">Password is not valid!</font></span>
#* code for checking if password match *#
<span ng-show="form.password1.$valid && mAddEditView.User.Password === mAddEditView.User.Password1"><font style="color:white;">Password Matched</font></span>
<span id="divMayus" style="visibility:hidden">Caps Lock is on!</span>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block" value="Login" ng-disabled="form.$invalid || vm.dataLoading || errors.mail.isExists || mAddEditView.User.Password !== mAddEditView.User.Password1">
Change Password
</button>
</div>
You can update your ng-show condition for message to
ng-show="form.password1.$valid && form.password1.$dirty && form.password.$dirty && mAddEditView.User.Password === mAddEditView.User.Password1"
So that it'll show only when user started editing both fields & user added valid password according to RegExp & then last condition to check if both are same.
*Required validation is necessary for making form invalid & avoiding user to submit it. So adding required attribute & for submit button form.$invalid will handle it itself
You should also check whether password1 is passing required validation or not.
ng-show="!form.password1.$error.required &&
form.password1.$valid &&
mAddEditView.User.Password === mAddEditView.User.Password1"

AngularJS validation not working

I've created a form with angularjs, but when I first load the page, the form starts out invalid and the entire form turns red. I've added "novalidate" to the form and "required" to an input, yet the whole form starts our invalid and red.
From this program when you are clicking submit button without entering any values validation will happen
<form name="product_form" class="form-horizontal" role="form" novalidate>
<div class="control-group" ng-class="{true: 'error'}[submitted && product_form.emailid.$invalid]">
<form-element label="Name" mod="product">
<input type="text" class="form-control" name="name" placeholder="name" ng-model="product.name" ng-disabled="product.id" required/>
<span class="help-block" ng-show="submitted && product_form.name.$error.required"> Please enter name </span>
</form-element>
<form-element label="Emailid" mod="product">
<input type="email" name="emailid" class="form-control" placeholder="emailid" ng-model="product.emailid" required>
<span ng-show="submitted && product_form.emailid.$error.required" class="help-block">Please enter emailid</span>
</form-element>
<form-element label="Password" mod="product">
<input type="password" name="password" class="form-control" placeholder="password" ng-model="product.password" required/>
<span ng-show="submitted && product_form.password.$error.required" class="help-block"> Please enter password </span>
</form-element>
</div>
<div class="space"></div>
<div class="space-4"></div>
<div class="modal-footer">
<a class="btn btn-sm" ng-click="cancel()" ><i class="ace-icon fa fa-reply"></i> Cancel </a>
<button ng-click="saveProduct(product);submitted=true"
ng-enabled="product_form.$invalid || enableUpdate"
class="btn btn-sm btn-primary ng-binding"
type="submit">
<i class="ace-icon fa fa-check"></i>Submit
</button>
</div>
</form>

How do i check if an email address exists when user signup (Angularjs and Firebase)

I am currently working on a project thats using firebase and angularjs and i want my application to check if an email already exists everytime a user try to create an account.
here is HTML markup:
<div class="well text-center">
<h1 class="text-info"><i class="fa fa-group socio_icon"></i></h1>
<h1>Blog title</h1>
<p class="lead">Slogan goes here</p>
<form name="signup_form" novalidate autocomplete="off">
<p>
<div class="input-group input-group-lg" ng-class="{'has-error': signup_form.fullnames.$dirty && signup_form.fullnames.$invalid , 'has-success': signup_form.fullnames.$valid}">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="text" class="form-control" placeholder="Enter full names" name="fullnames" ng-model="signup.form" ng-minlength=5 ng-maxlength=45 required />
<span class="input-group-addon valid" ng-show="signup_form.fullnames.$dirty && signup_form.fullnames.$valid"><i class="fa fa-check-circle text-success"></i></span>
<span class="input-group-addon valid" ng-show="signup_form.fullnames.$dirty && signup_form.fullnames.$invalid"><i class="fa fa-exclamation-circle text-warning"></i></span>
</div>
</p>
<p>
<div class="input-group input-group-lg" ng-class="{'has-error': signup_form.email.$dirty && signup_form.email.$invalid , 'has-success': signup_form.email.$valid}">
<span class="input-group-addon"><i class="fa fa-envelope-o"></i></span>
<input type="email" class="form-control" placeholder="Enter email address" name="email" ng-model="signup.email" ng-minlength=5 ng-maxlength=45 required />
<span class="input-group-addon valid" ng-show="signup_form.email.$dirty && signup_form.email.$valid"><i class="fa fa-check-circle text-success"></i></span>
<span class="input-group-addon valid" ng-show="signup_form.email.$dirty && signup_form.email.$invalid"><i class="fa fa-exclamation-circle text-warning"></i></span>
</p>
<p>
<div class="input-group input-group-lg" ng-class="{'has-error': signup_form.password1.$dirty && signup_form.password1.$invalid , 'has-success': signup_form.password1.$valid}">
<span class="input-group-addon"><i class="fa fa-lock"></i></i></span>
<input type="password" class="form-control" placeholder="Enter password" name="password1" ng-model="password1" ng-pattern="/(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z])/" ng-minlength=6 ng-maxlength=45 required />
<span class="input-group-addon valid" ng-show="signup_form.password1.$dirty && signup_form.password1.$valid"><i class="fa fa-check-circle text-success"></i></span>
<span class="input-group-addon valid" ng-show="signup_form.password1.$dirty && signup_form.password1.$invalid"><i class="fa fa-exclamation-circle text-warning"></i></span>
</div>
</p>
<p>
<div class="input-group input-group-lg" ng-class="{'has-error': signup_form.password2.$dirty && signup_form.password2.$invalid , 'has-success': signup_form.password2.$valid}">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input type="password" class="form-control" id="password2" name="password2" ng-model="password2" ng-minlength="8" ng-maxlength="20" ng-pattern="/(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z])/" required placeholder="Re-enter password"/>
<span class="input-group-addon valid" ng-show="signup_form.password2.$dirty && signup_form.password2.$valid"><i class="fa fa-check-circle text-success"></i></span>
<span class="input-group-addon valid" ng-show="signup_form.password2.$dirty && signup_form.password2.$invalid"><i class="fa fa-exclamation-circle text-warning"></i></span>
</div>
</p>
<p>
<button type="submit" ng-disabled="signup_form.$invalid" class="btn btn-primary btn-lg btn-block">Singup and enjoy!
</button>
</p>
</form>
</div>
and the js looks like this:
'use strict';
angular.module('myApp')
.controller('MainCtrl', function ($scope) {
var ref = new Firebase('https://myfirebase.firebaseio.com/');
});
The typical method for checking existence of records in Firebase is to use an index. Since firebase does not certain characters in references, you will need to escape the email first.
Since this is a common question I have a gist showing how to do this.
The "gist", to forgive a pun, is that you need to create an index whenever you create users, and then check that index:
var fb = new Firebase(URL);
function isDuplicateEmail(email, callback) {
fb.child('email_index/'+escapeEmail(email)).once('value', function(snap) {
callback( snap.val() !== null );
});
}
function updateUser(user_id, email) {
fb.child('user/'+user_id).set({email: email});
fb.child('email_index/'+escapeEmail(email)).set(user_id);
}
function escapeEmail(email) {
return (email || '').replace('.', ',');
}

Resources