Getting Value of a Checkbox in Velocity

ces55739's Avatar

ces55739

08 Jul, 2014 03:23 PM

Hi,

I was wondering how you can get the value of a checkbox in velocity. In Cascade, I have my page with a checkbox of id="all-day". Using a radio button, I can get the value perfectly like this:

$test.getChild("all-day").textValue

However, when I use a checkbox, I can only get the element, and not the value.

This works:

$test.getChild("all-day")

These do not work:

$test.getChild("all-day").getChild("value")
$test.getChild("all-day").getStructuredDataNode("value")
$test.getChild("all-day").value
$test.getChild("all-day").textValue

Any suggestions?

  1. 1 Posted by Ryan Griffith on 08 Jul, 2014 04:00 PM

    Ryan Griffith's Avatar

    Hi,

    I was doing some local testing and was able to output the value using $test.getChild("all-day").value in my local 7.10.2 instance. Keep in mind, though, this will end up being a string of all values together if there is more than one checkbox checked.

    Because checkboxes can contain multiple values, the resulting XML structure is slightly different. Specifically, checkboxes contain <value> elements whereas radios do not. I would suggest using the XPath Tool to obtain all of the potential value elements. This way you can more easily loop over each checked checkbox as well as check to see if there was at least one checked:

    #set ( $allDay = $_XPathTool.selectNodes($test, "all-day/value") )
    #if ( $allDay.size() > 0)
        #foreach ($checkbox in $allDay)
            ## ... do something with the value
            $checkbox.value
        #end
    #end
    

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by e-jameson on 08 Jul, 2014 05:02 PM

    e-jameson's Avatar

    Here is a snippet of the Page XML we are dealing with:

            <system-data-structure definition-path="Event">
                <image type="file">
                    <content/>
                    <path>/_shared-content/images/arrow-in-box.png</path>
                    <dynamic-metadata>
                        <name>caption</name>
                    </dynamic-metadata>
                    <link>site://Public/_shared-content/images/arrow-in-box.png</link>
                    <site>Public</site>
                    <name>arrow-in-box.png</name>
                </image>
                <caption>fsdf</caption>
                <link/>
                <event-dates>
                    <start-date>1404795600000</start-date>
                    <end-date>1404795600000</end-date>
                    <all-day>
                        <value>Yes</value>
                    </all-day>
                </event-dates>
                <event-dates>
                    <start-date/>
                    <end-date/>
                    <all-day>
                        <value>Yes</value>
                    </all-day>
                </event-dates>
    

    In Velocity, we are trying to do:

    #set ( $page = $_.locatePage($currentPagePath) )
    
    #foreach( $test in $page.getStructuredDataNodes("event-dates") )
    
       $test.getChild("all-day") ##this is a StructuredDataNodeAPIAdapter
    
       $test.getChild("all-day").value ## this is undefined
    
    #end
    

    We know the checkbox will only ever have one option (Checked == Yes, not checked is just <all-day /> in the XML.

    Any idea why $test.getChild("all-day").value is undefined?

  3. 3 Posted by Ryan Griffith on 08 Jul, 2014 06:31 PM

    Ryan Griffith's Avatar

    Thank you for the additional information.

    I was assuming you were using an Index Block, in which case we would be dealing with XML Elements. Instead, because you are using the Locator Tool, things change a bit as you will be working with different types of objects.

    What you will want to use is the textValues property for the field, which will return an array of each checked value. You can use the Property Tool to see the available properties for the StructuredDataNodeAPIAdapter.

    Note: there is a known defect in which the first item in this array is empty, so the array basically starts at index 1. The following should return your value:

    $test.getChild("all-day").textValues.get(1)
    

    Also, you may want to check the size of the array prior to attempting to use .get(1) to avoid an IndexOutOfBounds error. Because of the known defect, you basically need to check to see if the size is greater that 1 (or equal to 2 if you know for a fact there will only ever be one checkbox):

    #set ($allDayValues = $test.getChild("all-day").textValues)
    #if ($allDayValues.size() == 2)
        ## ... do something with the value "Yes"
        $allDayValues.get(1)
    #end
    

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by ces55739 on 08 Jul, 2014 06:38 PM

    ces55739's Avatar

    That appears to have done it. Thanks for all of the information and the prompt response times!

  5. 5 Posted by Ryan Griffith on 09 Jul, 2014 12:09 PM

    Ryan Griffith's Avatar

    Not a problem at all. I am glad to hear the information I provided was helpful.

    Note: when the known defect is eventually fixed, you may need to adjust your Format accordingly since that extra empty value won't be present anymore. Feel free to use this link to track the progress of this issue or keep an eye out for it on our Release Notes site.

    Have a great day!

  6. Ryan Griffith closed this discussion on 09 Jul, 2014 12:09 PM.

  7. Ryan Griffith re-opened this discussion on 02 Jun, 2016 02:28 PM

  8. Ryan Griffith closed this discussion on 02 Jun, 2016 02:28 PM.

Comments are closed, but you can start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac