多个字段的弹出一个错误

时间:2016-03-06 20:43:00

标签: java validation spring-mvc

我有一个关于验证多个表单字段是否为空并为所有字段返回一条错误消息的问题

例如,假设我有一个Customer实体,如下所示:

@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;
}

我有一个Spring表单可以通过名字,姓氏或SSN进行搜索,就像这样:



<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>
&#13;
&#13;
&#13;

现在我想在表单下面的div中显示一条错误消息,例如

&#34;请输入至少一个搜索条件。您不能将所有字段留空。&#34;

如果用户使用空表单点击提交。

现在我的主要问题是如何使用控制器或Spring Validation在后端处理此问题?我尝试使用Spring表单验证API,并提出了类似的内容,这对我来说很有用。

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");
    }
}

虽然这样可行,但我不喜欢这样做,因为它看起来不像是一种返回错误信息的优雅方式。 原因是我可以说我要将我的表单更改为最多10个字段用于我的搜索而不是3,然后我必须在一个if语句而不是3中包含10个检查。

如果有人有更好的方法返回一个全局错误,而没有将它浓缩成验证器中的一个if语句,那么我真的很感激它!

此处还有我的Controller方法和其他相关方法的片段:

@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";
}

服务getUniqueCustomer(客户)只调用DAO方法,所以我不会发布任何来自服务的内容,但这里是DAO方法。

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());
}

0 个答案:

没有答案