将新对象附加到Wicket中的自定义验证器

时间:2014-11-28 13:51:13

标签: java validation wicket custom-validators wicket-1.6

我有这样的自定义验证器:

public class PeriodClosedValidator implements IValidator<Date>{

    private Date dateStart;
    private Date dateEnd;

    public PeriodClosedValidator(Date dateStart, Date dateEnd) {
        this.dateStart = dateStart;
        this.dateEnd = dateEnd;
    }

    public void validate(IValidatable<Date> iv) {

        DateFormat sdf = new SimpleDateFormat("dd.MM.yy");

        DimCalendarDAO dimCalendarDao = new DimCalendarDAO(DimCalendar.class,      HibernateUtil.getSessionFactory());
        List<DimCalendar> list = dimCalendarDao.getOpenedOperDates(dateStart, dateEnd);
        StringBuilder errorMessage = new StringBuilder();
        int counter = 10;

        for (DimCalendar cal : list) {
            errorMessage.append(',');
            if (counter == 0) {
                errorMessage.append('\n');
                counter = 10;
            }
            errorMessage.append(sdf.format(cal.getDt()));
            counter--;
        }
        if (errorMessage.length() != 0) {
            ValidationError valError = new ValidationError();
            valError.addKey("error.close.date.period");
            valError.setVariable("dates", errorMessage.substring(1));
            iv.error(valError);
        }
    }
 }

我检查关闭操作日期。 (这段时间有一些信息)。然后我有表格,我附上那个验证器:

public class Frm_2_11A extends BasePage{

    private DimPeriod periodPtr;
    private DimRegion regionPtr;

    private final List<DimPeriod> periodList;
    private final List<DimRegion> regionList;

    private IModel<DimPeriod> periodModel = new PropertyModel<DimPeriod>(this, "periodPtr");
    private IModel<DimRegion> regionModel = new PropertyModel<DimRegion>(this, "regionPtr");

    public Frm_2_11A() {
        super();

        periodList = // get list of period;
        regionList = // get list of region;

        periodModel.setObject(new DimPeriod());

        DropDownChoice period = new DropDownChoice("period", periodModel, periodList, new ChoiceRenderer<DimPeriod>("periodName", "id"));
        period.setRequired(true);
        period.add(new PeriodClosedValidator(periodModel.getObject().getPeriodStart(), periodModel.getObject().getPeriodEnd()));

        DropDownChoice region = new DropDownChoice("region", regionModel, regionList, new ChoiceRenderer<DimRegion>("name", "code"));
        region.setRequired(true);

        Form form = new Form("frm_2_11A"){
            //do something if OK
        };
        form.add(period);
        form.add(region);
        add(new FeedbackPanel("feedback"));
        add(form);
    }

}

问题是,PeriodClosedValidator始终只占用代码periodModel.setObject(new DimPeriod());中的一个对象。我如何附加到自定义验证器新选择的对象?

更新 我如何制作自定义验证器?我想创造类似的东西:

DropDownChoice period = new DropDownChoice("period", periodModel, periodList, new ChoiceRenderer<DimPeriod>("periodName", "id"));
period.add(new MyCustomValidator(param1, param2));

param1param2某些日期,我想要验证。

1 个答案:

答案 0 :(得分:1)

由于您的表单有一个选择器给出DimPeriod,您需要为此创建一个验证器。

您的验证员类应该是:

public class DimPeriodValidator implements IValidator<DimPeriod> {
    @Override
    public void validate(IValidatable<DimPeriod> validatable) {
        //Validation logic here
    }
}

我跳过尝试实际编写验证的地方。它可能与您所拥有的类似,但是从DimPeriod参数中的validatable对象获取日期。