Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation failed for object='statutProduits'. Error count: 2

When i'm trying to submit my form i get this errors, well i need to create an object "status_product"

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Dec 07 04:07:23 GMT+01:00 2017 There was an unexpected error (type=Bad Request, status=400). Validation failed for object='statutProduits'. Error count: 2

I am having trouble finding the two validation errors show on the error page. I am pretty lost at this point. Hopefully someone can help me out, its preventing me from making progress at the moment.

StatutProduits class:

package com.example.demo.entities;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 *
 * @author G557437
 */
@Entity
@Table(name = "STATUT_PRODUITS", catalog = "", schema = "PACKOUT")

public class StatutProduits implements Serializable {
    private static final long serialVersionUID = 1L;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "ID_STATUT_PRODUIT")
    private BigDecimal idStatutProduit;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "CODE")
    private String code;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "LIBELLE")
    private String libelle;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "CREE_PAR")
    private String creePar;
    @Column(name = "DATE_CREATION")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateCreation;
    @Size(max = 10)
    @Column(name = "MAJ_PAR")
    private String majPar;
    @Column(name = "DATE_MAJ")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateMaj;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idStatutProduit")
    private List<Produits> produitsList;

    public StatutProduits() {
    }

    public StatutProduits(BigDecimal idStatutProduit) {
        this.idStatutProduit = idStatutProduit;
    }

    public StatutProduits(BigDecimal idStatutProduit, String code, String libelle, String creePar) {
        this.idStatutProduit = idStatutProduit;
        this.code = code;
        this.libelle = libelle;
        this.creePar = creePar;
    }

    public BigDecimal getIdStatutProduit() {
        return idStatutProduit;
    }

    public void setIdStatutProduit(BigDecimal idStatutProduit) {
        this.idStatutProduit = idStatutProduit;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getLibelle() {
        return libelle;
    }

    public void setLibelle(String libelle) {
        this.libelle = libelle;
    }

    public String getCreePar() {
        return creePar;
    }

    public void setCreePar(String creePar) {
        this.creePar = creePar;
    }

    public Date getDateCreation() {
        return dateCreation;
    }

    public void setDateCreation(Date dateCreation) {
        this.dateCreation = dateCreation;
    }

    public String getMajPar() {
        return majPar;
    }

    public void setMajPar(String majPar) {
        this.majPar = majPar;
    }

    public Date getDateMaj() {
        return dateMaj;
    }

    public void setDateMaj(Date dateMaj) {
        this.dateMaj = dateMaj;
    }

    public List<Produits> getProduitsList() {
        return produitsList;
    }

    public void setProduitsList(List<Produits> produitsList) {
        this.produitsList = produitsList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idStatutProduit != null ? idStatutProduit.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof StatutProduits)) {
            return false;
        }
        StatutProduits other = (StatutProduits) object;
        if ((this.idStatutProduit == null && other.idStatutProduit != null) || (this.idStatutProduit != null && !this.idStatutProduit.equals(other.idStatutProduit))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.sagemcom.tn.entities.StatutProduits[ idStatutProduit=" + idStatutProduit + " ]";
    }

}

Controller Class:

package com.example.demo.web;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.example.demo.dao.StatutProduitsRepository;
import com.example.demo.entities.StatutProduits;



@Controller
public class StatutProduitsController {

    @Autowired
    StatutProduitsRepository statutproduitsrepository ;


    @RequestMapping(value="/form", method=RequestMethod.GET)
    public String formStatProduit( Model model) {
        model.addAttribute("statproduit",new StatutProduits());
        return "FormStatutProduit";
    }

    @RequestMapping(value="/save", method=RequestMethod.POST)
    public String save( Model model, StatutProduits statproduit) {
        statutproduitsrepository.save(statproduit);
        return "Confirmation";
    }
}

thymleaf:

<body>
<div class="container">
    <div class="panel panel-default">
        <div class="panel-heading">Statut d'un produit</div>
            <div class="panel-body">
            <form th:action="@{save}"  method="post">
                <div class="form-group">

                    <label class="control-label">ID</label>
                    <input class="form-control" type="number" name="idStatutProduit"
                            th:value="${statproduit.idStatutProduit}"/>
                </div>
                <div class="form-group">

                    <label class="control-label">Code</label>
                    <input class="form-control" type="text" name="code"
                    th:value="${statproduit.code}"/>
                </div>
                <div class="form-group">

                    <label class="control-label">Cree par</label>
                    <input class="form-control" type="text" name="creePar"
                          th:value="${statproduit.creePar}"/>
                </div>
                <div class="form-group">

                    <label class="control-label">Date creation</label>
                    <input class="form-control" type="date"  name="dateCreation"
                    th:value="${statproduit.dateCreation}"/>
                </div>
                <div class="form-group">

                    <label class="control-label">Date maj</label>
                    <input class="form-control" type="date" name="dateMaj"
                    th:value="${statproduit.dateMaj}"/>
                </div>
                <div class="form-group">

                    <label class="control-label">Libelle</label>
                    <input class="form-control" type="text" name="libelle"
                    th:value="${statproduit.libelle}"/>
                </div>
                <div class="form-group">

                    <label class="control-label">Maj par</label>
                    <input class="form-control" type="text"  name="majPar"
                     th:value="${statproduit.majPar}"/>
                </div>
                <div>
                <button type="submit" class="btn btn-primary">Save</button>

                </div>
             </form>    
            </div>
     </div>
</div>
</body>
like image 471
Khadhri Hamza Avatar asked Mar 26 '26 01:03

Khadhri Hamza


1 Answers

In your case, you have added server-side validation but in your controller, you didn't check that conditions and blindly saving that object into your database. instead of doing like that check the conditions in the controller by using BindingResult

 @RequestMapping(value="/save", method=RequestMethod.POST)
 public String save( Model model, @Validated StatutProduits statproduit BindingResult bindingResult) {
     if(bindingResult.hasErrors()){
         return "to same page to shows error fields";
     }
    statutproduitsrepository.save(statproduit);
    return "Confirmation";
 }
like image 136
Kalaiselvan Avatar answered Mar 27 '26 14:03

Kalaiselvan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!