0

suppose i hava two view files(jsps) and one class(BudgetControlRegisterDto)

BudgetControlRegisterDto

public class BudgetControlRegisterDto implements Serializable {
@NotNull(message = "{NotNull.java.util.Date}")
    private Date demandReceiveDate;

@NotNull(message = "{NotNull.java.util.Date}")
    private Date demandOriginalDate;

@NotNull(message = "Start date {NotNull.java.util.Date}")
    private Date startDate;
@NotNull(message = "End date {NotNull.java.util.Date}")
    private Date endDate;


// setter and getter 
}

In one view file i want to validate startDate and endDate and in other view file i want to validate demandOriginalDate and demandReceiveDate using json ajax. when validation occurs i get validation message for all fields with below code:

controller class's method this is testing code used by both view files(jsps)

@RequestMapping(value = "/addnewdemand.json", method = RequestMethod.POST)
    public @ResponseBody BudgetControlRegisterDto addNewDemand(@Valid @ModelAttribute("bcrDto") BudgetControlRegisterDto bcrDto,Errors errors){
    log.info("addNewDemand invoked!");
        if(errors.hasErrors()) {
            log.info("has errors");
            bcrDto.setFieldsErrors(errors.getFieldErrors());
            return bcrDto;
        }


    return bcrDto;
    }

.js file this is testing code used by both view files(jsps) below code is ajax response code

if(response.fieldsErrors != null) {
    html ='<div class="ui-message-error">';
    for(var i= 0; i<response.fieldsErrors.length; i++) {
        html+='<span>'+response.fieldsErrors[i].defaultMessage+'</span><br/>';
    }
    html+='</div>';
    $("#bcrForm_message").html(html);
}

Question why m i getting validation message of all fields

Mohsin AR
  • 2,998
  • 2
  • 24
  • 36

1 Answers1

1

this is the situation where groups takes place. Here is a good tutorial about them.

The first thing you need to change is to add groups attribute to your @NotNull annotations.

public class BudgetControlRegisterDto implements Serializable {
@NotNull(message = "{NotNull.java.util.Date}",groups={First.class})
    private Date demandReceiveDate;

@NotNull(message = "{NotNull.java.util.Date}",groups={First.class})
    private Date demandOriginalDate;

@NotNull(message = "Start date {NotNull.java.util.Date}",groups={Second.class})
    private Date startDate;
@NotNull(message = "End date {NotNull.java.util.Date}",groups={Second.class})
    private Date endDate;


public interface First {};
public interface Second {};

// setter and getter 
}

the second one is to change from @Valid to @Validated because @Valid doesn't support validation groups.

controller's method:

@RequestMapping(value = "/addnewdemand.json", method = RequestMethod.POST)
    public @ResponseBody BudgetControlRegisterDto addNewDemand(@Validated(BudgetControlRegisterDto.First.class) @ModelAttribute("bcrDto") BudgetControlRegisterDto bcrDto,Errors errors){
    log.info("addNewDemand invoked!");
        if(errors.hasErrors()) {
            log.info("has errors");
            bcrDto.setFieldsErrors(errors.getFieldErrors());
            return bcrDto;
        }


    return bcrDto;
    }
Nikolay Rusev
  • 4,060
  • 3
  • 19
  • 29