You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Albert Lu <al...@nmdp.org> on 2003/07/02 23:26:33 UTC

velocity and radio button. Is there a better way?

Hi,

I encounter a problem with velocity and radio input box.

I want to migrate to velocity from JSP with Struts. But I see there is
no way to persist value of a radio values to an attribute in my form
bean "easily" like struts tag <bean:input ...>.

with velocity, I need to write a macro to display which radio value is
selected, and worse when the form is submitted, I need to go through the
request parameters to set the correct value.

The real problem is in my form, there are roughly 50 questions using
radio buttons and average 4 options in each question

html syntax of radio button:
    <input type="radio" name="option1" value="1" checked>option1</input>



Man, I'd regret of not using velocity because of this!

Any idea, suggestion is appreciated!

Albert Lu



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


RE: velocity and radio button. Is there a better way?

Posted by Tim Colson <tc...@cisco.com>.
Hey folks -
  Albert posted this to the DEV list, and I replied, but realized it
should be over here on Velocity-User.

Timo


> with velocity, I need to write a macro to display which radio value is
> selected, 
Yep, that's correct, but pretty easy, and one advantage over the bean
tag is that you see exactly what the HTML looks like and can change it
without recompiling the taglib. :-)

I use a macro like this one for a Yes/No/Unknown radio buttons:

#macro ( YesNoInput $fieldname $value)
    #if (! $value ) #set ($value = "") #end
    #set ($s=0)
    <input type="radio" value="Y"   name="$fieldname" #if ($value == "Y"
|| $value == "YES") #set ($s=1) checked #end >Yes
    <input type="radio" value="N"   name="$fieldname" #if ($value == "N"
|| $value == "NO" ) #set ($s=1) checked #end >No
    <input type="radio" value="UNK" name="$fieldname" #if ($s == 0)
checked #end>Unknown
#end

#YesNoInput ("happyCamper" $myForm.HappyCamper)


> and worse when the form is submitted, I need to go 
> through the request parameters to set the correct value.
Hmm... Velocity just creates the HTML, so just like the taglib, after
submission the Struts form is taking over and doing it's thing. There
might just be a field naming typo or something simple like that. I
wouldn't suspect Velocity has anything to do with it at this point.

> Man, I'd regret of not using velocity because of this!
> Any idea, suggestion is appreciated!
Heh heh... I don't think that will be necessary. :-)

Timo



> -----Original Message-----
> From: Albert Lu [mailto:alu@nmdp.org] 
> Sent: Wednesday, July 02, 2003 2:27 PM
> To: velocity-dev@jakarta.apache.org
> Subject: velocity and radio button. Is there a better way?
> 
> 
> Hi,
> 
> I encounter a problem with velocity and radio input box.
> 
> I want to migrate to velocity from JSP with Struts. But I see there is
> no way to persist value of a radio values to an attribute in my form
> bean "easily" like struts tag <bean:input ...>.
> 
> with velocity, I need to write a macro to display which radio value is
> selected, and worse when the form is submitted, I need to go 
> through the
> request parameters to set the correct value.
> 
> The real problem is in my form, there are roughly 50 questions using
> radio buttons and average 4 options in each question
> 
> html syntax of radio button:
>     <input type="radio" name="option1" value="1" 
> checked>option1</input>
> 
> 
> 
> Man, I'd regret of not using velocity because of this!
> 
> Any idea, suggestion is appreciated!
> 
> Albert Lu
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
> 
> 



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


RE: velocity and radio button. Is there a better way?

Posted by Tim Colson <tc...@cisco.com>.
> with velocity, I need to write a macro to display which radio value is
> selected, 
Yep, that's correct, but pretty easy, and one advantage over the bean
tag is that you see exactly what the HTML looks like and can change it
without recompiling the taglib. :-)

I use a macro like this one for a Yes/No/Unknown radio buttons:

#macro ( YesNoInput $fieldname $value)
    #if (! $value ) #set ($value = "") #end
    #set ($s=0)
    <input type="radio" value="Y"   name="$fieldname" #if ($value == "Y"
|| $value == "YES") #set ($s=1) checked #end >Yes
    <input type="radio" value="N"   name="$fieldname" #if ($value == "N"
|| $value == "NO" ) #set ($s=1) checked #end >No
    <input type="radio" value="UNK" name="$fieldname" #if ($s == 0)
checked #end>Unknown
#end

#YesNoInput ("happyCamper" $myForm.HappyCamper)


> and worse when the form is submitted, I need to go 
> through the request parameters to set the correct value.
Hmm... Velocity just creates the HTML, so just like the taglib, after
submission the Struts form is taking over and doing it's thing. There
might just be a field naming typo or something simple like that. I
wouldn't suspect Velocity has anything to do with it at this point.

> Man, I'd regret of not using velocity because of this!
> Any idea, suggestion is appreciated!
Heh heh... I don't think that will be necessary. :-)

Timo


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org