I am using spring boot as back-end and angular 2 as front-end. My problem consist how to bind spring validators with angular 2.
For example, i implemented crud functionality with spring boot.
i set the attribute quantite
not empty with annotation @notEmpty(message="the quantite field should not be empty").
In the form, if the quantite
field is empty and when i click on submit button, i want that spring validates the inputs.
Produit.class
public class Produit implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
private String designation;
private double prix;
@NotEmpty(message="the quantite field should not be empty")
private int quantite;
...
ProduitRestService.class
@RequestMapping(value="/produits/{id}",method=RequestMethod.PUT)
public Produit update(@RequestBody @valid Produit p,@PathVariable Long id)
{
p.setId(id); // ??????
return produitRepository.saveAndFlush(p);
}
angular 2
add.component.html:
<form #addForm="ngForm" (ngSubmit)="onSubmit(addForm.value)" >
<div class="md-form">
<input type="text" id="form4" class="form-control" name="des" [(ngModel)]="des" >
<label for="form4">designation!!!!!</label>
</div>
<div class="md-form">
<input type="number" id="form4" class="form-control" name="prix" [(ngModel)]="prix" >
<label for="form4">prix</label>
</div>
<div class="md-form">
<input type="number" id="form4" class="form-control" #quantiteref="ngModel" name="quantite" [(ngModel)]="quantite" >
<label for="form4">quantité</label>
<div [hidden]="quantiteref.valid" class="alert alert danger" > this field must not be empty</div> // i know this validation in angular 2, but i need the work to be in spring dev
</div>
<div class="text-center">
<button class="btn btn-deep-purple" (click)="addProduct()" >ADD</button>
</div>
</form>