You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "jignesh(india)" <ji...@aspl.in> on 2007/11/02 08:04:42 UTC

Struts Type Conversion Problem

Hello,
    I have read your conversion here,
    I am having a strange problem in my converter.

     case 1:-
    JSP
    <s:textfield name="myDate"/>

    classname-conversion.properties
    myDate = tutorial.MyTypeConverter
   
    case 2:-
    JSP
    <s:textfield name="user.myDate"/>

    classname-conversion.properties
    user.myDate = tutorial.MyTypeConverter
   
Now the problem here is that if i use case 1 i am successfully able to call
the convertToString(Map arg0, Object arg1) but in case 2 i am not able to
call convertToString(Map arg0, Object arg1). 

Can anybody understand me ?

Thanks in advanced.
-- 
View this message in context: http://www.nabble.com/Struts-Type-Conversion-Problem-tf4735856.html#a13543074
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 Gary Affonso <gl...@greywether.com>.
naveen472-m@yahoo.co.in wrote:
> Can anyone tell me how to do conditional validation in struts 2?
> 
> In one form I want validations for a particular category (say Category XYZ) only if the candidate in that. If he is NOT Category XYZ then by checking a checkbox he should be able to skip the validations for that. I am not able find any solution to this problem. I guess there are no provisions for conditional validation in Struts 2. Struts 1 had a <validwhen> tag. But I am not able to find any equivalent tag for Struts 2.

This was true for WebWork, I'm assuming it remains true for s2...

If you're using the xml-based validation framework then you can use the 
"expression" validator to do validations on as many fields/conditions 
(and their combinations) as you want.

Here's an example:

     <validator type="expression">
         <param name="expression">
             ! (
                 ( chosenAddressId.intValue() eq -1 )
                 and
                 ( regionId.intValue() eq -1 )
                 and
                 ( ( countryId.intValue() eq 1 ) or ( 
countryId.intValue() eq 2 ) )
                )
         </param>
         <message>
If you choose to use a custom address and choose US or Canada as your 
country, you must choose a state/region from the drop-down menu.</message>
     </validator>

The HTML that the above code validates has three fields.  A radio button 
with a "custom" address choice, a country drop-down (select) and a 
state/region drop-down (the drop-downs have IDs for values).

The above validation says that a region choice is required if the user 
chooses the "custom address" radio button AND they choose US or Canada 
for the country drop-down.

Be warned, though.  Writing and debugging complex OGNL validation 
expressions absolutely sucks (IMO).  It's confusing to get the logic 
right (you'll notice that I've written the checks above as positive 
statements and then reversed the whole expression.  I find writing the 
checks in the negative to be way less intuitive) and very, very 
difficult to debug (hint, while developing these you can include field 
references in the message (I think it's a ${} syntax but I can't 
remember) to try to get some debugging info back out to yourself.)

Your other choices are...

1) Just do validation at the top of your Action's execute method.
Your Action's execute method gets called after all the interceptors have 
run.  Meaning that all the form values will have been applied to the 
Action's (or domain objects) properties.

So the first thing you do at the top of your execute method is a bunch 
of validity checks on those properties.  If those checks fail, you just 
some sort of message to the Action's FieldErrors/ActionErrors 
(addActionError, addFieldError, etc.) lists and return a 
failure-oriented result code (commonly INPUT).  Same end-result as if 
you'd used the xml framework.

(Note: I think actions get their fieldError and ActionError lists by 
sublclassing ActionSupport which implements validationAware.  So if your 
actions are not doing either you'll want to check that out.)

2) Implement the "validatable" interface
Not a whole lot different than the above but in this case you put your 
validation logic in a "validate" method in your action.

---

If you want to do the same validation for a domain object in several 
different actions, Webwork (and presumably s2) has the option to shift 
XML-based validation to be domain-object-based (instead of being 
action-based).  Basically you write a -validation.xml file for the 
*domain* object not each of the actions and then you tell the action to 
delegate the validation back to the domain object.  I've completely 
forgotten how to do this but if that interests you you should be able to 
find it in the docs easily enough.

In general, what you want to do (multi-field validation) is common, 
directly supported, and fairly easy.  You just need to pick which route 
you want to go.  I'd probably recommend doing this in the "validate" 
method.  You get to use java, and it's absolutely clear that this is 
"validation" logic (because it's in a dedicated validate method).

That is, I'd recommend it if you're OK tying your validation logic to 
the actions.  If you want validation logic you can share across more 
tiers of your application that's an entirely separate discussion.

WARNING: the above was true for WebWork (2.x).  Or mostly.  It's been 
awhile since I've looked at any of this in depth and I'm going from 
memory here.  (and right-before-bed memory at that).  But the above 
should be correct enough to get you headed in the right direction.

Good luck!

- Gary




:-)


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


Conditional Validation

Posted by na...@yahoo.co.in.
Can anyone tell me how to do conditional validation in struts 2?

In one form I want validations for a particular category (say Category XYZ) only if the candidate in that. If he is NOT Category XYZ then by checking a checkbox he should be able to skip the validations for that. I am not able find any solution to this problem. I guess there are no provisions for conditional validation in Struts 2. Struts 1 had a <validwhen> tag. But I am not able to find any equivalent tag for Struts 2.

Also is there any plugin available for struts 2 for eclipse?

Regards
Naveen

       
---------------------------------
 Now you can chat without downloading messenger. Click here to know how.

RE: Struts Type Conversion Problem

Posted by "jignesh(india)" <ji...@aspl.in>.
For the conversion to work with user.myDate, we need to create a properties
file named User-conversion.properties and locate it in the directory
where the user class resides (assuming your User class is an
org.appfuse.model.User).  Then inside that file, we have to put:

mydate = tutorial.MyTypeConverter

This assumes of course that our "MyTypeConverter" class is in a package
named "tutorial". 

Shannon, Andrew-2 wrote:
> 
> I also submitted an email yesterday related to type conversion and your
> observation below was very helpful.  The fact that your case 2 doesn't
> work also describes my problem (submitted as "interceptor and converter
> cycle").
> 
> For now I guess I'll go with case 1 at the Action class level in order
> to achieve validation, and set the date on my user object separately.
> 
> Unfortunately I cannot explain why case 2 doesn't work, although I wish
> it did.
> 
> Thanks a bunch,
> Andrew 
> 
> -----Original Message-----
> From: jignesh(india) [mailto:jigneshd@aspl.in] 
> Sent: Friday, November 02, 2007 3:05 AM
> To: user@struts.apache.org
> Subject: Struts Type Conversion Problem
> 
> 
> Hello,
>     I have read your conversion here,
>     I am having a strange problem in my converter.
> 
>      case 1:-
>     JSP
>     <s:textfield name="myDate"/>
> 
>     classname-conversion.properties
>     myDate = tutorial.MyTypeConverter
>    
>     case 2:-
>     JSP
>     <s:textfield name="user.myDate"/>
> 
>     classname-conversion.properties
>     user.myDate = tutorial.MyTypeConverter
>    
> Now the problem here is that if i use case 1 i am successfully able to
> call the convertToString(Map arg0, Object arg1) but in case 2 i am not
> able to call convertToString(Map arg0, Object arg1). 
> 
> Can anybody understand me ?
> 
> Thanks in advanced.
> --
> View this message in context:
> http://www.nabble.com/Struts-Type-Conversion-Problem-tf4735856.html#a135
> 43074
> 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
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Struts-Type-Conversion-Problem-tf4735856.html#a13559902
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: Struts Type Conversion Problem

Posted by "Shannon, Andrew" <an...@pqa.com>.
I also submitted an email yesterday related to type conversion and your
observation below was very helpful.  The fact that your case 2 doesn't
work also describes my problem (submitted as "interceptor and converter
cycle").

For now I guess I'll go with case 1 at the Action class level in order
to achieve validation, and set the date on my user object separately.

Unfortunately I cannot explain why case 2 doesn't work, although I wish
it did.

Thanks a bunch,
Andrew 

-----Original Message-----
From: jignesh(india) [mailto:jigneshd@aspl.in] 
Sent: Friday, November 02, 2007 3:05 AM
To: user@struts.apache.org
Subject: Struts Type Conversion Problem


Hello,
    I have read your conversion here,
    I am having a strange problem in my converter.

     case 1:-
    JSP
    <s:textfield name="myDate"/>

    classname-conversion.properties
    myDate = tutorial.MyTypeConverter
   
    case 2:-
    JSP
    <s:textfield name="user.myDate"/>

    classname-conversion.properties
    user.myDate = tutorial.MyTypeConverter
   
Now the problem here is that if i use case 1 i am successfully able to
call the convertToString(Map arg0, Object arg1) but in case 2 i am not
able to call convertToString(Map arg0, Object arg1). 

Can anybody understand me ?

Thanks in advanced.
--
View this message in context:
http://www.nabble.com/Struts-Type-Conversion-Problem-tf4735856.html#a135
43074
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