You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Nathan Coast <na...@codeczar.com> on 2007/02/09 12:58:39 UTC

type validwhen validation

Hi,

I have two fields in a form.  I believe the validwhen rule executes a 
specific rule (e.g. required) on field a based upon the value of field 
b.  The situation I have is that I need to execute different validations 
on field a determined by the value of field b.

Is this possible client side with the latest validator framework 
(1.3.1)? or will I have to implement this functionality in the validate 
method of the form (server side)?

cheers
Nathan


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: type validwhen validation

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nathan,

Nathan Coast wrote:
> this feels better as the existing framework is responsible for executing
> all validations, assembling cumulative errors and creating localised
> messages.  I'm just not sure it's possible to add validations.

Whenever I find that I need some "validation" process done, but the
stock validators don't cover it, I'm usually talking about a single
action that will handle the form submission. In that case, I allow the
stock validators to handle the mundane stuff (required, integer, etc.)
and then just code the complex stuff directly in my action.

Is that feasible for you? It might turn out to be the simplest solution.

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFzMc29CaO5/Lv0PARAhOcAJ9Inji7P23jeYfYjTVtBSwicL1vUwCcCzji
LTVb3npHCNPuGNfaBZDU0PI=
=y3c1
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: type validwhen validation

Posted by Nathan Coast <na...@codeczar.com>.
excellent stuff,

It would probably have taken me a couple of weeks to get here :)

I'll let you know how I got on.

cheers
Nathan

Niall Pemberton wrote:
> On 2/9/07, Nathan Coast <na...@codeczar.com> wrote:
>> Hi Niall,
>>
>> thanks again for the quick response.
>>
>> I've been looking at the struts and validator source and have a couple
>> of ideas for a solution to my problem.
>>
>> 1) just create an appropriate validator and execute it.  Simpler but
>> doesn't 'feel' as good.
>>
>> IntegerValidator validator = IntegerValidator.getInstance();
>>
>>        Integer fooInteger = validator.validate(fooString, "#,##0.00",
>> Locale.GERMAN);
>>        if (fooInteger == null) {
>>            // error...not a valid Integer
>>            return;
>>        }
>>
>> 2) get the existing validator for the form and add the appropriate
>> validator, so all of the form validations are executed i.e. from the
>> config and additional validations.
>>
>> Validator validator = Resources.initValidator(validationKey,
>>                        this,
>>                        application, request,
>>                        errors, page);
>> *pseudocode*
>> validator.add(fooBarValidator,..,...,..)
>> validator.validate();
>>
>> this feels better as the existing framework is responsible for executing
>> all validations, assembling cumulative errors and creating localised
>> messages.  I'm just not sure it's possible to add validations.
>>
>> I think I may have to clone form validators so that adding validations
>> to the validator doesn't affect subsequent validations on forms with the
>> same name.
> 
> As you say option 1 is simple and I guess it depends whether this is a
> "one off" or you need to re-use it in more than one place?
> 
> Assuming the later - you would need to clone since changing the
> configuration for a set of form validation rules will affect them all.
> You would also need to set up any custom messages (<msg> and <arg>
> elements) unless a generic message (no label) would do for all. IMO it
> would be more straight forward to write a custom validator that
> delegates to the others
> 
> public static Object validateConditional (
>        Object bean,
>        ValidatorAction va,
>        Field field,
>        ActionMessages errors,
>        Validator validator,
>        HttpServletRequest request) {
> 
>    if (??? Short ???) {
>        return FieldChecks.validateShort(bean, va, field, errors,
> validator, request);
>    } else if (??? Integer???) {
>        return FieldChecks.validateInteger(bean, va, field, errors,
> validator, request);
>    } else if (??? Double???) {
>        return FieldChecks.validateDouble(bean, va, field, errors,
> validator, request);
>    } else if (??? Date???) {
>        return FieldChecks.validateDate(bean, va, field, errors,
> validator, request);
>    } else if ...
>    }
> 
> }
> 
> That way you could configure messages/arguments in you validation.xml as 
> usual:
> 
>    <field property="foo" depends="conditional">
>        <msg name="short"   key="foo.short.error">
>        <msg name="integer" key="foo.integer.error">
>        <msg name="double" key="foo.double.error">
>        <msg name="date" key="foo.date.error">
>        <arg0 key="Foo Label" resource="false">
>        
> <var><var-name>datePattern<var-name><var-value>dd/MM/yyyy<var-value></var>
>    </field>
> 
> Need to remember to configure your "conditional" validator:
> 
> <validator name="conditional"
>                   classname="myPackage.MyValidations"
>                   method="validateConditional"
>                   methodParams="java.lang.Object,
>                       org.apache.commons.validator.ValidatorAction,
>                       org.apache.commons.validator.Field,
>                       org.apache.struts.action.ActionMessages,
>                       org.apache.commons.validator.Validator,
>                       javax.servlet.http.HttpServletRequest"
>                   depends=""
>                   msg="default,msg"/>
> 
> Not something I've done though, so just shooting from the hip.
> 
> Niall
> 
>> any chance you can point me in the right direction before I waste 2 days
>> trying something impossible :)
>>
>> cheers
>> Nathan
>>
>>
>> Niall Pemberton wrote:
>> > On 2/9/07, Nathan Coast <na...@codeczar.com> wrote:
>> >> thanks Niall,
>> >>
>> >> I've had a look at the examples and I'm not sure they demonstrate 
>> what I
>> >> need.  The examples all seem to be simple boolean expressions e.g.
>> >>
>> >>        <field property="address1" depends="validwhen">
>> >>          <arg key="validWhenForm.address1" />
>> >>          <var>
>> >>            <var-name>test</var-name>
>> >>            <var-value>( (*this* != null) or
>> >>                       ( (address2 == null) and
>> >>                       ( (city == null) and
>> >>                         (zip == null) )))</var-value>
>> >>          </var>
>> >>        </field>
>> >>
>> >> I have a select (fieldB) that determines the type of fieldA. So the
>> >> validation to operate on fieldA depends on the value of fieldB.
>> >>
>> >>        <field property="fieldA" depends="validwhen,required">
>> >>          <arg key="form.fieldA" />
>> >>          <var>
>> >>            <var-name>test</var-name>
>> >>            <var-value>
>> >>       ((fieldB == 'java.lang.Integer') && validateInteger(fieldA))
>> >>    || ((fieldB == 'java.lang.Float') && validateFloat(fieldA))
>> >>    || ((fieldB == 'java.util.Date') && validateDate(fieldA))
>> >> etc....
>> >>          </var>
>> >>        </field>
>> >
>> > The above looks like JavaScript - because validwhen is server side
>> > only. Either way validwhen doesn't cater for doing different types of
>> > validation (e.g. integer, float etc) depending on a value.
>> >
>> >> is this possible?  I'm thinking not, so a custom validation method is
>> >> probably what I need.
>> >
>> > Looks like it
>> >
>> > Niall
>> >
>> >> cheers
>> >> Nathan
>> >>
>> >>
>> >> Niall Pemberton wrote:
>> >> > On 2/9/07, Nathan Coast <na...@codeczar.com> wrote:
>> >> >> Hi,
>> >> >>
>> >> >> I have two fields in a form.  I believe the validwhen rule 
>> executes a
>> >> >> specific rule (e.g. required) on field a based upon the value of 
>> field
>> >> >> b.  The situation I have is that I need to execute different
>> >> validations
>> >> >> on field a determined by the value of field b.
>> >> >
>> >> > Yes - theres a validwhen example page in the struts-examples 
>> webapp in
>> >> > the binary distro (see the validation module).
>> >> >
>> >> >> Is this possible client side with the latest validator framework
>> >> >> (1.3.1)? or will I have to implement this functionality in the
>> >> validate
>> >> >> method of the form (server side)?
>> >> >
>> >> > validwhen is server side only.
>> >> >
>> >> > Its also a custom Struts validator - not part of Commons 
>> Validator so
>> >> > in that respect the version of Commons Validator is not relevant.
>> >> >
>> >> > Niall
>> >> >
>> >> >> cheers
>> >> >> Nathan
>> >> >>
>> >> >>
>> >> >> 
>> ---------------------------------------------------------------------
>> >> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> >> >> For additional commands, e-mail: user-help@struts.apache.org
>> >> >>
>> >> >>
>> >> >
>> >> > 
>> ---------------------------------------------------------------------
>> >> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> >> > For additional commands, e-mail: user-help@struts.apache.org
>> >> >
>> >> >
>> >> >
>> >>
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> >> For additional commands, e-mail: user-help@struts.apache.org
>> >>
>> >>
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> > For additional commands, e-mail: user-help@struts.apache.org
>> >
>> >
>> >
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: type validwhen validation

Posted by Nathan Coast <na...@codeczar.com>.
apologies if this is a dumb question, but how come there is no 
validateBoolean?

Niall Pemberton wrote:
> On 2/9/07, Nathan Coast <na...@codeczar.com> wrote:
>> Hi Niall,
>>
>> thanks again for the quick response.
>>
>> I've been looking at the struts and validator source and have a couple
>> of ideas for a solution to my problem.
>>
>> 1) just create an appropriate validator and execute it.  Simpler but
>> doesn't 'feel' as good.
>>
>> IntegerValidator validator = IntegerValidator.getInstance();
>>
>>        Integer fooInteger = validator.validate(fooString, "#,##0.00",
>> Locale.GERMAN);
>>        if (fooInteger == null) {
>>            // error...not a valid Integer
>>            return;
>>        }
>>
>> 2) get the existing validator for the form and add the appropriate
>> validator, so all of the form validations are executed i.e. from the
>> config and additional validations.
>>
>> Validator validator = Resources.initValidator(validationKey,
>>                        this,
>>                        application, request,
>>                        errors, page);
>> *pseudocode*
>> validator.add(fooBarValidator,..,...,..)
>> validator.validate();
>>
>> this feels better as the existing framework is responsible for executing
>> all validations, assembling cumulative errors and creating localised
>> messages.  I'm just not sure it's possible to add validations.
>>
>> I think I may have to clone form validators so that adding validations
>> to the validator doesn't affect subsequent validations on forms with the
>> same name.
> 
> As you say option 1 is simple and I guess it depends whether this is a
> "one off" or you need to re-use it in more than one place?
> 
> Assuming the later - you would need to clone since changing the
> configuration for a set of form validation rules will affect them all.
> You would also need to set up any custom messages (<msg> and <arg>
> elements) unless a generic message (no label) would do for all. IMO it
> would be more straight forward to write a custom validator that
> delegates to the others
> 
> public static Object validateConditional (
>        Object bean,
>        ValidatorAction va,
>        Field field,
>        ActionMessages errors,
>        Validator validator,
>        HttpServletRequest request) {
> 
>    if (??? Short ???) {
>        return FieldChecks.validateShort(bean, va, field, errors,
> validator, request);
>    } else if (??? Integer???) {
>        return FieldChecks.validateInteger(bean, va, field, errors,
> validator, request);
>    } else if (??? Double???) {
>        return FieldChecks.validateDouble(bean, va, field, errors,
> validator, request);
>    } else if (??? Date???) {
>        return FieldChecks.validateDate(bean, va, field, errors,
> validator, request);
>    } else if ...
>    }
> 
> }
> 
> That way you could configure messages/arguments in you validation.xml as 
> usual:
> 
>    <field property="foo" depends="conditional">
>        <msg name="short"   key="foo.short.error">
>        <msg name="integer" key="foo.integer.error">
>        <msg name="double" key="foo.double.error">
>        <msg name="date" key="foo.date.error">
>        <arg0 key="Foo Label" resource="false">
>        
> <var><var-name>datePattern<var-name><var-value>dd/MM/yyyy<var-value></var>
>    </field>
> 
> Need to remember to configure your "conditional" validator:
> 
> <validator name="conditional"
>                   classname="myPackage.MyValidations"
>                   method="validateConditional"
>                   methodParams="java.lang.Object,
>                       org.apache.commons.validator.ValidatorAction,
>                       org.apache.commons.validator.Field,
>                       org.apache.struts.action.ActionMessages,
>                       org.apache.commons.validator.Validator,
>                       javax.servlet.http.HttpServletRequest"
>                   depends=""
>                   msg="default,msg"/>
> 
> Not something I've done though, so just shooting from the hip.
> 
> Niall
> 
>> any chance you can point me in the right direction before I waste 2 days
>> trying something impossible :)
>>
>> cheers
>> Nathan
>>
>>
>> Niall Pemberton wrote:
>> > On 2/9/07, Nathan Coast <na...@codeczar.com> wrote:
>> >> thanks Niall,
>> >>
>> >> I've had a look at the examples and I'm not sure they demonstrate 
>> what I
>> >> need.  The examples all seem to be simple boolean expressions e.g.
>> >>
>> >>        <field property="address1" depends="validwhen">
>> >>          <arg key="validWhenForm.address1" />
>> >>          <var>
>> >>            <var-name>test</var-name>
>> >>            <var-value>( (*this* != null) or
>> >>                       ( (address2 == null) and
>> >>                       ( (city == null) and
>> >>                         (zip == null) )))</var-value>
>> >>          </var>
>> >>        </field>
>> >>
>> >> I have a select (fieldB) that determines the type of fieldA. So the
>> >> validation to operate on fieldA depends on the value of fieldB.
>> >>
>> >>        <field property="fieldA" depends="validwhen,required">
>> >>          <arg key="form.fieldA" />
>> >>          <var>
>> >>            <var-name>test</var-name>
>> >>            <var-value>
>> >>       ((fieldB == 'java.lang.Integer') && validateInteger(fieldA))
>> >>    || ((fieldB == 'java.lang.Float') && validateFloat(fieldA))
>> >>    || ((fieldB == 'java.util.Date') && validateDate(fieldA))
>> >> etc....
>> >>          </var>
>> >>        </field>
>> >
>> > The above looks like JavaScript - because validwhen is server side
>> > only. Either way validwhen doesn't cater for doing different types of
>> > validation (e.g. integer, float etc) depending on a value.
>> >
>> >> is this possible?  I'm thinking not, so a custom validation method is
>> >> probably what I need.
>> >
>> > Looks like it
>> >
>> > Niall
>> >
>> >> cheers
>> >> Nathan
>> >>
>> >>
>> >> Niall Pemberton wrote:
>> >> > On 2/9/07, Nathan Coast <na...@codeczar.com> wrote:
>> >> >> Hi,
>> >> >>
>> >> >> I have two fields in a form.  I believe the validwhen rule 
>> executes a
>> >> >> specific rule (e.g. required) on field a based upon the value of 
>> field
>> >> >> b.  The situation I have is that I need to execute different
>> >> validations
>> >> >> on field a determined by the value of field b.
>> >> >
>> >> > Yes - theres a validwhen example page in the struts-examples 
>> webapp in
>> >> > the binary distro (see the validation module).
>> >> >
>> >> >> Is this possible client side with the latest validator framework
>> >> >> (1.3.1)? or will I have to implement this functionality in the
>> >> validate
>> >> >> method of the form (server side)?
>> >> >
>> >> > validwhen is server side only.
>> >> >
>> >> > Its also a custom Struts validator - not part of Commons 
>> Validator so
>> >> > in that respect the version of Commons Validator is not relevant.
>> >> >
>> >> > Niall
>> >> >
>> >> >> cheers
>> >> >> Nathan
>> >> >>
>> >> >>
>> >> >> 
>> ---------------------------------------------------------------------
>> >> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> >> >> For additional commands, e-mail: user-help@struts.apache.org
>> >> >>
>> >> >>
>> >> >
>> >> > 
>> ---------------------------------------------------------------------
>> >> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> >> > For additional commands, e-mail: user-help@struts.apache.org
>> >> >
>> >> >
>> >> >
>> >>
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> >> For additional commands, e-mail: user-help@struts.apache.org
>> >>
>> >>
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> > For additional commands, e-mail: user-help@struts.apache.org
>> >
>> >
>> >
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: type validwhen validation

Posted by Niall Pemberton <ni...@gmail.com>.
On 2/9/07, Nathan Coast <na...@codeczar.com> wrote:
> Hi Niall,
>
> thanks again for the quick response.
>
> I've been looking at the struts and validator source and have a couple
> of ideas for a solution to my problem.
>
> 1) just create an appropriate validator and execute it.  Simpler but
> doesn't 'feel' as good.
>
> IntegerValidator validator = IntegerValidator.getInstance();
>
>        Integer fooInteger = validator.validate(fooString, "#,##0.00",
> Locale.GERMAN);
>        if (fooInteger == null) {
>            // error...not a valid Integer
>            return;
>        }
>
> 2) get the existing validator for the form and add the appropriate
> validator, so all of the form validations are executed i.e. from the
> config and additional validations.
>
> Validator validator = Resources.initValidator(validationKey,
>                        this,
>                        application, request,
>                        errors, page);
> *pseudocode*
> validator.add(fooBarValidator,..,...,..)
> validator.validate();
>
> this feels better as the existing framework is responsible for executing
> all validations, assembling cumulative errors and creating localised
> messages.  I'm just not sure it's possible to add validations.
>
> I think I may have to clone form validators so that adding validations
> to the validator doesn't affect subsequent validations on forms with the
> same name.

As you say option 1 is simple and I guess it depends whether this is a
"one off" or you need to re-use it in more than one place?

Assuming the later - you would need to clone since changing the
configuration for a set of form validation rules will affect them all.
You would also need to set up any custom messages (<msg> and <arg>
elements) unless a generic message (no label) would do for all. IMO it
would be more straight forward to write a custom validator that
delegates to the others

public static Object validateConditional (
        Object bean,
        ValidatorAction va,
        Field field,
        ActionMessages errors,
        Validator validator,
        HttpServletRequest request) {

    if (??? Short ???) {
        return FieldChecks.validateShort(bean, va, field, errors,
validator, request);
    } else if (??? Integer???) {
        return FieldChecks.validateInteger(bean, va, field, errors,
validator, request);
    } else if (??? Double???) {
        return FieldChecks.validateDouble(bean, va, field, errors,
validator, request);
    } else if (??? Date???) {
        return FieldChecks.validateDate(bean, va, field, errors,
validator, request);
    } else if ...
    }

}

That way you could configure messages/arguments in you validation.xml as usual:

    <field property="foo" depends="conditional">
        <msg name="short"   key="foo.short.error">
        <msg name="integer" key="foo.integer.error">
        <msg name="double" key="foo.double.error">
        <msg name="date" key="foo.date.error">
        <arg0 key="Foo Label" resource="false">
        <var><var-name>datePattern<var-name><var-value>dd/MM/yyyy<var-value></var>
    </field>

Need to remember to configure your "conditional" validator:

<validator name="conditional"
                   classname="myPackage.MyValidations"
                   method="validateConditional"
                   methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
                   depends=""
                   msg="default,msg"/>

Not something I've done though, so just shooting from the hip.

Niall

> any chance you can point me in the right direction before I waste 2 days
> trying something impossible :)
>
> cheers
> Nathan
>
>
> Niall Pemberton wrote:
> > On 2/9/07, Nathan Coast <na...@codeczar.com> wrote:
> >> thanks Niall,
> >>
> >> I've had a look at the examples and I'm not sure they demonstrate what I
> >> need.  The examples all seem to be simple boolean expressions e.g.
> >>
> >>        <field property="address1" depends="validwhen">
> >>          <arg key="validWhenForm.address1" />
> >>          <var>
> >>            <var-name>test</var-name>
> >>            <var-value>( (*this* != null) or
> >>                       ( (address2 == null) and
> >>                       ( (city == null) and
> >>                         (zip == null) )))</var-value>
> >>          </var>
> >>        </field>
> >>
> >> I have a select (fieldB) that determines the type of fieldA. So the
> >> validation to operate on fieldA depends on the value of fieldB.
> >>
> >>        <field property="fieldA" depends="validwhen,required">
> >>          <arg key="form.fieldA" />
> >>          <var>
> >>            <var-name>test</var-name>
> >>            <var-value>
> >>       ((fieldB == 'java.lang.Integer') && validateInteger(fieldA))
> >>    || ((fieldB == 'java.lang.Float') && validateFloat(fieldA))
> >>    || ((fieldB == 'java.util.Date') && validateDate(fieldA))
> >> etc....
> >>          </var>
> >>        </field>
> >
> > The above looks like JavaScript - because validwhen is server side
> > only. Either way validwhen doesn't cater for doing different types of
> > validation (e.g. integer, float etc) depending on a value.
> >
> >> is this possible?  I'm thinking not, so a custom validation method is
> >> probably what I need.
> >
> > Looks like it
> >
> > Niall
> >
> >> cheers
> >> Nathan
> >>
> >>
> >> Niall Pemberton wrote:
> >> > On 2/9/07, Nathan Coast <na...@codeczar.com> wrote:
> >> >> Hi,
> >> >>
> >> >> I have two fields in a form.  I believe the validwhen rule executes a
> >> >> specific rule (e.g. required) on field a based upon the value of field
> >> >> b.  The situation I have is that I need to execute different
> >> validations
> >> >> on field a determined by the value of field b.
> >> >
> >> > Yes - theres a validwhen example page in the struts-examples webapp in
> >> > the binary distro (see the validation module).
> >> >
> >> >> Is this possible client side with the latest validator framework
> >> >> (1.3.1)? or will I have to implement this functionality in the
> >> validate
> >> >> method of the form (server side)?
> >> >
> >> > validwhen is server side only.
> >> >
> >> > Its also a custom Struts validator - not part of Commons Validator so
> >> > in that respect the version of Commons Validator is not relevant.
> >> >
> >> > Niall
> >> >
> >> >> cheers
> >> >> Nathan
> >> >>
> >> >>
> >> >> ---------------------------------------------------------------------
> >> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> >> For additional commands, e-mail: user-help@struts.apache.org
> >> >>
> >> >>
> >> >
> >> > ---------------------------------------------------------------------
> >> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> > For additional commands, e-mail: user-help@struts.apache.org
> >> >
> >> >
> >> >
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> For additional commands, e-mail: user-help@struts.apache.org
> >>
> >>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: type validwhen validation

Posted by Nathan Coast <na...@codeczar.com>.
Hi Niall,

thanks again for the quick response.

I've been looking at the struts and validator source and have a couple 
of ideas for a solution to my problem.

1) just create an appropriate validator and execute it.  Simpler but 
doesn't 'feel' as good.

IntegerValidator validator = IntegerValidator.getInstance();

       Integer fooInteger = validator.validate(fooString, "#,##0.00", 
Locale.GERMAN);
       if (fooInteger == null) {
           // error...not a valid Integer
           return;
       }

2) get the existing validator for the form and add the appropriate 
validator, so all of the form validations are executed i.e. from the 
config and additional validations.

Validator validator = Resources.initValidator(validationKey,
                       this,
                       application, request,
                       errors, page);
*pseudocode*
validator.add(fooBarValidator,..,...,..)
validator.validate();

this feels better as the existing framework is responsible for executing 
all validations, assembling cumulative errors and creating localised 
messages.  I'm just not sure it's possible to add validations.

I think I may have to clone form validators so that adding validations 
to the validator doesn't affect subsequent validations on forms with the 
same name.

any chance you can point me in the right direction before I waste 2 days 
trying something impossible :)

cheers
Nathan


Niall Pemberton wrote:
> On 2/9/07, Nathan Coast <na...@codeczar.com> wrote:
>> thanks Niall,
>>
>> I've had a look at the examples and I'm not sure they demonstrate what I
>> need.  The examples all seem to be simple boolean expressions e.g.
>>
>>        <field property="address1" depends="validwhen">
>>          <arg key="validWhenForm.address1" />
>>          <var>
>>            <var-name>test</var-name>
>>            <var-value>( (*this* != null) or
>>                       ( (address2 == null) and
>>                       ( (city == null) and
>>                         (zip == null) )))</var-value>
>>          </var>
>>        </field>
>>
>> I have a select (fieldB) that determines the type of fieldA. So the
>> validation to operate on fieldA depends on the value of fieldB.
>>
>>        <field property="fieldA" depends="validwhen,required">
>>          <arg key="form.fieldA" />
>>          <var>
>>            <var-name>test</var-name>
>>            <var-value>
>>       ((fieldB == 'java.lang.Integer') && validateInteger(fieldA))
>>    || ((fieldB == 'java.lang.Float') && validateFloat(fieldA))
>>    || ((fieldB == 'java.util.Date') && validateDate(fieldA))
>> etc....
>>          </var>
>>        </field>
> 
> The above looks like JavaScript - because validwhen is server side
> only. Either way validwhen doesn't cater for doing different types of
> validation (e.g. integer, float etc) depending on a value.
> 
>> is this possible?  I'm thinking not, so a custom validation method is
>> probably what I need.
> 
> Looks like it
> 
> Niall
> 
>> cheers
>> Nathan
>>
>>
>> Niall Pemberton wrote:
>> > On 2/9/07, Nathan Coast <na...@codeczar.com> wrote:
>> >> Hi,
>> >>
>> >> I have two fields in a form.  I believe the validwhen rule executes a
>> >> specific rule (e.g. required) on field a based upon the value of field
>> >> b.  The situation I have is that I need to execute different 
>> validations
>> >> on field a determined by the value of field b.
>> >
>> > Yes - theres a validwhen example page in the struts-examples webapp in
>> > the binary distro (see the validation module).
>> >
>> >> Is this possible client side with the latest validator framework
>> >> (1.3.1)? or will I have to implement this functionality in the 
>> validate
>> >> method of the form (server side)?
>> >
>> > validwhen is server side only.
>> >
>> > Its also a custom Struts validator - not part of Commons Validator so
>> > in that respect the version of Commons Validator is not relevant.
>> >
>> > Niall
>> >
>> >> cheers
>> >> Nathan
>> >>
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> >> For additional commands, e-mail: user-help@struts.apache.org
>> >>
>> >>
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> > For additional commands, e-mail: user-help@struts.apache.org
>> >
>> >
>> >
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: type validwhen validation

Posted by Niall Pemberton <ni...@gmail.com>.
On 2/9/07, Nathan Coast <na...@codeczar.com> wrote:
> thanks Niall,
>
> I've had a look at the examples and I'm not sure they demonstrate what I
> need.  The examples all seem to be simple boolean expressions e.g.
>
>        <field property="address1" depends="validwhen">
>          <arg key="validWhenForm.address1" />
>          <var>
>            <var-name>test</var-name>
>            <var-value>( (*this* != null) or
>                       ( (address2 == null) and
>                       ( (city == null) and
>                         (zip == null) )))</var-value>
>          </var>
>        </field>
>
> I have a select (fieldB) that determines the type of fieldA. So the
> validation to operate on fieldA depends on the value of fieldB.
>
>        <field property="fieldA" depends="validwhen,required">
>          <arg key="form.fieldA" />
>          <var>
>            <var-name>test</var-name>
>            <var-value>
>       ((fieldB == 'java.lang.Integer') && validateInteger(fieldA))
>    || ((fieldB == 'java.lang.Float') && validateFloat(fieldA))
>    || ((fieldB == 'java.util.Date') && validateDate(fieldA))
> etc....
>          </var>
>        </field>

The above looks like JavaScript - because validwhen is server side
only. Either way validwhen doesn't cater for doing different types of
validation (e.g. integer, float etc) depending on a value.

> is this possible?  I'm thinking not, so a custom validation method is
> probably what I need.

Looks like it

Niall

> cheers
> Nathan
>
>
> Niall Pemberton wrote:
> > On 2/9/07, Nathan Coast <na...@codeczar.com> wrote:
> >> Hi,
> >>
> >> I have two fields in a form.  I believe the validwhen rule executes a
> >> specific rule (e.g. required) on field a based upon the value of field
> >> b.  The situation I have is that I need to execute different validations
> >> on field a determined by the value of field b.
> >
> > Yes - theres a validwhen example page in the struts-examples webapp in
> > the binary distro (see the validation module).
> >
> >> Is this possible client side with the latest validator framework
> >> (1.3.1)? or will I have to implement this functionality in the validate
> >> method of the form (server side)?
> >
> > validwhen is server side only.
> >
> > Its also a custom Struts validator - not part of Commons Validator so
> > in that respect the version of Commons Validator is not relevant.
> >
> > Niall
> >
> >> cheers
> >> Nathan
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> For additional commands, e-mail: user-help@struts.apache.org
> >>
> >>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: type validwhen validation

Posted by Nathan Coast <na...@codeczar.com>.
thanks Niall,

I've had a look at the examples and I'm not sure they demonstrate what I 
need.  The examples all seem to be simple boolean expressions e.g.

       <field property="address1" depends="validwhen">
         <arg key="validWhenForm.address1" />
         <var>
           <var-name>test</var-name>
           <var-value>( (*this* != null) or
                      ( (address2 == null) and
                      ( (city == null) and
                        (zip == null) )))</var-value>
         </var>
       </field>

I have a select (fieldB) that determines the type of fieldA. So the 
validation to operate on fieldA depends on the value of fieldB.

       <field property="fieldA" depends="validwhen,required">
         <arg key="form.fieldA" />
         <var>
           <var-name>test</var-name>
           <var-value>
      ((fieldB == 'java.lang.Integer') && validateInteger(fieldA))
   || ((fieldB == 'java.lang.Float') && validateFloat(fieldA))
   || ((fieldB == 'java.util.Date') && validateDate(fieldA))
etc....
         </var>
       </field>

is this possible?  I'm thinking not, so a custom validation method is 
probably what I need.

cheers
Nathan


Niall Pemberton wrote:
> On 2/9/07, Nathan Coast <na...@codeczar.com> wrote:
>> Hi,
>>
>> I have two fields in a form.  I believe the validwhen rule executes a
>> specific rule (e.g. required) on field a based upon the value of field
>> b.  The situation I have is that I need to execute different validations
>> on field a determined by the value of field b.
> 
> Yes - theres a validwhen example page in the struts-examples webapp in
> the binary distro (see the validation module).
> 
>> Is this possible client side with the latest validator framework
>> (1.3.1)? or will I have to implement this functionality in the validate
>> method of the form (server side)?
> 
> validwhen is server side only.
> 
> Its also a custom Struts validator - not part of Commons Validator so
> in that respect the version of Commons Validator is not relevant.
> 
> Niall
> 
>> cheers
>> Nathan
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: type validwhen validation

Posted by Niall Pemberton <ni...@gmail.com>.
On 2/9/07, Nathan Coast <na...@codeczar.com> wrote:
> Hi,
>
> I have two fields in a form.  I believe the validwhen rule executes a
> specific rule (e.g. required) on field a based upon the value of field
> b.  The situation I have is that I need to execute different validations
> on field a determined by the value of field b.

Yes - theres a validwhen example page in the struts-examples webapp in
the binary distro (see the validation module).

> Is this possible client side with the latest validator framework
> (1.3.1)? or will I have to implement this functionality in the validate
> method of the form (server side)?

validwhen is server side only.

Its also a custom Struts validator - not part of Commons Validator so
in that respect the version of Commons Validator is not relevant.

Niall

> cheers
> Nathan
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org