form state changing to $invalid after submit - angularjs

I don't understand and have done lot of research to no avail. After I hit submit button, my form state changes back to $valid = false. This causes the input controls to go RED. How do I fix this. Here is my code.
<form class="form-horizontal" role="form" name="testMessageForm" novalidate>
<div class="form-group"
data-ng-class="{'has-error':testMessageForm.PhoneNumber.$invalid && testMessageForm.PhoneNumber.$dirty}">
<label class="col-sm-3 control-label" for="Phone">Cell Number</label>
<div class="col-sm-9">
<input required
placeholder="nnn-nnn-nnnn"
ng-pattern="/^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$/"
minlength="10"
maxlength="14"
data-ng-model="reminderTest.PhoneNumber"
id="PhoneNumber"
name="PhoneNumber"
class="form-control" />
<span class="help-block has-error">
<span ng-show="testMessageForm.PhoneNumber.$error.required">
required
</span>
<span ng-show="testMessageForm.PhoneNumber.$error.minlength">
At least 10 digits.
</span>
<span ng-show="testMessageForm.PhoneNumber.$error.maxlength">
Why so many characters. Double check.
</span>
<span ng-show="testMessageForm.PhoneNumber.$error.pattern">
Wrong Phone number pattern.
</span>
</span>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="Email"></label>
<div class="col-sm-9">
<button class=" btn btn-success"
type="submit" value="Test"
data-ng-disabled="testMessageForm.$invalid"
data-ng-click="submitTestReminderForm()">
Test Reminder Now
</button>
</div>
</div>
<div data-ng-show="loading == true">
<div class="spinner">
Loading...
</div>
</div>
<div class="alert alert-success" data-ng-show="successMsg.length > 0">
×
<strong>Great! </strong> {{successMsg}}
</div>
<div class="alert alert-danger" data-ng-show="errorMsg.length > 0">
×
<strong>oops! </strong> {{errorMsg}}
</div>
</form>
Controller code.
$scope.submitTestReminderForm = function () {
reminderService.insertTestReminder($scope.reminderTest).then(onReminderTestComplete, onReminderErrorTest);
};
Service code
var insertTestReminder = function (reminderTest) {
return $http.post("/api/ReminderTest", reminderTest);
};

Related

I have created a form with angularjs validation but its not showing any errors

I am trying to create a form that changes tabs of login and signup that is working fine but the validations that i have used in the code are not working . I have created the controller but still the code is not working. Please have a look at the code and suggest the problem and solution for this problem.
<body ng-app="myApp" ng-controller="panelController as panel">
<div class="row no-gutters">
<div class="col">
<div class="container-fluid">
<nav class="navbar navbar-expand-lg">
<a class="navbar-brand mx-auto" href="#"><img class="brand-image"
src="images/recruitment.png">JobsFinder</a>
</nav>
<div class="logo-text">
<h3>The easiest way to get you new job</h3>
<p>We offer 12000 jobs vacation right now</p>
</div>
</div>
</div>
<div class="col">
<section>
<div class="form animated flipInX">
<ul class="tab-group">
<li class="tab" ng-class="{active: panel.isSelected(2)}"><a
href ng-click="panel.selectTab(2)">Log In</a></li>
<li class="tab" ng-class="{active: panel.isSelected(1)}"><a
href class="test" ng-click="panel.selectTab(1)">Sign Up</a></li>
</ul>
<div class="tab-content">
<div id="signup" ng-show="panel.isSelected(1)" >
<h3>Sign Up for Free</h3>
<form name="signupForm" class="signupForm" novalidate>
<div class="top-row">
<div class="field-wrap">
<input type="text" required autocomplete="off"
placeholder="First name" ng-model="fname">
<span style="color:red" ng-
show="signupForm.fname.$dirty && signupForm.fname.$invalid">
<span ng-
show="signupForm.fname.$error.required">First name is required.</span>
</span>
</div>
<div class="field-wrap">
<input type="text"required autocomplete="off"
placeholder="Last name" ng-model="lname">
<span style="color:red" ng-
show="signupForm.lname.$dirty && signupForm.lname.$invalid">
<span ng-
show="signupForm.lname.$error.required">Last name is required.</span>
</span>
</div>
</div>
<div class="field-wrap">
<input type="email" required autocomplete="off"
placeholder="Email" ng-model="signupemail">
<span style="color: indianred" ng-
show="signupForm.signupemail.$dirty && signupForm.email.$invalid">
<span ng-
show="signupForm.signupemail.$error.required">Email is required</span>
<span ng-
show="signupForm.signupemail.$error.email">Invalid email</span>
</span>
</div>
<div class="field-wrap">
<input type="password"required autocomplete="off"
placeholder="Password" ng-model="signpassword">
<span style="color: indianred" ng-
show="signupForm.signpassword.$dirty && signupForm.signpassword.$invalid">
<span ng-
show="signupForm.signpassword.$error.required">Password is required</span>
</span>
</div>
<div class="field-wrap">
<input type="password"required autocomplete="off"
placeholder="Confirm password" ng-model="cpass">
<span style="color: indianred" ng-
show="signupForm.cpass.$dirty && signupForm.cpass.$invalid">
<span ng-
show="signupForm.cpass.$error.required">Password is required</span>
</span>
</div>
<button type="submit" class="button button-block" ng-
click="submit()" ng-disabled="signupForm.signpassword.$dirty &&
signupForm.signpassword.$invalid || signupForm.signupemail.$dirty &&
signupForm.signupemail.$invalid || signupForm.cpass.$dirty &&
signupForm.cpass.$invalid"/>Get Started</button>
</form>
</div>
<div id="login" ng-show="panel.isSelected(2)" >
<h3>Welcome Back!</h3>
<form class="loginForm" name="loginForm" novalidate>
<div class="field-wrap">
<input type="email"required autocomplete="off"
placeholder="Email" ng-model="loginemail">
<span style="color: indianred" ng-
show="loginForm.loginemail.$dirty && loginForm.email.$invalid &&
loginForm.loginemail.$touched">
<span ng-
show="loginForm.loginemail.$error.required">Email is required</span>
<span ng-
show="loginForm.loginemail.$error.email">Invalid email</span>
</span>
</div>
<div class="field-wrap">
<input type="password"required autocomplete="off"
placeholder="Password" ng-model="logpassword">
<span style="color: indianred" ng-
show="loginForm.logpassword.$dirty && loginForm.logpassword.$invalid">
<span ng-
show="loginForm.logpassword.$error.required">Password is required</span>
</span>
</div>
<p class="forgot">Forgot Password?</p>
<button class="button button-block" type="submit" ng-
click="submit()" ng-disabled="loginForm.logpassword.$dirty &&
loginForm.logpassword.$invalid || loginForm.loginemail.$dirty &&
loginForm.loginemail.$invalid"/>Log In</button>
</form>
</div>
</div>
</div>
</section>
</div>
</div>
<footer>
<div class="text-center">
<p>Copyright © JobFinder 2017. Developed by Avneet Virk &
Shubham Dobriyal</p>
</div>
</footer>
<script src="https://unpkg.com/ionicons#4.2.2/dist/ionicons.js"></script>
<script src="js/panelController.js"></script>
</body>
You haven't specified any name for your inputs. So they're not correctly registered, underthat (absent) name in the form, so signupForm.fname (for example), is undefied.
Inputs must have a name:
<input name="fname" type="text" ... >

Ng-click validation is not working

I am beginer in angular js. I am validating a form with some input feild and form is posting on ng-click but validation is not working, validation message are displaying for a white then disappear i have to submit the form after validating. form ng-click should not be called untill the form is valid please help me . Thanks in advance.
<form name="teamForm" novalidate ng-submit="submit(teamForm)" class="formfields">
<div class="col-md-12">
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<label for="lname">First Name:</label>
<input type="text" name="firstname"
ng-model="FirstName" class="form-control custom-form-control"
placeholder="First Name" required="required">
<span class="text-danger"
ng-show="(teamForm.firstname.$dirty || submitted) && teamForm.firstname.$error.required">Required</span>
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="form-group">
<label for="lname">Last Name:</label>
<input type="text" name="lastname"
ng-model="LastName" class="form-control custom-form-control"
placeholder="Last Name" required="required">
<span class="text-danger"
ng-show="(teamForm.lastname.$dirty || submitted) && teamForm.lastname.$error.required">Required</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<label for="email">Email:</label>
<input type="text" name="email"
ng-model="Email" class="form-control custom-form-control"
ng-pattern="/^[^\s#]+#[^\s#]+\.[^\s#]{2,}$/"
placeholder="Email" required="required">
<span class="text-danger"
ng-show="(teamForm.email.$dirty || submitted) && teamForm.email.$error.required">Required</span>
<span class="text-danger"
ng-show="teamForm.email.$dirty &&teamForm.email.$error.pattern">Please Enter Valid Email</span>
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="form-group">
<label>Phone Number:</label>
<div class="clearfix"></div>
<input type="text" name="phone"
ng-model="Phone" class="form-control custom-form-control"
placeholder="XXXXXXXXXX" required="required">
<span class="text-danger"
ng-show="(teamForm.phone.$dirty || submitted) && teamForm.phone.$error.required">Required</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 col-sm-12">
<div class="form-group">
<label>Message:</label>
<textarea class="form-control rounded-0" rows="5"
name="comment" placeholder="Message"
ng-model="Comment" required="required"></textarea>
<span class="text-danger"
ng-show="(teamForm.comment.$dirty || submitted) && teamForm.comment.$error.required">Required</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 col-sm-12">
<div class="form-group">
<label>Upload Resume:</label>
<!--<input type="file" name="ResumePath" id="filehandler" />-->
<input type="file" id="file1" name="file" class="filelabel sr-only" multiple ng-files="getTheFiles($files)" onchange="Checkfiles($(this))" />
<!-- <input type="file" name="file" onchange="angular.element(this).scope().uploadFile(this.files)"/> -->
<label for="file1" class="form-control">
<span><i class="fa fa-file"></i> Drag file here or choose file</span>
</label>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<div vc-recaptcha key="'6Lc860IUAAAAAAyWI9WD8EV4eITu4ODdhuYHdzi8'"
class="grecaptcha" ng-model="respone1"></div>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<button type="button" id="btnSubmit"
ng-click="uploadFiles()" value="Upload"
class="btn btn-green center-block pull-left">
<i class="fa fa-send"></i>{{btnText}}</button>
</div>
</div>
<div class="form-group text-center">
<h5 class="text-success" style="font-weight:bold">{{messagesuccess}}</h5>
<h5 class="text-danger" style="font-weight:bold">{{messageerror}}</h5>
</div>
</div>
</form>
ng-click (or it's vanilla cousin, onclick) do not check form validation. The function for submission needs to be defined at the form level, and then you specify which button acts as the submit button in order to get form behavior.
I see you already have a submit function defined. I assume you want to change that to uploadFiles. And if you want the form to conduct validation, remove the novalidation attribute.
<form name="teamForm" ng-submit="uploadFiles()" class="formfields">
then, for the button you would specify it is the submission button and remove the ng-click.
<button type="submit" id="btnSubmit"
value="Upload"
class="btn btn-green center-block pull-left">
<i class="fa fa-send"></i>{{btnText}}</button>

Angular 2 Parsley Prevent Form Submission When Input is Invalid

<div>
<form class="form-horizontal form-label-left parsleyjs" method="post" data-parsley-priority-enabled="false"
(ngSubmit)="submitForm()">
<fieldset>
<div class="form-group row">
<label class="form-control-label col-md-3 col-xs-12" for="task-id">
ID
<span class="help-block">
Number Only
</span>
</label>
<div class="col-md-9 col-xs-12">
<input type="text" id="task-id" name="task-id" class="form-control"
data-parsley-type="number"
required="required">
</div>
</div>
</fieldset>
<div class="form-actions">
<button type="submit" id="task-detail-submit-button" class="btn btn-danger btn-rounded pull-xs-right">Submit</button>
</div>
</form>
</div>
How can the Parsley stop invoking "submitForm()" during form submission when the input is invalid. In this case, "task-id" is not a number.
hope this code give you a idea
<form class="form-horizontal form-label-left parsleyjs" name="formname" method="post" data-parsley-priority-enabled="false"
(ngSubmit)="formname.valid && formname.submitForm()">
This code is not tested if it does not work sorry for it

Angularjs IF - ELSE condtion on checkboxes

i am new to Angular js i want to ask i have a case in which i have to return "YES" if the checkbox is checked or true and want to return "NO" when the value is false or unchecked.
actually my data is storing in databse in "Tinyint" 1 or 0 form i want to do when 1 occurs it shows YES any solution for this Any help will be appreciated.
Here is my html
<form class="form-horizontal">
<div class="form-group">
<label class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" ng-model="rec.Name" placeholder="Name" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<div class="checkbox c-checkbox">
<label>
<input type="checkbox" value="" ng-model="rec.isSpecial">
<span class="fa fa-check"></span>is Special</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<div class="checkbox c-checkbox">
<label>
<input type="checkbox" ng-model="rec.isMultiple">
<span class="fa fa-check"></span>Is Multiple</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="button" ng-click="add(rec)" class="btn btn-sm btn-primary">ADD</button>
</div>
</div>
</form>
i need to target checkbox ismultiple and isspecial .
You can use ng-true-value of ng-false-value like this:
<form class="form-horizontal">
<div class="form-group">
<label class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" ng-model="rec.Name" placeholder="Name" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<div class="checkbox c-checkbox">
<label>
<input type="checkbox" ng-model="rec.isSpecial" ng-true-value = "YES" ng-false-value="NO">
<span class="fa fa-check"></span>is Special</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<div class="checkbox c-checkbox">
<label>
<input type="checkbox" ng-model="rec.isMultiple" ng-true-value = "YES" ng-false-value="NO">
<span class="fa fa-check"></span>Is Multiple</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="button" ng-click="add(rec)" class="btn btn-sm btn-primary">ADD</button>
</div>
</div>
</form>

Jhipster - issue with custom authentication view

I am trying to modify the Jhipster template by doing the following:
If the user is not authenticated , the authentication form is showing (instead of showing a link to the authentication page as it is set by default). I m showing it in the main.html view
<div class="row">
<div class="col-md-4">
<span class="hipster img-responsive img-rounded"></span>
</div>
<div class="col-md-8">
<h1 translate="main.title">Welcome, Java Hipster!</h1>
<p class="lead" translate="main.subtitle">This is your homepage</p>
<div ng-switch="authenticated">
<div class="alert alert-success" ng-switch-when="true"
translate="main.logged.message" translate-values="{username: '{{account.login}}'}">
You are logged in as user "Admin".
</div>
<div ng-switch-when="false">
<div class="row">
<div class="col-md-4">
<h1 translate="login.title">Authentication</h1>
<div class="alert alert-danger" ng-show="authenticationError" translate="login.messages.error.authentication">
<strong>Authentication failed!</strong> Please check your credentials and try again.
</div>
<form class="form" role="form" ng-submit="login()">
<div class="form-group">
<label for="username" translate="global.form.username">Login</label>
<input type="text" class="form-control" id="username" placeholder="{{'global.form.username.placeholder' | translate}}" ng-model="username">
</div>
<div class="form-group">
<label for="password" translate="login.form.password">Password</label>
<input type="password" class="form-control" id="password" placeholder="{{'login.form.password.placeholder' | translate}}"
ng-model="password">
</div>
<div class="form-group">
<label for="rememberMe">
{{"login.form.rememberme" | translate}}
<input type="checkbox" class="checkbox inline_class" id="rememberMe"
ng-model="rememberMe" checked>
</label>
</div>
<button type="submit" class="btn btn-primary" translate="login.form.button">Authenticate</button>
</form>
<p></p>
<div class="alert alert-warning" translate="global.messages.info.register">
You don't have an account yet? Register a new account
</div>
</div>
</div>
</div>
</div>
</div>
</div>
and modified apps.js :
.otherwise({
templateUrl: 'views/main.html',
controller: 'LoginController',
access: {
authorizedRoles: [USER_ROLES.all]
}
});
It works if I m not using the ng-switch-when="false" statement.
If I m using this statement, the value of $scope.password and $scope.login are "undefined" in the LoginController.
Of course this statement is mandatory if I want to show the authentication form only if the user is not authenticated.
I can't understand why.
If you can help me on this, it would be great.
Thanks.
It's a common gotcha with ng-switch and other directives that create sub-scopes. This should work:
<div class="row">
<div class="col-md-4">
<span class="hipster img-responsive img-rounded"></span>
</div>
<div class="col-md-8">
<h1 translate="main.title">Welcome, Java Hipster!</h1>
<p class="lead" translate="main.subtitle">This is your homepage</p>
<div ng-switch="authenticated">
<div class="alert alert-success" ng-switch-when="true"
translate="main.logged.message" translate-values="{username: '{{account.login}}'}">
You are logged in as user "Admin".
</div>
<div ng-switch-when="false">
<div class="row">
<div class="col-md-4">
<h1 translate="login.title">Authentication</h1>
<div class="alert alert-danger" ng-show="authenticationError" translate="login.messages.error.authentication">
<strong>Authentication failed!</strong> Please check your credentials and try again.
</div>
<form class="form" role="form" ng-submit="login()">
<div class="form-group">
<label for="username" translate="global.form.username">Login</label>
<input type="text" class="form-control" id="username" placeholder="{{'global.form.username.placeholder' | translate}}" ng-model="$parent.username">
</div>
<div class="form-group">
<label for="password" translate="login.form.password">Password</label>
<input type="password" class="form-control" id="password" placeholder="{{'login.form.password.placeholder' | translate}}"
ng-model="$parent.password">
</div>
<div class="form-group">
<label for="rememberMe">
{{"login.form.rememberme" | translate}}
<input type="checkbox" class="checkbox inline_class" id="rememberMe"
ng-model="$parent.rememberMe" checked>
</label>
</div>
<button type="submit" class="btn btn-primary" translate="login.form.button">Authenticate</button>
</form>
<p></p>
<div class="alert alert-warning" translate="global.messages.info.register">
You don't have an account yet? Register a new account
</div>
</div>
</div>
</div>
</div>
</div>
Instead of $parent you may bind values to an object, but then you will need to modify Jhipster's LoginController accordingly (and also make sure to update login.html or create a variant of the controller for the main screen only)

Resources