Angular 4 Reset status form - forms

I have a modal with a form, and some required fields.
When I open the modal I correctly see the fields empty, if I write something in the field required and then I cancel the words writed I correctly see the field as invalid.
The problem is that if I close and reopen the modal I want to see the empty fields like if it's the first time I open it but for some reason I see the previous invalid status also if I reset the form.
This is my modal after I close it, it seems that the status never be resetted:
<form #modelForm="ngForm">
<div class="modal-body" *ngIf="checkpoint">
<div class="row">
<div class="form-group label-floating">
<label class="control-label">{{'checkpoint.table.dialog.labels.name'
| translate }}<span class="star">*</span>
</label> <input class="form-control" id="name" name="name" required
[(ngModel)]="checkpoint.name" #name="ngModel" /><small
[hidden]="name.valid || name.pristine" class="text-danger">
{{'checkpoint.table.validations.required' | translate }}</small>
</div>
</div>
<div class="row">
<div class="form-group label-floating">
<label class="control-label">{{'checkpoint.table.dialog.labels.passStockAlert'
| translate }}</label> <input pattern="[0-9]*"
class="form-control" name="passStockAlert"
id="passStockAlert"
[(ngModel)]="checkpoint.passStockAlert"
#checkPoint="ngModel" /><small
[hidden]="checkPoint.valid || checkPoint.pristine"
class="text-danger">
{{'checkpoint.table.validations.invalid' | translate }}</small>
</div>
</div>
<div class="row">
<div class="category form-category">
<span class="star">*</span> {{ 'form.requiredfields' |
translate }}
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info btn-round"
(click)="save();resetForm(modelForm)" data-dismiss="modal"
[disabled]="modelForm.form.invalid || modelForm.form.pristine"
label="Save">{{'checkpoint.table.dialog.save' |
translate }}</button>
<button type="button" class="btn btn-danger btn-round"
(click)="resetForm(modelForm)" data-dismiss="modal">{{'checkpoint.table.dialog.close' |
translate }}</button>
</div>
</form>
And this is my reset form method:
resetForm(myForm: NgForm) {
myForm.form.reset();
}

I have successfully implemented this:
my-component.html
<form (ngSubmit)="resetFormFlags(myForm)" #myForm="ngForm">
<h4> title </h4>
<button type="submit"> btnText</button>
</form>
my-component.ts
resetFormFlags(myForm: any) {
myForm.reset();
}
Hope it helps!

Try like this below code :
<form name="modelForm" role="form" novalidate (ngSubmit)="resetForm(modelForm)" #modelForm="ngForm">
<div class="modal-body" *ngIf="checkpoint">
<div class="row">
<div class="form-group label-floating">
<label class="control-label">{{'checkpoint.table.dialog.labels.name'
| translate }}<span class="star">*</span>
</label>
<input class="form-control" id="name" name="name" required [(ngModel)]="checkpoint.name" />
<small [hidden]="!(modelForm.controls.name?.dirty && modelForm.controls.name?.invalid)" class="text-danger">
{{'checkpoint.table.validations.required' | translate }}
</small>
</div>
<button class="btn btn-primary" [disabled]="modelForm.form.invalid" type="submit">{{'checkpoint.table.dialog.save' | translate }}</button>
<button class="btn btn-white" type="button" (click)="resetForm(modelForm)"> {{'checkpoint.table.dialog.close' | translate }}</button>
</div>
</div>
</form>
componen.ts
resetForm(editForm: NgForm) {
editForm.reset();
}

Related

How to set a Razor Html.BeginForm within another Html.BeginForm

Description
I want to have two buttons to edit and delete an identity role. The edit button works but if I want to delete a role the site is reloading but nothing happens.
The post function will not be called. Is there another way to have a post form in another post form?
Code
<div class="row mb-3">
<div class="col-12">
<div class="card">
<div class="card-body">
<h4 class="card-title">Rollennamen bearbeiten</h4>
#using(Html.BeginForm("EditRole", "Administration", FormMethod.Post)) {
<div class="form-group row">
<label asp-for="EditRole.RoleName" class="col-sm-12 col-md-3"></label>
<div class="col-sm-12 col-md-9">
<input asp-for="EditRole.Id" type="hidden" value="#roles.Id" />
<input asp-for="EditRole.RoleName" class="form-control" value="#roles.Name" />
<span asp-validation-for="EditRole.RoleName" class="text-danger"></span>
</div>
</div>
<div class="d-flex flex-row-reverse">
<button type="submit" class="btn btn-success">Rolle bearbeiten</button>
#using(Html.BeginForm("DeleteRole", "Administration", FormMethod.Post)) {
<input name="id" type="hidden" value="#roles.Id" />
<button type="submit" class="btn btn-danger mr-2">Rolle löschen</button>
}
</div>
}
</div>
</div>
</div>
</div>
You could handle multiple submit in one form use asp-action like
#using (Html.BeginForm(FormMethod.Post))
{
<div class="d-flex flex-row-reverse">
<input type="submit" value="Save" class="btn btn-success" asp-controller="Administration" asp-action="EditRole" />
<input type="submit" value="Delete" class="btn btn-danger" asp-controller="Administration" asp-action="DeleteRole" />
</div>
}

Datepicker - invalid date & class style

Following your sample code
<div class="col-6">
<form class="form-inline">
<div class="form-group">
<div class="input-group">
<input class="form-control form-control-sm" placeholder="yyyy-mm-dd"
name="d2" #c2="ngModel" [(ngModel)]="model2" ngbDatepicker #d2="ngbDatepicker">
<div class="input-group-append">
<button class="btn btn-sm btn-outline-secondary dropdown-toggle" (click)="d2.toggle()" type="button">
</button>
</div>
</div>
</div>
</form>
<hr/>
<button class="btn btn-sm btn-outline-primary" (click)="model2 = null">Select Today</button>
<hr/>
<pre>M: {{ model2 | json }}</pre>
<pre>S: {{ c2.status }}</pre>
<pre>B: {{ c2.status==INVALID }}</pre>
</div>
works fine. But I want to set an error-class for the whole input-group, something like
<div class="input-group" [ngClass]="{ 'has-error':isInValid }">
But the evalutation of the c2.status never evaltuates to true/false dynamically. It remains unchanged ==> How to make a proper compare to set the class dynamically?
Thx to JB Nizet I figured out the following proper syntax:
<div class="input-group" [ngClass]="{ 'has-error': c2.invalid }">

How to access to RESTController through a controller?

I am using Spring.
The method in my RESTController is this,
http://localhost:8080/delivery-api/training/submit. It is able to save hard code values into database.
This is part of my html form, it can be open up using this URL: http://localhost:8080/delivery-web/training/apply.
<form th:object="${applyForm}" class="form-horizontal" role="form" method="post" action="http://localhost:8080/delivery-api/training/submit" >
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1" > Country </label>
<div class="col-sm-9">
<input type="text" id="form-field-1" placeholder="Country"
class="col-xs-10 col-sm-7" th:field="*{country}" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1"> Fee </label>
<div class="col-sm-9">
<input type="text" id="form-field-1" th:field="*{Fee}" class="col-xs-10 col-sm-7" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1"> Start Date </label>
<div class="col-sm-9">
<input type="date" id="form-field-1" th:field="*{startDate}"
class="col-xs-10 col-sm-7" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1">End Date </label>
<div class="col-sm-9">
<input type="date" id="form-field-1" th:field="*{endDate}"
class="col-xs-10 col-sm-7" />
</div>
</div>
<br> <br>
<button class="btn btn-info" type="submit">
<i class="ace-icon fa fa-check bigger-110"></i> Submit
</button>
</div>
</div>
</form>
When I click on submit, it can be directed to http://localhost:8080/delivery-api/training/submit and insert the hard coded values into database, but it did not direct to my application-form.html page after that.
My controller
#PostMapping(value = "/apply")
public String apply(#Valid #ModelAttribute("applyForm") DeliveryApplicationForm applyForm, BindingResult errors, Model model) {
model.addAttribute("applyForm", applyForm);
return VIEW_PATH + "application-form";
}
I would like to render html page and also pass data from my form to RESTController method through a controller after I click on the submit button in my html page.
May I know how I can do it?

Angular error:Form submission canceled because the form is not connected

What I am trying to do is -
I am trying to add a form to the existing form but the data not getting stored
what the error coming up -
in the console its showing form connection is missing.. how can I connect it with following code I have?
The code behind the click is something like this:
<button type="submit" class="btn btn-default" routerLink="../viewemployee" [disabled]="empForm.invalid">Add Employee</button>
please refer the link below for the code.. need help to move on from this to solve other tasks.
If it is required to post the code here as well, I will do.Please answer and respond.
Thanx in advance.
How to connect the form in angular routing
createemployee.component.html
<h2>Add Employee:</h2>
<form class="form-horizontal" #empForm="ngForm">
<div class="form-group">
<label class="control-label col-sm-2" for="name">Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" minlength="4" maxlength="10" pattern="^[A-Za-z0-9]*[A-Za-z0-9][A-Za-z0-9]\S*$" [(ngModel)]="model.name" placeholder="Enter Your Name"
#name="ngModel" required/>
<div *ngIf="name.invalid && (name.dirty || name.touched)" class="alert alert-danger">
<div *ngIf="name.errors.required">
Name is required.
</div>
<div *ngIf="name.errors.pattern">
No Spaces
</div>
<div *ngIf="name.errors.minlength">
Name must be at least 4 characters long.
</div>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="position">Position:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="position" minlength="4" maxlength="10" pattern="^[a-z]*$" [(ngModel)]="model.position" placeholder="Enter your position"
#position="ngModel" required />
<div *ngIf="position.invalid && (position.dirty || position.touched)" class="alert alert-danger">
<div *ngIf="position.errors.required">
Position is required.
</div>
<div *ngIf="position.errors.pattern">
Only Alphabets are must be entered
</div>
<div *ngIf="position.errors.minlength">
Position must be at least 4 characters long.
</div>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="salary">Salary:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="salary" pattern="[0-9]*"
minlength="5" maxlength="7" [(ngModel)]="model.salary" placeholder="Enter Salary" #salary="ngModel" required />
<div *ngIf="salary.invalid && (salary.dirty || salary.touched)" class="alert alert-danger">
<div *ngIf="salary.errors.required">
Salary is required.
</div>
<div *ngIf="salary.errors.minlength">
Salary must be in 5 numbers.
</div>
<div *ngIf="salary.errors.maxlength">
Salary must not be exceeded morethan 7 numbers.
</div>
<div *ngIf="salary.errors?.pattern">Only numebers should be typed
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" routerLink="../viewemployee" [disabled]="empForm.invalid">Add Employee</button>
<button type="button" class="btn btn-default" routerLink="../home">Cancel</button>
</div>
</div>
</form>
You could change your component as follows:
Add event handler to the form:
<form (ngSubmit)="continue()">
Handle the routing in code:
continue() {
...
this.router.navigateByUrl("../viewemployee");
}
You need to inject the router:
constructor(private router: Router) {}

submit a form pressing ENTER

I have a submit form, when I click on the button "log in" it works, $_POST['login'] contains something, but when I press ENTER key it is empty.
form
<form class="form-vertical login-form" action="conexio.php" method="post">
<h3 class="form-title">Login to your account</h3>
<div class="alert alert-error hide">
<button class="close" data-dismiss="alert"></button>
<span>Enter any username and password.</span>
</div>
<div class="control-group">
<!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
<label class="control-label visible-ie8 visible-ie9">Username</label>
<div class="controls">
<div class="input-icon left">
<i class="icon-user"></i>
<input class="m-wrap placeholder-no-fix" type="text" autocomplete="off" placeholder="Username" name="username"/>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label visible-ie8 visible-ie9">Password</label>
<div class="controls">
<div class="input-icon left">
<i class="icon-lock"></i>
<input class="m-wrap placeholder-no-fix" type="password" autocomplete="off" placeholder="Password" name="password"/>
</div>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn blue pull-right" name="login">
Login <i class="m-icon-swapright m-icon-white"></i>
</button>
</div>
</form>
Try replacing <button> with <input type='submit'>.