I did contact form in my application in Spring. And i have this problem:
Field error in object 'contact' on field 'name': rejected value [null]; codes [NotBlank.contact.name,NotBlank.name,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [contact.name,name]; arguments []; default message [name]]; default message [it can't be empty] Field error in object 'contact' on field 'message': rejected value [null]; codes [NotBlank.contact.message,NotBlank.message,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [contact.message,message]; arguments []; default message [message]]; default message [it can't be empty] Field error in object 'contact' on field 'subject': rejected value [null]; codes [NotBlank.contact.subject,NotBlank.subject,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [contact.subject,subject]; arguments []; default message [subject]]; default message [it can't be empty] Field error in object 'contact' on field 'phone': rejected value [null]; codes [NotBlank.contact.phone,NotBlank.phone,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [contact.phone,phone]; arguments []; default message [phone]]; default message [не может быть пусто] Field error in object 'contact' on field 'email': rejected value [null]; codes [NotBlank.contact.email,NotBlank.email,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [contact.email,email]; arguments []; default message [email]]; default message [it can't be empty]]
Controller:
@Controller public class ContactController { @Autowired MailComponent mailComponent; @GetMapping("/contact") public String contact(@ModelAttribute Contact contact) { return "contact"; } @PostMapping("/contact") public String processContact(@Validated Contact contact, RedirectAttributes model, BindingResult bindingResult) { if (bindingResult.hasErrors()) return "contact"; if (mailComponent.sendSimpleMail(contact)) { model.addFlashAttribute("classCss", "alert sukcesu"); model.addFlashAttribute("message", "Twoja wiadomość została wysłana"); } else { model.addFlashAttribute("classCss", "alert ostrzeżenie"); model.addFlashAttribute("message", "Wystąpił nieoczekiwany błąd, powtórz później"); } return "redirect:/"; } } Entity:
public class Contact { @NotBlank @Pattern(regexp = "[\\p{L} '-]+", message = "{com.diet4you.LapkoEkaterina.constraint.Name.message}") private String name; @NotBlank @Pattern(regexp = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])", message = "{com.diet4you.LapkoEkaterina.constraint.Email.message}") private String email; @NotBlank @Pattern(regexp = "\\+(?:[0-9] ?){6,14}[0-9]", message = "{com.diet4you.LapkoEkaterina.constraint.Phone.message}") private String phone; @NotBlank private String subject; @NotBlank private String message; public Contact() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } Validation properties:
javax.validation.constraints.AssertFalse.message = must be false javax.validation.constraints.AssertTrue.message = must be true javax.validation.constraints.DecimalMax.message = must be less than ${inclusive == true ? 'or equal to ' : ''}{value} javax.validation.constraints.DecimalMin.message = must be greater than ${inclusive == true ? 'or equal to ' : ''}{value} javax.validation.constraints.Digits.message = numeric value out of bounds (<{integer} digits>.<{fraction} digits> expected) javax.validation.constraints.Future.message = must be in the future javax.validation.constraints.Max.message = must be less than or equal to {value} javax.validation.constraints.Min.message = must be greater than or equal to {value} javax.validation.constraints.NotNull.message = may not be null javax.validation.constraints.Null.message = must be null javax.validation.constraints.Past.message = must be in the past javax.validation.constraints.Pattern.message = must match "{regexp}" javax.validation.constraints.Size.message = size must be between {min} and {max} org.hibernate.validator.constraints.CreditCardNumber.message = invalid credit card number org.hibernate.validator.constraints.EAN.message = invalid {type} barcode org.hibernate.validator.constraints.Email.message = not a well-formed email address org.hibernate.validator.constraints.Length.message = length must be between {min} and {max} org.hibernate.validator.constraints.LuhnCheck.message = The check digit for ${validatedValue} is invalid, Luhn Modulo 10 checksum failed org.hibernate.validator.constraints.Mod10Check.message = The check digit for ${validatedValue} is invalid, Modulo 10 checksum failed org.hibernate.validator.constraints.Mod11Check.message = The check digit for ${validatedValue} is invalid, Modulo 11 checksum failed org.hibernate.validator.constraints.ModCheck.message = The check digit for ${validatedValue} is invalid, ${modType} checksum failed org.hibernate.validator.constraints.NotBlank.message = This field is required org.hibernate.validator.constraints.NotEmpty.message = may not be empty org.hibernate.validator.constraints.ParametersScriptAssert.message = script expression "{script}" didn't evaluate to true org.hibernate.validator.constraints.Range.message = must be between {min} and {max} org.hibernate.validator.constraints.SafeHtml.message = may have unsafe html content org.hibernate.validator.constraints.ScriptAssert.message = script expression "{script}" didn't evaluate to true org.hibernate.validator.constraints.URL.message = must be a valid URL org.hibernate.validator.constraints.br.CNPJ.message = invalid Brazilian corporate taxpayer registry number (CNPJ) org.hibernate.validator.constraints.br.CPF.message = invalid Brazilian individual taxpayer registry number (CPF) org.hibernate.validator.constraints.br.TituloEleitoral.message = invalid Brazilian Voter ID card number com.diet4you.LE.Name.message = Tylko litery com.diet4you.LE.Email.message = Nie format e-mail com.diet4you.LE.Phone.message = InNie format numeru 13 Answers
BindingResult needs to be immediately after the bean:
Try changing from
@PostMapping("/contact") public String processContact(@Validated Contact contact, RedirectAttributes model, BindingResult bindingResult) To
@PostMapping("/contact") public String processContact(@Validated Contact contact, BindingResult bindingResult, RedirectAttributes model) As you have set the validations for the fields Name, Email, Phone, Subject, Message in the Entity class 'Contact', please check whether you are passing all these values or not in your form. If you are not passing the values as per your Entity class, Spring will not be able to bind the Contact object that you have passed in method processContact and throw BeanPropertyBindingResult errors.
1Remove
import io.swagger.v3.oas.annotations.parameters.RequestBody; and add
import org.springframework.web.bind.annotation.RequestBody; and also add in controller
import org.springframework.validation.annotation.Validated; add in Request body Object
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotBlank;