set select field option tags with prehook in MODX

In a MODX Revo site, I have a FormIt form, with a select field which should look like:

<select name="arrangement" id="arrangement">
<option value="0" [[!+fi.arrangement:FormItIsSelected=`0`]]>Arrangement 0</option>
<option value="1" [[!+fi.arrangement:FormItIsSelected=`1`]]>Arrangement 1</option>
<option value="2" [[!+fi.arrangement:FormItIsSelected=`2`]]>Arrangement 2</option>

I would like to set the options with a FormIt prehook, called 'getArrangements'.

The prehook is called in the FormIt form definition before the form tags:

[[!FormIt?
   &preHooks=`getMaxYear,getArrangements` 

I succeeded to set the value of a simple input field with the first called prehook 'getMaxYear', it is clear to me how that works, but with the select field things are less clear. According to the MODX RTFM it should be done by json-encoding an array, like:

$hook->setValue('hobbies',json_encode(array('music','films','books')));

I removed the option tags from the select HTML and created a snippet named 'getArrangements':

<?php
$arrangements = array(  '0' => 'Arrangement 0', 
                        '1' => 'Arrangement 1', 
                        '2' => 'Arrangement 2'); 
$hook->setValue('arrangement',json_encode($arrangements));
return true;

This doesn't generate errors, but it leaves me with an empty select field.

I must be overlooking something here, I can imagine that the way I'm trying to do this doesn't work. But I'm stuck right now.

Could someone point me in the right direction to accomplish this?

Answers


FormItIsSelected wants a numeric array and you use an associative array. Use the following code to select option 0.

<?php
$arrangements = array('0'); 
$hook->setValue('arrangement',json_encode($arrangements));
return true;

Probably my final solution is of use for others with the same question: I made a snippet called 'getArrangementOptions':

$arrangements = array(  '0' => 'Arrangement 0', 
                        '1' => 'Arrangement 1', 
                        '2' => 'Arrangement 2'); 
$output = '';
foreach ($arrangements as $key => $value) {
    $output .= '<option value="'.$key.'" [[!+fi.arrangement:FormItIsSelected=`'.$key.'`]]>'.$value.'</option>';
}
return $output;

And call it inside the select tags:

<select name="arrangement" id="arrangement">
    [[!getArrangementOptions]]
</select>

How easy can it be :)


Need Your Help

Restrict Places Autocomplete API to cities of specific country android

android google-play-services google-places-api

I am using PlaceAutocompleteApi from Play Services, what I want to do is restrict auto-complete to specific country &amp; city only. Eg. All cities from lets say India only. I am using Autocomplete...

android oncreateview throwing inflateException using viewpager

android google-maps android-fragments android-viewpager

I'm trying to put a google maps fragment in another fragment, and that fragment is part of a viewpager. The initial part of it works, the map fragment gets put on my phone's screen.