Hi André,
You can add this validator to the form
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;
import org.apache.wicket.util.string.Strings;
import org.apache.wicket.validation.ValidationError;
public class ThreeFieldsValidator extends AbstractFormValidator {
private final FormComponent<?>[] components;
public ThreeFieldsValidator(FormComponent<?> formComponent1,
FormComponent<?> formComponent2, FormComponent<?> formComponent3) {
if (formComponent1 == null) {
throw new IllegalArgumentException("argument formComponent1 cannot
be null");
}
if (formComponent2 == null) {
throw new IllegalArgumentException("argument formComponent2 cannot
be null");
}
if (formComponent3 == null) {
throw new IllegalArgumentException("argument formComponent3 cannot
be null");
}
components = new FormComponent<?>[]{formComponent1, formComponent2,
formComponent3};
}
@Override
public FormComponent<?>[] getDependentFormComponents() {
return components;
}
@Override
public void validate(Form<?> form) {
final FormComponent<?> formComponent1 = components[0];
final FormComponent<?> formComponent2 = components[1];
final FormComponent<?> formComponent3 = components[2];
String s1 = formComponent1.getInput();
String s2 = formComponent2.getInput();
String s3 = formComponent3.getInput();
if (s1.length() + s2.length() + s3.length() != 0) {
if (Strings.isEmpty(s1)) {
ValidationError ve = new ValidationError();
ve.addKey(resourceKey());
components[0].error(ve);
}
if (Strings.isEmpty(s2)) {
ValidationError ve = new ValidationError();
ve.addKey(resourceKey());
components[1].error(ve);
}
if (Strings.isEmpty(s3)) {
ValidationError ve = new ValidationError();
ve.addKey(resourceKey());
components[2].error(ve);
}
}
}
}
François Meillet
Formation Wicket - Développement Wicket
Le 14 avr. 2013 à 11:36, Sven Meier <[email protected]> a écrit :
> You can implement an IFormValidator. Alternatively you use a custom
> IValidator which checks the current value of the optional field:
>
> public class OptionalValidator<T> implements IValidator<T>
> {
>
> private FormComponent<?> optional;
>
> private IValidator<T> validator;
>
> public OptionalValidator(FormComponent<?> optional, IValidator<T>
> validator)
> {
> this.optional = optional;
>
> this.validator = validator;
> }
>
> @Override
> public void validate(IValidatable<T> validatable)
> {
> if (!Strings.isEmpty(optional.getValue()))
> {
> validator.validate(validatable);
> }
> }
> }
>
> Sven
>
> On 04/14/2013 10:28 AM, [email protected] wrote:
>> Hi,
>> i'd like to implement a "3-Field-Validator". This means if one optional
>> field in a form is set, two other fields become mandatory as well.
>> What's the best way to implement this?
>> My first thought was to use it IValidator, but this accepts just values of
>> one field - as far as i've understood
>> My second was to implement a validator method in the submit event of the
>> AjaxButton and to add some FeedbackMessages to the page in case of failure
>>
>> Any other approaches?
>>
>> Thx André
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>