I have a question about validating if multiple form fields are empty and returning one error message for all of the fields
For example lets say I have a Customer entity as follows:
@Entity
@Table(name="customer")
public class Customer {
@Column(name="first_name")
private String firstName;
@Column(name="middle_name")
private String middleName;
@Column(name="last_name")
private String lastName;
@Column(name="ssn")
private Integer ssn;
@Column(name="phone")
private Long phoneNumber;
@Column(name="email")
private String email;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getSsn() {
return ssn;
}
public void setSsn(Integer ssn) {
this.ssn = ssn;
}
public Long getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(Long phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
And I have a Spring form to do a search by either First Name, Last Name, or SSN like that goes like this:
<form:form method="get" modelAttribute="customer" action="submitCustomerSearch" >
First Name:
<form:input type="text" path="firstName" name="firstName"/>
<br>
Last Name:
<form:input type="text" path="lastName" name="lastName"/>
<br>
SSN:
<form:input path="ssn" name="ssn"/>
<br>
<input type="submit" name="submit" value="Submit" class="btn"><br>
<form:errors />
</form:form>
Now I would like to display an error message in a div right underneath the form saying something like,
"Please enter at least one search criteria. you may not leave all fields blank."
in the case that the user hits submit with an empty form.
Now my main question is that how do I handle this in the backend by using either a controller or Spring Validation? I tried messing around with Spring form validation API, and came up up with something like this which works out for me.
public void validate(Object target, Errors errors) {
Customer customer = (Customer) target;
if(customer.getFirstName().isEmpty() && customer.getLastName().isEmpty() &&
customer.getSsn() == null ){
errors.reject("empty.form");
}
}
Although this will work, I don't prefer doing this, because it doesn't seem like an elegant way of returning an error message. The reason being is lets say I were to change my form to have up to 10 fields for my search instead of 3, then I would have to include 10 checks in one if statement instead of 3.
If anyone has a better way of returning a global error without condensing this into one if statement in the validator then I would really appreciate it!
Also here is a snippet of my Controller method and other relevant methods:
@Autowired
private CustomerService customerService;
...
@Autowired
CustomerValidator customerValidator;
...
@InitBinder
public void initBinder(WebDataBinder binder){
binder.setValidator(customerValidator);
}
...
@RequestMapping(value="/customers/searchCustomer")
public String searchCustomer(Model model){
model.addAttribute("customer", new Customer());
return "searchCustomer";
}
@RequestMapping(value="/customers/submitCustomerSearch", method=RequestMethod.GET)
public String submitCustomerSearch(@ModelAttribute("customer") Customer customer, BindingResult result, Model model, final RedirectAttributes redirectAttributes){
customerValidator.validate(customer, result);
if(result.hasErrors()){
return "searchCustomer";
}
Set<Customer> custs = customerService.getUniqueCustomers(customer);
redirectAttributes.addFlashAttribute("custs", custs);
return"redirect:/customers/searchCustomer";
}
The Service getUniqueCustomer(customer) only calls the DAO method so I am not gonna post anything from the service, but here is the DAO method.
public Set<Customer> getUniqueCustomers(Customer customer){
Criteria crit = this.sessionFactory.getCurrentSession().createCriteria(Customer.class);
if(!customer.getFirstName().isEmpty())
crit.add(Restrictions.like("firstName", "%"+customer.getFirstName()+"%"));
if(!customer.getLastName().isEmpty())
crit.add(Restrictions.like("lastName", "%"+customer.getLastName()+"%"));
if(customer.getSsn() != null)
crit.add(Restrictions.sqlRestriction("cast(ssn as varchar) LIKE '%"+customer.getSsn()+"%'"));
return new LinkedHashSet<>(crit.list());
}