Attach new object to custom validator in Wicket

501 views Asked by At

I have such custom validator:

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

where I check closing operation dates. (is there some info for such period). Then I have form, where I attach that validator:

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

}

the problem is, PeriodClosedValidator always take just one object in code periodModel.setObject(new DimPeriod());. How I can attach to custom validator new chosen object?

UPDATE How I can make custom validator? I want create something like that:

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

Where param1 and param2 some dates, which I want to validate.

1

There are 1 answers

0
Don Roby On BEST ANSWER

Since your form has a selector giving a DimPeriod, you need to make a validator for that.

Your validator class should be something like:

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

where I've skipped trying to actually write the validation. It's probably similar to what you've got, but getting the dates from DimPeriod object inside the validatable argument.