You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Zoran Avtarovski <zo...@sparecreative.com> on 2007/05/02 07:37:23 UTC
Conditional validation
Is there a way to setup conditional validation through the validation.xml
file?
I'm currently user the visitor validator method where my core user
validation properties are in a User-validation.xml file. I want to be able
to use this same file for all my user actions (register, add, update) and
just have conditional code which looks a user field to determine validation.
At the moment I'm using a combination of the valididation.xml file and the
validate method in the action.
I know the following doesn't work, but can I have something like this:
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator
1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<validator type="expression">
<param name="expression">
user.action.equals('insert') || user.action.equals('register')</param>
<!-- only validate these fileds when inserting and registering a user -->
<field name="user.password">
<field-validator type="requiredstring">
<message key="user.password.empty"/>
</field-validator>
</field>
<field name="user.confirmPassword">
<field-validator type="requiredstring">
<message key="user.confirmPassword.empty"/>
</field-validator>
</field>
</validator>
<!-- core validated fields -->
<field name="user.name">
<field-validator type="requiredstring">
<message key="user.name.empty">
resource not found</message>
</field-validator>
</field>
<field name="user.email">
<field-validator type="requiredstring"
short-circuit="true">
<message key="user.email.empty"/>
</field-validator>
<field-validator type="email">
<message key="user.email.invalid"/>
</field-validator>
</field>
<field name="user.phone">
<field-validator type="stringlength">
<param name="minLength">10</param>
<message key="user.phone.length"/>
</field-validator>
</field>
<field name="user.city">
<field-validator type="requiredstring">
<message key="user.city.empty"/>
</field-validator>
</field>
Z.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Conditional validation
Posted by j alex <st...@gmail.com>.
I'm still wondering how to do a pure flag check alone using validators
- i.ei need to check a guard expression before firing actual
validations on the
field ; and if the guard expression fails - the validations must simply be
skipped (as if the field didn't exist) and move on to next page - without
showing any error message.
This is somewhat similar to shortcircuiting, but shortcircuit looks for the
presence of errors to stop remaining validators - and in the scenario i
described , it's not really an error, but a guard condition.
-Joseph
On 8/13/07, mraible <ma...@raibledesigns.com> wrote:
>
>
> I haven't tried working with Struts 2's annotations for validation yet, so
> I'm unable to answer this question. In a week, things might be different.
> ;-)
>
> Matt
>
>
> strutstwouser wrote:
> >
> > Hi Matt,
> >
> > Can you please tell me what's needed to use this validator using
> > annotations alone? . Also, i need a simple conditional validator - ie a
> > field needs to be validated only if a prerequisite condition is
> satisfied,
> > else the validations on it must be skipped and no error must be added
> > (since this is just a condition check).
> >
> > Example : if appname is to be validated only if appid < 100, i will give
> > the inverse expression in ConditionalFieldValidator with shortCircuit
> true
> > and the subsequent validations below it ; assumption being if the first
> > condition becomes true (i.e prerequisite not satisfied), then dont
> > validate further.
> >
> > The annotation i need would look like :
> >
> > @ConditionalFieldValidator(fieldName = "app.appname", expression =
> > "app.appid > 100", message = "", shortCircuit=true)
> > @StringLengthFieldValidator(fieldName = "app.appname" message = "App
> Name
> > must be gt 5 chars", minLength = "5", maxLength = "12")
> >
> > I don't know how to make Struts 2 "see" this custom validator.
> >
> > Thanks,
> > Joseph
> >
> >
> >
> > mraible wrote:
> >>
> >> I figured out how to do this - posting here so others will benefit.
> >>
> >> 1. Create a new ConditionalVisitorFieldValidator.java:
> >>
> >> public class ConditionalVisitorFieldValidator extends
> >> VisitorFieldValidator
> >> {
> >> private String expression;
> >>
> >> public void setExpression(String expression)
> >> {
> >> this.expression = expression;
> >> }
> >>
> >> public String getExpression()
> >> {
> >> return expression;
> >> }
> >>
> >> /**
> >> * If expression evaluates to true, invoke visitor validation.
> >> * @param object the object being validated
> >> * @throws ValidationException
> >> */
> >> public void validate(Object object) throws ValidationException
> >> {
> >> if (validateExpression(object))
> >> {
> >> super.validate(object);
> >> }
> >> }
> >>
> >> /**
> >> * Validate the expression contained in the "expression" paramter.
> >> * @param object the object you're validating
> >> * @return true if expression evaluates to true (implying a
> validation
> >> failure)
> >> * @throws ValidationException if anything goes wrong
> >> */
> >> public boolean validateExpression(Object object) throws
> >> ValidationException
> >> {
> >> Boolean answer = Boolean.FALSE;
> >> Object obj = null;
> >>
> >> try
> >> {
> >> obj = getFieldValue(expression, object);
> >> }
> >> catch (ValidationException e)
> >> {
> >> throw e;
> >> }
> >> catch (Exception e)
> >> {
> >> // let this pass, but it will be logged right below
> >> }
> >>
> >> if ((obj != null) && (obj instanceof Boolean))
> >> {
> >> answer = (Boolean) obj;
> >> }
> >> else
> >> {
> >> log.warn("Got result of " + obj + " when trying to get
> Boolean.");
> >> }
> >>
> >> return answer;
> >> }
> >> }
> >>
> >> 2. Add it to your validators.xml:
> >>
> >> <validator name="conditionalvisitor"
> >> class="com...validation.ConditionalVisitorFieldValidator"/>
> >>
> >> 3. Write your validation rule:
> >>
> >> <field name="colleaguePosition">
> >> <field-validator type="fieldexpression" short-circuit="true">
> >> reason == 'colleague' and colleaguePositionID == '_CHOOSE_'
> >> <message>You must choose a position where you worked with this
> >> person, or choose "Other..."</message>
> >> </field-validator>
> >> <field-validator type="conditionalvisitor">
> >> reason == 'colleague' and colleaguePositionID == 'OTHER'
> >> <message/>
> >> </field-validator>
> >> </field>
> >>
> >> Hope this helps,
> >>
> >> Matt
> >>
> >>
> >> mraible wrote:
> >>>
> >>> I need to do something similar - is it possible to have conditional
> >>> visitor validation in Struts 2? AFAICT, it isn't.
> >>>
> >>> Basically, I'd like to have a couple of validation rules for a
> >>> drop-down. One rule is that the user must select at least one choice
> >>> when the drop-down has its radio button selected:
> >>>
> >>> <field-validator type="fieldexpression" short-circuit="true">
> >>> reason != 'partner' or (reason == 'partner' and
> >>> partnerPositionID != '_CHOOSE_')
> >>> <message>You must choose a position where you worked with this
> >>> person, or choose "Other..."</message>
> >>> </field-validator>
> >>>
> >>> This works. Now I want to require a number of fields if the person
> >>> selects the "Other..." option. The validation syntax starts to get
> >>> complicated at this point. I'd like to do something like:
> >>>
> >>> reason != 'partner' or (reason == 'partner' and partnerPositionID !=
> >>> 'OTHER') -> kick in visitor validation.
> >>>
> >>> I still think the above syntax is confusing (ref
> >>> http://tinyurl.com/2htw2k), I'd much rather write something like:
> >>>
> >>> reason == 'partner' and partnerPositionID == 'OTHER' -> show message
> >>>
> >>> I'm guessing it's possible to write my own FieldExpressionValidator
> that
> >>> inverses the true/false outcome?
> >>>
> >>> Why do I need conditional visitor validation?
> >>>
> >>> I'm trying to create a "component" that can be re-used in the backend
> >>> (model object w/ its own validation rules) and on the front-end (using
> >>> JSP tag files or the s:component tag).
> >>>
> >>> If there's an easier way to do this, please let me know. Of course, I
> >>> could use JSF/Wicket/Tapestry - and that might be the outcome if this
> is
> >>> not possible.
> >>>
> >>> Thanks,
> >>>
> >>> Matt
> >>>
> >>>
> >>> Sparecreative wrote:
> >>>>
> >>>> Is there a way to setup conditional validation through the
> >>>> validation.xml
> >>>> file?
> >>>>
> >>>> I'm currently user the visitor validator method where my core user
> >>>> validation properties are in a User-validation.xml file. I want to be
> >>>> able
> >>>> to use this same file for all my user actions (register, add, update)
> >>>> and
> >>>> just have conditional code which looks a user field to determine
> >>>> validation.
> >>>>
> >>>> At the moment I'm using a combination of the valididation.xml file
> and
> >>>> the
> >>>> validate method in the action.
> >>>>
> >>>> I know the following doesn't work, but can I have something like
> this:
> >>>>
> >>>> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator
> >>>> 1.0.2//EN"
> >>>> "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
> >>>> <validators>
> >>>>
> >>>> <validator type="expression">
> >>>>
> >>>> user.action.equals('insert') || user.action.equals('register')
> >>>> <!-- only validate these fileds when inserting and registering a
> user
> >>>> -->
> >>>> <field name="user.password">
> >>>> <field-validator type="requiredstring">
> >>>> <message key="user.password.empty"/>
> >>>> </field-validator>
> >>>> </field>
> >>>> <field name="user.confirmPassword">
> >>>> <field-validator type="requiredstring">
> >>>> <message key="user.confirmPassword.empty"/>
> >>>> </field-validator>
> >>>> </field>
> >>>> </validator>
> >>>> <!-- core validated fields -->
> >>>> <field name="user.name">
> >>>> <field-validator type="requiredstring">
> >>>> <message key="user.name.empty">
> >>>> resource not found</message>
> >>>> </field-validator>
> >>>> </field>
> >>>> <field name="user.email">
> >>>> <field-validator type="requiredstring"
> >>>> short-circuit="true">
> >>>> <message key="user.email.empty"/>
> >>>> </field-validator>
> >>>> <field-validator type="email">
> >>>> <message key="user.email.invalid"/>
> >>>> </field-validator>
> >>>> </field>
> >>>> <field name="user.phone">
> >>>> <field-validator type="stringlength">
> >>>> 10
> >>>> <message key="user.phone.length"/>
> >>>> </field-validator>
> >>>> </field>
> >>>> <field name="user.city">
> >>>> <field-validator type="requiredstring">
> >>>> <message key="user.city.empty"/>
> >>>> </field-validator>
> >>>> </field>
> >>>>
> >>>>
> >>>>
> >>>> Z.
> >>>>
> >>>>
> >>>>
> >>>> ---------------------------------------------------------------------
> >>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >>>> For additional commands, e-mail: user-help@struts.apache.org
> >>>>
> >>>>
> >>>>
> >>>
> >>>
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/Conditional-validation-tf3678771.html#a12131596
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
Re: Conditional validation
Posted by mraible <ma...@raibledesigns.com>.
I haven't tried working with Struts 2's annotations for validation yet, so
I'm unable to answer this question. In a week, things might be different.
;-)
Matt
strutstwouser wrote:
>
> Hi Matt,
>
> Can you please tell me what's needed to use this validator using
> annotations alone? . Also, i need a simple conditional validator - ie a
> field needs to be validated only if a prerequisite condition is satisfied,
> else the validations on it must be skipped and no error must be added
> (since this is just a condition check).
>
> Example : if appname is to be validated only if appid < 100, i will give
> the inverse expression in ConditionalFieldValidator with shortCircuit true
> and the subsequent validations below it ; assumption being if the first
> condition becomes true (i.e prerequisite not satisfied), then dont
> validate further.
>
> The annotation i need would look like :
>
> @ConditionalFieldValidator(fieldName = "app.appname", expression =
> "app.appid > 100", message = "", shortCircuit=true)
> @StringLengthFieldValidator(fieldName = "app.appname" message = "App Name
> must be gt 5 chars", minLength = "5", maxLength = "12")
>
> I don't know how to make Struts 2 "see" this custom validator.
>
> Thanks,
> Joseph
>
>
>
> mraible wrote:
>>
>> I figured out how to do this - posting here so others will benefit.
>>
>> 1. Create a new ConditionalVisitorFieldValidator.java:
>>
>> public class ConditionalVisitorFieldValidator extends
>> VisitorFieldValidator
>> {
>> private String expression;
>>
>> public void setExpression(String expression)
>> {
>> this.expression = expression;
>> }
>>
>> public String getExpression()
>> {
>> return expression;
>> }
>>
>> /**
>> * If expression evaluates to true, invoke visitor validation.
>> * @param object the object being validated
>> * @throws ValidationException
>> */
>> public void validate(Object object) throws ValidationException
>> {
>> if (validateExpression(object))
>> {
>> super.validate(object);
>> }
>> }
>>
>> /**
>> * Validate the expression contained in the "expression" paramter.
>> * @param object the object you're validating
>> * @return true if expression evaluates to true (implying a validation
>> failure)
>> * @throws ValidationException if anything goes wrong
>> */
>> public boolean validateExpression(Object object) throws
>> ValidationException
>> {
>> Boolean answer = Boolean.FALSE;
>> Object obj = null;
>>
>> try
>> {
>> obj = getFieldValue(expression, object);
>> }
>> catch (ValidationException e)
>> {
>> throw e;
>> }
>> catch (Exception e)
>> {
>> // let this pass, but it will be logged right below
>> }
>>
>> if ((obj != null) && (obj instanceof Boolean))
>> {
>> answer = (Boolean) obj;
>> }
>> else
>> {
>> log.warn("Got result of " + obj + " when trying to get Boolean.");
>> }
>>
>> return answer;
>> }
>> }
>>
>> 2. Add it to your validators.xml:
>>
>> <validator name="conditionalvisitor"
>> class="com...validation.ConditionalVisitorFieldValidator"/>
>>
>> 3. Write your validation rule:
>>
>> <field name="colleaguePosition">
>> <field-validator type="fieldexpression" short-circuit="true">
>> reason == 'colleague' and colleaguePositionID == '_CHOOSE_'
>> <message>You must choose a position where you worked with this
>> person, or choose "Other..."</message>
>> </field-validator>
>> <field-validator type="conditionalvisitor">
>> reason == 'colleague' and colleaguePositionID == 'OTHER'
>> <message/>
>> </field-validator>
>> </field>
>>
>> Hope this helps,
>>
>> Matt
>>
>>
>> mraible wrote:
>>>
>>> I need to do something similar - is it possible to have conditional
>>> visitor validation in Struts 2? AFAICT, it isn't.
>>>
>>> Basically, I'd like to have a couple of validation rules for a
>>> drop-down. One rule is that the user must select at least one choice
>>> when the drop-down has its radio button selected:
>>>
>>> <field-validator type="fieldexpression" short-circuit="true">
>>> reason != 'partner' or (reason == 'partner' and
>>> partnerPositionID != '_CHOOSE_')
>>> <message>You must choose a position where you worked with this
>>> person, or choose "Other..."</message>
>>> </field-validator>
>>>
>>> This works. Now I want to require a number of fields if the person
>>> selects the "Other..." option. The validation syntax starts to get
>>> complicated at this point. I'd like to do something like:
>>>
>>> reason != 'partner' or (reason == 'partner' and partnerPositionID !=
>>> 'OTHER') -> kick in visitor validation.
>>>
>>> I still think the above syntax is confusing (ref
>>> http://tinyurl.com/2htw2k), I'd much rather write something like:
>>>
>>> reason == 'partner' and partnerPositionID == 'OTHER' -> show message
>>>
>>> I'm guessing it's possible to write my own FieldExpressionValidator that
>>> inverses the true/false outcome?
>>>
>>> Why do I need conditional visitor validation?
>>>
>>> I'm trying to create a "component" that can be re-used in the backend
>>> (model object w/ its own validation rules) and on the front-end (using
>>> JSP tag files or the s:component tag).
>>>
>>> If there's an easier way to do this, please let me know. Of course, I
>>> could use JSF/Wicket/Tapestry - and that might be the outcome if this is
>>> not possible.
>>>
>>> Thanks,
>>>
>>> Matt
>>>
>>>
>>> Sparecreative wrote:
>>>>
>>>> Is there a way to setup conditional validation through the
>>>> validation.xml
>>>> file?
>>>>
>>>> I'm currently user the visitor validator method where my core user
>>>> validation properties are in a User-validation.xml file. I want to be
>>>> able
>>>> to use this same file for all my user actions (register, add, update)
>>>> and
>>>> just have conditional code which looks a user field to determine
>>>> validation.
>>>>
>>>> At the moment I'm using a combination of the valididation.xml file and
>>>> the
>>>> validate method in the action.
>>>>
>>>> I know the following doesn't work, but can I have something like this:
>>>>
>>>> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator
>>>> 1.0.2//EN"
>>>> "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
>>>> <validators>
>>>>
>>>> <validator type="expression">
>>>>
>>>> user.action.equals('insert') || user.action.equals('register')
>>>> <!-- only validate these fileds when inserting and registering a user
>>>> -->
>>>> <field name="user.password">
>>>> <field-validator type="requiredstring">
>>>> <message key="user.password.empty"/>
>>>> </field-validator>
>>>> </field>
>>>> <field name="user.confirmPassword">
>>>> <field-validator type="requiredstring">
>>>> <message key="user.confirmPassword.empty"/>
>>>> </field-validator>
>>>> </field>
>>>> </validator>
>>>> <!-- core validated fields -->
>>>> <field name="user.name">
>>>> <field-validator type="requiredstring">
>>>> <message key="user.name.empty">
>>>> resource not found</message>
>>>> </field-validator>
>>>> </field>
>>>> <field name="user.email">
>>>> <field-validator type="requiredstring"
>>>> short-circuit="true">
>>>> <message key="user.email.empty"/>
>>>> </field-validator>
>>>> <field-validator type="email">
>>>> <message key="user.email.invalid"/>
>>>> </field-validator>
>>>> </field>
>>>> <field name="user.phone">
>>>> <field-validator type="stringlength">
>>>> 10
>>>> <message key="user.phone.length"/>
>>>> </field-validator>
>>>> </field>
>>>> <field name="user.city">
>>>> <field-validator type="requiredstring">
>>>> <message key="user.city.empty"/>
>>>> </field-validator>
>>>> </field>
>>>>
>>>>
>>>>
>>>> Z.
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>>> For additional commands, e-mail: user-help@struts.apache.org
>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
--
View this message in context: http://www.nabble.com/Conditional-validation-tf3678771.html#a12131596
Sent from the Struts - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Conditional validation
Posted by strutstwouser <st...@gmail.com>.
Hi Matt,
Can you please tell me what's needed to use this validator using annotations
alone? . Also, i need a simple conditional validator - ie a field needs to
be validated only if a prerequisite condition is satisfied, else the
validations on it must be skipped and no error must be added (since this is
just a condition check).
Example : if appname is to be validated only if appid < 100, i will give the
inverse expression in ConditionalFieldValidator with shortCircuit true and
the subsequent validations below it ; assumption being if the first
condition becomes true (i.e prerequisite not satisfied), then dont validate
further.
The annotation i need would look like :
@ConditionalFieldValidator(fieldName = "app.appname", expression =
"app.appid > 100", message = "", shortCircuit=true)
@StringLengthFieldValidator(fieldName = "app.appname" message = "App Name
must be gt 5 chars", minLength = "5", maxLength = "12")
I don't know how to make Struts 2 "see" this custom validator.
Thanks,
Joseph
mraible wrote:
>
> I figured out how to do this - posting here so others will benefit.
>
> 1. Create a new ConditionalVisitorFieldValidator.java:
>
> public class ConditionalVisitorFieldValidator extends
> VisitorFieldValidator
> {
> private String expression;
>
> public void setExpression(String expression)
> {
> this.expression = expression;
> }
>
> public String getExpression()
> {
> return expression;
> }
>
> /**
> * If expression evaluates to true, invoke visitor validation.
> * @param object the object being validated
> * @throws ValidationException
> */
> public void validate(Object object) throws ValidationException
> {
> if (validateExpression(object))
> {
> super.validate(object);
> }
> }
>
> /**
> * Validate the expression contained in the "expression" paramter.
> * @param object the object you're validating
> * @return true if expression evaluates to true (implying a validation
> failure)
> * @throws ValidationException if anything goes wrong
> */
> public boolean validateExpression(Object object) throws
> ValidationException
> {
> Boolean answer = Boolean.FALSE;
> Object obj = null;
>
> try
> {
> obj = getFieldValue(expression, object);
> }
> catch (ValidationException e)
> {
> throw e;
> }
> catch (Exception e)
> {
> // let this pass, but it will be logged right below
> }
>
> if ((obj != null) && (obj instanceof Boolean))
> {
> answer = (Boolean) obj;
> }
> else
> {
> log.warn("Got result of " + obj + " when trying to get Boolean.");
> }
>
> return answer;
> }
> }
>
> 2. Add it to your validators.xml:
>
> <validator name="conditionalvisitor"
> class="com...validation.ConditionalVisitorFieldValidator"/>
>
> 3. Write your validation rule:
>
> <field name="colleaguePosition">
> <field-validator type="fieldexpression" short-circuit="true">
> reason == 'colleague' and colleaguePositionID == '_CHOOSE_'
> <message>You must choose a position where you worked with this
> person, or choose "Other..."</message>
> </field-validator>
> <field-validator type="conditionalvisitor">
> reason == 'colleague' and colleaguePositionID == 'OTHER'
> <message/>
> </field-validator>
> </field>
>
> Hope this helps,
>
> Matt
>
>
> mraible wrote:
>>
>> I need to do something similar - is it possible to have conditional
>> visitor validation in Struts 2? AFAICT, it isn't.
>>
>> Basically, I'd like to have a couple of validation rules for a drop-down.
>> One rule is that the user must select at least one choice when the
>> drop-down has its radio button selected:
>>
>> <field-validator type="fieldexpression" short-circuit="true">
>> reason != 'partner' or (reason == 'partner' and partnerPositionID
>> != '_CHOOSE_')
>> <message>You must choose a position where you worked with this
>> person, or choose "Other..."</message>
>> </field-validator>
>>
>> This works. Now I want to require a number of fields if the person
>> selects the "Other..." option. The validation syntax starts to get
>> complicated at this point. I'd like to do something like:
>>
>> reason != 'partner' or (reason == 'partner' and partnerPositionID !=
>> 'OTHER') -> kick in visitor validation.
>>
>> I still think the above syntax is confusing (ref
>> http://tinyurl.com/2htw2k), I'd much rather write something like:
>>
>> reason == 'partner' and partnerPositionID == 'OTHER' -> show message
>>
>> I'm guessing it's possible to write my own FieldExpressionValidator that
>> inverses the true/false outcome?
>>
>> Why do I need conditional visitor validation?
>>
>> I'm trying to create a "component" that can be re-used in the backend
>> (model object w/ its own validation rules) and on the front-end (using
>> JSP tag files or the s:component tag).
>>
>> If there's an easier way to do this, please let me know. Of course, I
>> could use JSF/Wicket/Tapestry - and that might be the outcome if this is
>> not possible.
>>
>> Thanks,
>>
>> Matt
>>
>>
>> Sparecreative wrote:
>>>
>>> Is there a way to setup conditional validation through the
>>> validation.xml
>>> file?
>>>
>>> I'm currently user the visitor validator method where my core user
>>> validation properties are in a User-validation.xml file. I want to be
>>> able
>>> to use this same file for all my user actions (register, add, update)
>>> and
>>> just have conditional code which looks a user field to determine
>>> validation.
>>>
>>> At the moment I'm using a combination of the valididation.xml file and
>>> the
>>> validate method in the action.
>>>
>>> I know the following doesn't work, but can I have something like this:
>>>
>>> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator
>>> 1.0.2//EN"
>>> "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
>>> <validators>
>>>
>>> <validator type="expression">
>>>
>>> user.action.equals('insert') || user.action.equals('register')
>>> <!-- only validate these fileds when inserting and registering a user
>>> -->
>>> <field name="user.password">
>>> <field-validator type="requiredstring">
>>> <message key="user.password.empty"/>
>>> </field-validator>
>>> </field>
>>> <field name="user.confirmPassword">
>>> <field-validator type="requiredstring">
>>> <message key="user.confirmPassword.empty"/>
>>> </field-validator>
>>> </field>
>>> </validator>
>>> <!-- core validated fields -->
>>> <field name="user.name">
>>> <field-validator type="requiredstring">
>>> <message key="user.name.empty">
>>> resource not found</message>
>>> </field-validator>
>>> </field>
>>> <field name="user.email">
>>> <field-validator type="requiredstring"
>>> short-circuit="true">
>>> <message key="user.email.empty"/>
>>> </field-validator>
>>> <field-validator type="email">
>>> <message key="user.email.invalid"/>
>>> </field-validator>
>>> </field>
>>> <field name="user.phone">
>>> <field-validator type="stringlength">
>>> 10
>>> <message key="user.phone.length"/>
>>> </field-validator>
>>> </field>
>>> <field name="user.city">
>>> <field-validator type="requiredstring">
>>> <message key="user.city.empty"/>
>>> </field-validator>
>>> </field>
>>>
>>>
>>>
>>> Z.
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>>
>>>
>>>
>>
>>
>
>
--
View this message in context: http://www.nabble.com/Conditional-validation-tf3678771.html#a12131518
Sent from the Struts - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Conditional validation
Posted by Dave Newton <ne...@yahoo.com>.
--- stanlick <st...@gmail.com> wrote:
> <validator type="expression">
> model.password.equals(model.confirmPassword)
> <message>
> ${getText("nomatch")} ${model.password} and
> ${model.confirmPassword}
> </message>
> </validator>
I wouldn't even think that would pass DTD validation;
doesn't the expression need to be in a param tag?
d.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Conditional validation
Posted by Zoran Avtarovski <zo...@sparecreative.com>.
I'm pretty sure you need to have <param name="expression"> </param> around
your expression.
Z.
>
> Perhaps someone can order this XML to produce the desired result?
>
> <field name="model.password">
> <field-validator type="requiredstring">
> <message key="requiredstring" />
> </field-validator>
> </field>
> <field name="model.confirmPassword">
> <field-validator type="requiredstring">
> <message key="requiredstring" />
> </field-validator>
> </field>
>
>
> <validator type="expression">
>
> model.password.equals(model.confirmPassword)
>
> <message>
> ${getText("nomatch")} ${model.password} and
> ${model.confirmPassword}
> </message>
> </validator>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Conditional validation
Posted by stanlick <st...@gmail.com>.
Perhaps someone can order this XML to produce the desired result?
<field name="model.password">
<field-validator type="requiredstring">
<message key="requiredstring" />
</field-validator>
</field>
<field name="model.confirmPassword">
<field-validator type="requiredstring">
<message key="requiredstring" />
</field-validator>
</field>
<validator type="expression">
model.password.equals(model.confirmPassword)
<message>
${getText("nomatch")} ${model.password} and
${model.confirmPassword}
</message>
</validator>
--
View this message in context: http://www.nabble.com/Conditional-validation-tf3678771.html#a12234444
Sent from the Struts - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Conditional validation
Posted by j alex <st...@gmail.com>.
Can you paste the entry from validator xml -- and the line which is
flagged with the error?
>From the error message, it just looks like an ordering problem -- ie
we must have param tag before the message tag ; but i may be
oversimplifying things..
On 8/18/07, stanlick <st...@gmail.com> wrote:
>
> Matt --
>
> Did you have trouble getting your validation entries to pass the dtd rules?
> I'm using "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd" and
> getting the error The content of element type "field-validator" must match
> "(param*,message)".
>
>
>
> mraible wrote:
> >
> > I figured out how to do this - posting here so others will benefit.
> >
> > 1. Create a new ConditionalVisitorFieldValidator.java:
> >
> > public class ConditionalVisitorFieldValidator extends
> > VisitorFieldValidator
> > {
> > private String expression;
> >
> > public void setExpression(String expression)
> > {
> > this.expression = expression;
> > }
> >
> > public String getExpression()
> > {
> > return expression;
> > }
> >
> > /**
> > * If expression evaluates to true, invoke visitor validation.
> > * @param object the object being validated
> > * @throws ValidationException
> > */
> > public void validate(Object object) throws ValidationException
> > {
> > if (validateExpression(object))
> > {
> > super.validate(object);
> > }
> > }
> >
> > /**
> > * Validate the expression contained in the "expression" paramter.
> > * @param object the object you're validating
> > * @return true if expression evaluates to true (implying a validation
> > failure)
> > * @throws ValidationException if anything goes wrong
> > */
> > public boolean validateExpression(Object object) throws
> > ValidationException
> > {
> > Boolean answer = Boolean.FALSE;
> > Object obj = null;
> >
> > try
> > {
> > obj = getFieldValue(expression, object);
> > }
> > catch (ValidationException e)
> > {
> > throw e;
> > }
> > catch (Exception e)
> > {
> > // let this pass, but it will be logged right below
> > }
> >
> > if ((obj != null) && (obj instanceof Boolean))
> > {
> > answer = (Boolean) obj;
> > }
> > else
> > {
> > log.warn("Got result of " + obj + " when trying to get Boolean.");
> > }
> >
> > return answer;
> > }
> > }
> >
> > 2. Add it to your validators.xml:
> >
> > <validator name="conditionalvisitor"
> > class="com...validation.ConditionalVisitorFieldValidator"/>
> >
> > 3. Write your validation rule:
> >
> > <field name="colleaguePosition">
> > <field-validator type="fieldexpression" short-circuit="true">
> > reason == 'colleague' and colleaguePositionID == '_CHOOSE_'
> > <message>You must choose a position where you worked with this
> > person, or choose "Other..."</message>
> > </field-validator>
> > <field-validator type="conditionalvisitor">
> > reason == 'colleague' and colleaguePositionID == 'OTHER'
> > <message/>
> > </field-validator>
> > </field>
> >
> > Hope this helps,
> >
> > Matt
> >
> >
> > mraible wrote:
> >>
> >> I need to do something similar - is it possible to have conditional
> >> visitor validation in Struts 2? AFAICT, it isn't.
> >>
> >> Basically, I'd like to have a couple of validation rules for a drop-down.
> >> One rule is that the user must select at least one choice when the
> >> drop-down has its radio button selected:
> >>
> >> <field-validator type="fieldexpression" short-circuit="true">
> >> reason != 'partner' or (reason == 'partner' and partnerPositionID
> >> != '_CHOOSE_')
> >> <message>You must choose a position where you worked with this
> >> person, or choose "Other..."</message>
> >> </field-validator>
> >>
> >> This works. Now I want to require a number of fields if the person
> >> selects the "Other..." option. The validation syntax starts to get
> >> complicated at this point. I'd like to do something like:
> >>
> >> reason != 'partner' or (reason == 'partner' and partnerPositionID !=
> >> 'OTHER') -> kick in visitor validation.
> >>
> >> I still think the above syntax is confusing (ref
> >> http://tinyurl.com/2htw2k), I'd much rather write something like:
> >>
> >> reason == 'partner' and partnerPositionID == 'OTHER' -> show message
> >>
> >> I'm guessing it's possible to write my own FieldExpressionValidator that
> >> inverses the true/false outcome?
> >>
> >> Why do I need conditional visitor validation?
> >>
> >> I'm trying to create a "component" that can be re-used in the backend
> >> (model object w/ its own validation rules) and on the front-end (using
> >> JSP tag files or the s:component tag).
> >>
> >> If there's an easier way to do this, please let me know. Of course, I
> >> could use JSF/Wicket/Tapestry - and that might be the outcome if this is
> >> not possible.
> >>
> >> Thanks,
> >>
> >> Matt
> >>
> >>
> >> Sparecreative wrote:
> >>>
> >>> Is there a way to setup conditional validation through the
> >>> validation.xml
> >>> file?
> >>>
> >>> I'm currently user the visitor validator method where my core user
> >>> validation properties are in a User-validation.xml file. I want to be
> >>> able
> >>> to use this same file for all my user actions (register, add, update)
> >>> and
> >>> just have conditional code which looks a user field to determine
> >>> validation.
> >>>
> >>> At the moment I'm using a combination of the valididation.xml file and
> >>> the
> >>> validate method in the action.
> >>>
> >>> I know the following doesn't work, but can I have something like this:
> >>>
> >>> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator
> >>> 1.0.2//EN"
> >>> "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
> >>> <validators>
> >>>
> >>> <validator type="expression">
> >>>
> >>> user.action.equals('insert') || user.action.equals('register')
> >>> <!-- only validate these fileds when inserting and registering a user
> >>> -->
> >>> <field name="user.password">
> >>> <field-validator type="requiredstring">
> >>> <message key="user.password.empty"/>
> >>> </field-validator>
> >>> </field>
> >>> <field name="user.confirmPassword">
> >>> <field-validator type="requiredstring">
> >>> <message key="user.confirmPassword.empty"/>
> >>> </field-validator>
> >>> </field>
> >>> </validator>
> >>> <!-- core validated fields -->
> >>> <field name="user.name">
> >>> <field-validator type="requiredstring">
> >>> <message key="user.name.empty">
> >>> resource not found</message>
> >>> </field-validator>
> >>> </field>
> >>> <field name="user.email">
> >>> <field-validator type="requiredstring"
> >>> short-circuit="true">
> >>> <message key="user.email.empty"/>
> >>> </field-validator>
> >>> <field-validator type="email">
> >>> <message key="user.email.invalid"/>
> >>> </field-validator>
> >>> </field>
> >>> <field name="user.phone">
> >>> <field-validator type="stringlength">
> >>> 10
> >>> <message key="user.phone.length"/>
> >>> </field-validator>
> >>> </field>
> >>> <field name="user.city">
> >>> <field-validator type="requiredstring">
> >>> <message key="user.city.empty"/>
> >>> </field-validator>
> >>> </field>
> >>>
> >>>
> >>>
> >>> Z.
> >>>
> >>>
> >>>
> >>> ---------------------------------------------------------------------
> >>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >>> For additional commands, e-mail: user-help@struts.apache.org
> >>>
> >>>
> >>>
> >>
> >>
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Conditional-validation-tf3678771.html#a12216265
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> 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: Conditional validation
Posted by stanlick <st...@gmail.com>.
Matt --
Did you have trouble getting your validation entries to pass the dtd rules?
I'm using "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd" and
getting the error The content of element type "field-validator" must match
"(param*,message)".
mraible wrote:
>
> I figured out how to do this - posting here so others will benefit.
>
> 1. Create a new ConditionalVisitorFieldValidator.java:
>
> public class ConditionalVisitorFieldValidator extends
> VisitorFieldValidator
> {
> private String expression;
>
> public void setExpression(String expression)
> {
> this.expression = expression;
> }
>
> public String getExpression()
> {
> return expression;
> }
>
> /**
> * If expression evaluates to true, invoke visitor validation.
> * @param object the object being validated
> * @throws ValidationException
> */
> public void validate(Object object) throws ValidationException
> {
> if (validateExpression(object))
> {
> super.validate(object);
> }
> }
>
> /**
> * Validate the expression contained in the "expression" paramter.
> * @param object the object you're validating
> * @return true if expression evaluates to true (implying a validation
> failure)
> * @throws ValidationException if anything goes wrong
> */
> public boolean validateExpression(Object object) throws
> ValidationException
> {
> Boolean answer = Boolean.FALSE;
> Object obj = null;
>
> try
> {
> obj = getFieldValue(expression, object);
> }
> catch (ValidationException e)
> {
> throw e;
> }
> catch (Exception e)
> {
> // let this pass, but it will be logged right below
> }
>
> if ((obj != null) && (obj instanceof Boolean))
> {
> answer = (Boolean) obj;
> }
> else
> {
> log.warn("Got result of " + obj + " when trying to get Boolean.");
> }
>
> return answer;
> }
> }
>
> 2. Add it to your validators.xml:
>
> <validator name="conditionalvisitor"
> class="com...validation.ConditionalVisitorFieldValidator"/>
>
> 3. Write your validation rule:
>
> <field name="colleaguePosition">
> <field-validator type="fieldexpression" short-circuit="true">
> reason == 'colleague' and colleaguePositionID == '_CHOOSE_'
> <message>You must choose a position where you worked with this
> person, or choose "Other..."</message>
> </field-validator>
> <field-validator type="conditionalvisitor">
> reason == 'colleague' and colleaguePositionID == 'OTHER'
> <message/>
> </field-validator>
> </field>
>
> Hope this helps,
>
> Matt
>
>
> mraible wrote:
>>
>> I need to do something similar - is it possible to have conditional
>> visitor validation in Struts 2? AFAICT, it isn't.
>>
>> Basically, I'd like to have a couple of validation rules for a drop-down.
>> One rule is that the user must select at least one choice when the
>> drop-down has its radio button selected:
>>
>> <field-validator type="fieldexpression" short-circuit="true">
>> reason != 'partner' or (reason == 'partner' and partnerPositionID
>> != '_CHOOSE_')
>> <message>You must choose a position where you worked with this
>> person, or choose "Other..."</message>
>> </field-validator>
>>
>> This works. Now I want to require a number of fields if the person
>> selects the "Other..." option. The validation syntax starts to get
>> complicated at this point. I'd like to do something like:
>>
>> reason != 'partner' or (reason == 'partner' and partnerPositionID !=
>> 'OTHER') -> kick in visitor validation.
>>
>> I still think the above syntax is confusing (ref
>> http://tinyurl.com/2htw2k), I'd much rather write something like:
>>
>> reason == 'partner' and partnerPositionID == 'OTHER' -> show message
>>
>> I'm guessing it's possible to write my own FieldExpressionValidator that
>> inverses the true/false outcome?
>>
>> Why do I need conditional visitor validation?
>>
>> I'm trying to create a "component" that can be re-used in the backend
>> (model object w/ its own validation rules) and on the front-end (using
>> JSP tag files or the s:component tag).
>>
>> If there's an easier way to do this, please let me know. Of course, I
>> could use JSF/Wicket/Tapestry - and that might be the outcome if this is
>> not possible.
>>
>> Thanks,
>>
>> Matt
>>
>>
>> Sparecreative wrote:
>>>
>>> Is there a way to setup conditional validation through the
>>> validation.xml
>>> file?
>>>
>>> I'm currently user the visitor validator method where my core user
>>> validation properties are in a User-validation.xml file. I want to be
>>> able
>>> to use this same file for all my user actions (register, add, update)
>>> and
>>> just have conditional code which looks a user field to determine
>>> validation.
>>>
>>> At the moment I'm using a combination of the valididation.xml file and
>>> the
>>> validate method in the action.
>>>
>>> I know the following doesn't work, but can I have something like this:
>>>
>>> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator
>>> 1.0.2//EN"
>>> "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
>>> <validators>
>>>
>>> <validator type="expression">
>>>
>>> user.action.equals('insert') || user.action.equals('register')
>>> <!-- only validate these fileds when inserting and registering a user
>>> -->
>>> <field name="user.password">
>>> <field-validator type="requiredstring">
>>> <message key="user.password.empty"/>
>>> </field-validator>
>>> </field>
>>> <field name="user.confirmPassword">
>>> <field-validator type="requiredstring">
>>> <message key="user.confirmPassword.empty"/>
>>> </field-validator>
>>> </field>
>>> </validator>
>>> <!-- core validated fields -->
>>> <field name="user.name">
>>> <field-validator type="requiredstring">
>>> <message key="user.name.empty">
>>> resource not found</message>
>>> </field-validator>
>>> </field>
>>> <field name="user.email">
>>> <field-validator type="requiredstring"
>>> short-circuit="true">
>>> <message key="user.email.empty"/>
>>> </field-validator>
>>> <field-validator type="email">
>>> <message key="user.email.invalid"/>
>>> </field-validator>
>>> </field>
>>> <field name="user.phone">
>>> <field-validator type="stringlength">
>>> 10
>>> <message key="user.phone.length"/>
>>> </field-validator>
>>> </field>
>>> <field name="user.city">
>>> <field-validator type="requiredstring">
>>> <message key="user.city.empty"/>
>>> </field-validator>
>>> </field>
>>>
>>>
>>>
>>> Z.
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>>
>>>
>>>
>>
>>
>
>
--
View this message in context: http://www.nabble.com/Conditional-validation-tf3678771.html#a12216265
Sent from the Struts - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Conditional validation
Posted by Ted Husted <hu...@apache.org>.
I added this as
* http://jira.opensymphony.com/browse/XW-565
-Ted.
On 8/3/07, mraible <ma...@raibledesigns.com> wrote:
>
> I figured out how to do this - posting here so others will benefit.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Conditional validation
Posted by mraible <ma...@raibledesigns.com>.
I figured out how to do this - posting here so others will benefit.
1. Create a new ConditionalVisitorFieldValidator.java:
public class ConditionalVisitorFieldValidator extends VisitorFieldValidator
{
private String expression;
public void setExpression(String expression)
{
this.expression = expression;
}
public String getExpression()
{
return expression;
}
/**
* If expression evaluates to true, invoke visitor validation.
* @param object the object being validated
* @throws ValidationException
*/
public void validate(Object object) throws ValidationException
{
if (validateExpression(object))
{
super.validate(object);
}
}
/**
* Validate the expression contained in the "expression" paramter.
* @param object the object you're validating
* @return true if expression evaluates to true (implying a validation
failure)
* @throws ValidationException if anything goes wrong
*/
public boolean validateExpression(Object object) throws
ValidationException
{
Boolean answer = Boolean.FALSE;
Object obj = null;
try
{
obj = getFieldValue(expression, object);
}
catch (ValidationException e)
{
throw e;
}
catch (Exception e)
{
// let this pass, but it will be logged right below
}
if ((obj != null) && (obj instanceof Boolean))
{
answer = (Boolean) obj;
}
else
{
log.warn("Got result of " + obj + " when trying to get Boolean.");
}
return answer;
}
}
2. Add it to your validators.xml:
<validator name="conditionalvisitor"
class="com...validation.ConditionalVisitorFieldValidator"/>
3. Write your validation rule:
<field name="colleaguePosition">
<field-validator type="fieldexpression" short-circuit="true">
reason == 'colleague' and colleaguePositionID == '_CHOOSE_'
<message>You must choose a position where you worked with this person,
or choose "Other..."</message>
</field-validator>
<field-validator type="conditionalvisitor">
reason == 'colleague' and colleaguePositionID == 'OTHER'
<message/>
</field-validator>
</field>
Hope this helps,
Matt
mraible wrote:
>
> I need to do something similar - is it possible to have conditional
> visitor validation in Struts 2? AFAICT, it isn't.
>
> Basically, I'd like to have a couple of validation rules for a drop-down.
> One rule is that the user must select at least one choice when the
> drop-down has its radio button selected:
>
> <field-validator type="fieldexpression" short-circuit="true">
> reason != 'partner' or (reason == 'partner' and partnerPositionID
> != '_CHOOSE_')
> <message>You must choose a position where you worked with this
> person, or choose "Other..."</message>
> </field-validator>
>
> This works. Now I want to require a number of fields if the person selects
> the "Other..." option. The validation syntax starts to get complicated at
> this point. I'd like to do something like:
>
> reason != 'partner' or (reason == 'partner' and partnerPositionID !=
> 'OTHER') -> kick in visitor validation.
>
> I still think the above syntax is confusing (ref
> http://tinyurl.com/2htw2k), I'd much rather write something like:
>
> reason == 'partner' and partnerPositionID == 'OTHER' -> show message
>
> I'm guessing it's possible to write my own FieldExpressionValidator that
> inverses the true/false outcome?
>
> Why do I need conditional visitor validation?
>
> I'm trying to create a "component" that can be re-used in the backend
> (model object w/ its own validation rules) and on the front-end (using JSP
> tag files or the s:component tag).
>
> If there's an easier way to do this, please let me know. Of course, I
> could use JSF/Wicket/Tapestry - and that might be the outcome if this is
> not possible.
>
> Thanks,
>
> Matt
>
>
> Sparecreative wrote:
>>
>> Is there a way to setup conditional validation through the validation.xml
>> file?
>>
>> I'm currently user the visitor validator method where my core user
>> validation properties are in a User-validation.xml file. I want to be
>> able
>> to use this same file for all my user actions (register, add, update) and
>> just have conditional code which looks a user field to determine
>> validation.
>>
>> At the moment I'm using a combination of the valididation.xml file and
>> the
>> validate method in the action.
>>
>> I know the following doesn't work, but can I have something like this:
>>
>> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator
>> 1.0.2//EN"
>> "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
>> <validators>
>>
>> <validator type="expression">
>>
>> user.action.equals('insert') || user.action.equals('register')
>> <!-- only validate these fileds when inserting and registering a user
>> -->
>> <field name="user.password">
>> <field-validator type="requiredstring">
>> <message key="user.password.empty"/>
>> </field-validator>
>> </field>
>> <field name="user.confirmPassword">
>> <field-validator type="requiredstring">
>> <message key="user.confirmPassword.empty"/>
>> </field-validator>
>> </field>
>> </validator>
>> <!-- core validated fields -->
>> <field name="user.name">
>> <field-validator type="requiredstring">
>> <message key="user.name.empty">
>> resource not found</message>
>> </field-validator>
>> </field>
>> <field name="user.email">
>> <field-validator type="requiredstring"
>> short-circuit="true">
>> <message key="user.email.empty"/>
>> </field-validator>
>> <field-validator type="email">
>> <message key="user.email.invalid"/>
>> </field-validator>
>> </field>
>> <field name="user.phone">
>> <field-validator type="stringlength">
>> 10
>> <message key="user.phone.length"/>
>> </field-validator>
>> </field>
>> <field name="user.city">
>> <field-validator type="requiredstring">
>> <message key="user.city.empty"/>
>> </field-validator>
>> </field>
>>
>>
>>
>> Z.
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>>
>
>
--
View this message in context: http://www.nabble.com/Conditional-validation-tf3678771.html#a11990883
Sent from the Struts - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Spring 2.0 Session scope beans
Posted by Chris Pratt <th...@gmail.com>.
Your best bet is to write it as an Interceptor and add it to your
default interceptor stack.
(*Chris*)
On 8/1/07, Jiang, Jane (NIH/NCI) [C] <ji...@mail.nih.gov> wrote:
> Hi,
>
> I am experimenting with the Session scope beans. I was able to define
> on and verify that one bean is created and associated with each session.
>
> Could I get access to the session from the bean? I plan to use this
> bean to store user information. I need to get the user id from the
> request header and populate this object.
>
> I can also check from the action and populate the bean if it is not
> initialized. But then I will have to check from many places.
>
> Thanks in advance for your help,
>
> Jane
>
>
>
> ---------------------------------------------------------------------
> 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
Spring 2.0 Session scope beans
Posted by "Jiang, Jane (NIH/NCI) [C]" <ji...@mail.nih.gov>.
Hi,
I am experimenting with the Session scope beans. I was able to define
on and verify that one bean is created and associated with each session.
Could I get access to the session from the bean? I plan to use this
bean to store user information. I need to get the user id from the
request header and populate this object.
I can also check from the action and populate the bean if it is not
initialized. But then I will have to check from many places.
Thanks in advance for your help,
Jane
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Conditional validation
Posted by mraible <ma...@raibledesigns.com>.
I need to do something similar - is it possible to have conditional visitor
validation in Struts 2? AFAICT, it isn't.
Basically, I'd like to have a couple of validation rules for a drop-down.
One rule is that the user must select at least one choice when the drop-down
has its radio button selected:
<field-validator type="fieldexpression" short-circuit="true">
reason != 'partner' or (reason == 'partner' and partnerPositionID !=
'_CHOOSE_')
<message>You must choose a position where you worked with this
person, or choose "Other..."</message>
</field-validator>
This works. Now I want to require a number of fields if the person selects
the "Other..." option. The validation syntax starts to get complicated at
this point. I'd like to do something like:
reason != 'partner' or (reason == 'partner' and partnerPositionID !=
'OTHER') -> kick in visitor validation.
I still think the above syntax is confusing (ref http://tinyurl.com/2htw2k),
I'd much rather write something like:
reason == 'partner' and partnerPositionID == 'OTHER' -> show message
I'm guessing it's possible to write my own FieldExpressionValidator that
inverses the true/false outcome?
Why do I need conditional visitor validation?
I'm trying to create a "component" that can be re-used in the backend (model
object w/ its own validation rules) and on the front-end (using JSP tag
files or the s:component tag).
If there's an easier way to do this, please let me know. Of course, I could
use JSF/Wicket/Tapestry - and that might be the outcome if this is not
possible.
Thanks,
Matt
Sparecreative wrote:
>
> Is there a way to setup conditional validation through the validation.xml
> file?
>
> I'm currently user the visitor validator method where my core user
> validation properties are in a User-validation.xml file. I want to be able
> to use this same file for all my user actions (register, add, update) and
> just have conditional code which looks a user field to determine
> validation.
>
> At the moment I'm using a combination of the valididation.xml file and the
> validate method in the action.
>
> I know the following doesn't work, but can I have something like this:
>
> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator
> 1.0.2//EN"
> "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
> <validators>
>
> <validator type="expression">
>
> user.action.equals('insert') || user.action.equals('register')
> <!-- only validate these fileds when inserting and registering a user -->
> <field name="user.password">
> <field-validator type="requiredstring">
> <message key="user.password.empty"/>
> </field-validator>
> </field>
> <field name="user.confirmPassword">
> <field-validator type="requiredstring">
> <message key="user.confirmPassword.empty"/>
> </field-validator>
> </field>
> </validator>
> <!-- core validated fields -->
> <field name="user.name">
> <field-validator type="requiredstring">
> <message key="user.name.empty">
> resource not found</message>
> </field-validator>
> </field>
> <field name="user.email">
> <field-validator type="requiredstring"
> short-circuit="true">
> <message key="user.email.empty"/>
> </field-validator>
> <field-validator type="email">
> <message key="user.email.invalid"/>
> </field-validator>
> </field>
> <field name="user.phone">
> <field-validator type="stringlength">
> 10
> <message key="user.phone.length"/>
> </field-validator>
> </field>
> <field name="user.city">
> <field-validator type="requiredstring">
> <message key="user.city.empty"/>
> </field-validator>
> </field>
>
>
>
> Z.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>
--
View this message in context: http://www.nabble.com/Conditional-validation-tf3678771.html#a11950499
Sent from the Struts - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org