You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Thorsten Scherler <th...@juntadeandalucia.es> on 2009/04/27 12:16:04 UTC

Validator for 2 fields

Hi all,

I have a question about validating one field with another. 

I have a form that ask for the start page and for the end page of an
article. I need to validate whether the startPage is lesser or equal to
the endPage.

I could do this in public void onSubmit() {...} of the form like:
public void onSubmit() {
 if(model.getStartPage()>model.getEndPage()){
      this.error("The startPage cannot be bigger then the endPage");
    }
...
}

However I wonder if that is not cleaner with a validator. My problem is
ATM that I have not found an example that shows how to validate a field
comparing it to another field in the same form.

In pseudo code:

RequiredTextField start = new RequiredTextField("startPage",
Integer.class);
add(start);
RequiredTextField end = new RequiredTextField("endPage", Integer.class);

// the next line does not exits but would be what I need

NumberValidator max = NumberValidator.minimum(start);
end.add(max);
add(end);

What is the best way to implement such a validation?

TIA for any tips.

salu2
-- 
Thorsten Scherler <thorsten.at.apache.org>
Open Source Java <consulting, training and solutions>

Sociedad Andaluza para el Desarrollo de la Sociedad 
de la Información, S.A.U. (SADESI)





---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Validator for 2 fields

Posted by Thorsten Scherler <th...@juntadeandalucia.es>.
On Mon, 2009-04-27 at 12:16 +0200, Thorsten Scherler wrote:
> Hi all,
> 
> I have a question about validating one field with another. 
> 
> I have a form that ask for the start page and for the end page of an
> article. I need to validate whether the startPage is lesser or equal to
> the endPage.
> 
> I could do this in public void onSubmit() {...} of the form like:
> public void onSubmit() {
>  if(model.getStartPage()>model.getEndPage()){
>       this.error("The startPage cannot be bigger then the endPage");
>     }
> ...
> }
> 
> However I wonder if that is not cleaner with a validator. My problem is
> ATM that I have not found an example that shows how to validate a field
> comparing it to another field in the same form.

I found a solution. :)


    RequiredTextField start = new RequiredTextField("startPage",
Integer.class);
    add(start);
    RequiredTextField end = new RequiredTextField("endPage",
Integer.class);
    MinimumValidator miniVal = new MinimumValidator (start);
    end.add(miniVal);
    add(end);


I created a small Validator that is doing the comparison and is working
very nicely:

public class MinimumValidator extends AbstractValidator {
  
  private TextField field;

  public MinimumValidator(TextField field){
    this.field = field;
  }
  protected Map variablesMap(IValidatable validatable)
  {
          final Map map = super.variablesMap(validatable);
          map.put("field", field);
          return map;
  }

  @Override
  protected void onValidate(IValidatable validatable) {
    int end = (Integer)validatable.getValue();
    int start = Integer.parseInt(field.getValue());
    if (start>end){
      error(validatable);
    }
  }

salu2
-- 
Thorsten Scherler <thorsten.at.apache.org>
Open Source Java <consulting, training and solutions>

Sociedad Andaluza para el Desarrollo de la Sociedad 
de la Información, S.A.U. (SADESI)





---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Validator for 2 fields

Posted by Thorsten Scherler <th...@juntadeandalucia.es>.
On Mon, 2009-04-27 at 06:43 -0400, Willis Blackburn wrote:
> Thorsten,
> 
> Look at the Wicket class called EqualInputValidator for guidance.
> 
> You have to add it to the form, not to the individual fields.
> 

Thanks.

Meanwhile I found a working solution. Will post it now. 

salu2

> W
> 
> 
> On Apr 27, 2009, at 6:16 AM, Thorsten Scherler wrote:
> 
> > Hi all,
> >
> > I have a question about validating one field with another.
> >
> > I have a form that ask for the start page and for the end page of an
> > article. I need to validate whether the startPage is lesser or equal  
> > to
> > the endPage.
> >
> > I could do this in public void onSubmit() {...} of the form like:
> > public void onSubmit() {
> > if(model.getStartPage()>model.getEndPage()){
> >      this.error("The startPage cannot be bigger then the endPage");
> >    }
> > ...
> > }
> >
> > However I wonder if that is not cleaner with a validator. My problem  
> > is
> > ATM that I have not found an example that shows how to validate a  
> > field
> > comparing it to another field in the same form.
> >
> > In pseudo code:
> >
> > RequiredTextField start = new RequiredTextField("startPage",
> > Integer.class);
> > add(start);
> > RequiredTextField end = new RequiredTextField("endPage",  
> > Integer.class);
> >
> > // the next line does not exits but would be what I need
> >
> > NumberValidator max = NumberValidator.minimum(start);
> > end.add(max);
> > add(end);
> >
> > What is the best way to implement such a validation?
> >
> > TIA for any tips.
> >
> > salu2
> > -- 
> > Thorsten Scherler <thorsten.at.apache.org>
> > Open Source Java <consulting, training and solutions>
> >
> > Sociedad Andaluza para el Desarrollo de la Sociedad
> > de la Información, S.A.U. (SADESI)
> >
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> > For additional commands, e-mail: users-help@wicket.apache.org
> >
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 
-- 
Thorsten Scherler <thorsten.at.apache.org>
Open Source Java <consulting, training and solutions>

Sociedad Andaluza para el Desarrollo de la Sociedad 
de la Información, S.A.U. (SADESI)





---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Validator for 2 fields

Posted by Willis Blackburn <wb...@panix.com>.
Thorsten,

Look at the Wicket class called EqualInputValidator for guidance.

You have to add it to the form, not to the individual fields.

W


On Apr 27, 2009, at 6:16 AM, Thorsten Scherler wrote:

> Hi all,
>
> I have a question about validating one field with another.
>
> I have a form that ask for the start page and for the end page of an
> article. I need to validate whether the startPage is lesser or equal  
> to
> the endPage.
>
> I could do this in public void onSubmit() {...} of the form like:
> public void onSubmit() {
> if(model.getStartPage()>model.getEndPage()){
>      this.error("The startPage cannot be bigger then the endPage");
>    }
> ...
> }
>
> However I wonder if that is not cleaner with a validator. My problem  
> is
> ATM that I have not found an example that shows how to validate a  
> field
> comparing it to another field in the same form.
>
> In pseudo code:
>
> RequiredTextField start = new RequiredTextField("startPage",
> Integer.class);
> add(start);
> RequiredTextField end = new RequiredTextField("endPage",  
> Integer.class);
>
> // the next line does not exits but would be what I need
>
> NumberValidator max = NumberValidator.minimum(start);
> end.add(max);
> add(end);
>
> What is the best way to implement such a validation?
>
> TIA for any tips.
>
> salu2
> -- 
> Thorsten Scherler <thorsten.at.apache.org>
> Open Source Java <consulting, training and solutions>
>
> Sociedad Andaluza para el Desarrollo de la Sociedad
> de la Información, S.A.U. (SADESI)
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org