I know that the formItIsSelected utility works perfectly well to keep the value of a Select field in a form when (for example) the form fails to validate for some reason. But has anybody tried to use this when a Select field populates from a table in MySQL? This is surely more useful than a Select field populated with static values..
I have a form in a modx site, hooked with formit and a select field in it retrieves dynamically values from a table in MySQL. When the form fails to validate this specific field loses the value the user has selected. My field (in the form) has the following setting:
<select id="Field245" name="typeOfRelationship" class="field select medium" tabindex="4">[[!getRelationshipOptions? &selected=`[[!+fi.typeOfRelationship]]`]]</select>
and the snippet and which works correctly simply does:
<?php if (!$modx->addPackage('contacts', MODX_CORE_PATH . 'components/contacts/model/')) {return 'Could not load xPDO model';}$current = $modx->getOption('selected', $scriptProperties, '');$output = [];$relationships= $modx->getCollection('RelationshipCodes');foreach ($relationships as $relationship) {$selected = $current == $relationship->get('codes') ? 'selected="selected' : '';$value=$relationship->get('descriptions');$output[] = '<option value="' . $relationship->get('descriptions') . '" ' . $selected . '>' . $relationship->get('descriptions') . '</option>';}return implode('', $output);
So far so good. But when I replace the $output[] line with:
$output[] = '<option value="' .$value . '" '. '[[!+fi.typeOfRelationship:FormItIsSelected=' ."'".$value. "'". $selected. ']]>' . $value . '</option>';
this fails! It doesn't error but it still allows the Select field to lose its setting when the form fails validation. Do you see a problem? Or maybe FormItIsSelected does not work in that context?
Many many thanks