Retaining selected value after error in single value pulldown form field

8 posts by 2 authors in: Forums > CMS Builder
Last Post: May 21, 2019   (RSS)

By gkornbluth - May 5, 2019

Hi All,

I must be missing something simple, but try as I might, I can’t get a single value pulldown field in a form to retain its selected value after an error is thrown.

The list field (practice_country) gets it’s list options from the ‘num’ and ‘category’ fields of another database.

The code I’ve got now is below.

Any help appreciated.

Thanks,

Jerry Kornbluth

$country = @$_REQUEST['practice_country']; // in the 'add record if no errors' section at the top of the page. (can't seem to get $country to print after form submission either)

<select name="practice_country" />
<?php foreach ($country_categoriesRecords as $record): ?>

<option value="<?php echo htmlencode($record['category']) ?>"><?php echo htmlencode($record['category']) ?></option>
<?php endforeach; ?>
</select>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By daniel - May 8, 2019

Hi Jerry,

In order to have a dropdown retain a value, there needs to be some code attached to the <option> elements to indicate which should be selected. We have a helper function called "selectedIf()" for this purpose, which would look something like this:

<?php foreach ($country_categoriesRecords as $record): ?>
  <option value="<?php echo htmlencode($record['category']) ?>" <?php selectedIf($record['category'], $country); ?>><?php echo htmlencode($record['category']) ?></option>
<?php endforeach; ?>

However, you also note that you aren't able to print out $country after form submission, which may indicate an additional problem passing along the form value. If the above doesn't work, can you also add the following (somewhere below the "$country =" assignment) and let me know the output?

<?php var_dump($country); ?>

Thanks,

Daniel
Technical Lead
interactivetools.com

By daniel - May 10, 2019

Hey Jerry,

Generally, getListOptions() will get all available options for a list field, so if you want to only show countries that have been selected by non-hidden records, we'll probably need to try a different approach.

First, place this code below the "$fieldname = " assignment:

<?php $tablename = accountsTable(); ?>
<?php
  // get all table records
  list($tableRecords, $tableRecordsMeta) = getRecords(array(
    'tableName'     => $tablename,
    'loadUploads'   => false,
    'loadCreatedBy' => false,
    'allowSearch'   => false,
  ));
  
  // group records by target field to find option keys
  $tableRecordsByField = array_groupBy($tableRecords, $fieldname);
  $keys                  = array_keys($tableRecordsByField);
  
  // create option list
  $listOptions = [];
  foreach($keys as $key) {
    if (!empty( $key )) { 
      $listOptions[ $key ] = $tableRecordsByField[ $key ][ $fieldname.':label' ];
    }
  }
?>

This will create the variable $listOptions that should contain all of the currently in-use list values and labels for the target table/field. This should let you then update the opening of your foreach to look like this:

<?php foreach ($listOptions as $value => $label): ?>

Note that this will only work for single-value list fields, such as radio buttons or single dropdowns.

Try it out and let me know if you have any issues!

Thanks,

Daniel
Technical Lead
interactivetools.com

By gkornbluth - May 10, 2019

Thanks Daniel,

I'm out of town for  few  days but I'll give it try when I return.

Thanks for all your help.

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gkornbluth - May 21, 2019 - edited: May 21, 2019

Hi Daniel,

Just got back to the office and I think I'm almost there.

The code you suggested works, but it doesn't display the existing value of the radio button or retain it after an error.

I've tried to implement the selectedIf function above but can't seem to get anywhere.

Thanks as always

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By daniel - May 21, 2019

Hey Jerry,

For radio buttons and checkboxes, you'll need to use checkedIf() instead of selectedIf() - can you try that out? The parameters should be the same for both functions. If that doesn't work, can you paste your latest code here for me to check out?

Thanks,

Daniel
Technical Lead
interactivetools.com

By gkornbluth - May 21, 2019

Thanks Daniel,

I added a checkedIf that I found on the forum to the code above and it now works as planned.

Here's the final code for anyone else who can benefit.

<div class="rTableRow">
<div class="rTableCell">Country</div>
<div class="rTableCell">
<?php $fieldname = 'practice_country'; ?>

<?php $tablename = accountsTable(); ?>
<?php
// get all table records
list($tableRecords, $tableRecordsMeta) = getRecords(array(
'tableName' => $tablename,
'loadUploads' => false,
'loadCreatedBy' => false,
'allowSearch' => false,
));

// group records by target field to find option keys
$tableRecordsByField = array_groupBy($tableRecords, $fieldname);
$keys = array_keys($tableRecordsByField);

// create option list
$listOptions = [];
foreach($keys as $key) {
if (!empty( $key )) {
$listOptions[ $key ] = $tableRecordsByField[ $key ][ $fieldname.':label' ];
}
}
?>
<?php $idCounter = 0; ?>
<?php foreach ($listOptions as $value => $label): ?>
<?php $id = "$fieldname." . ++$idCounter; ?>
<input id="<?php echo $id ?>" name="<?php echo $fieldname ?>" type="radio" value="<?php echo htmlencode($value) ?>" <?php checkedIf(@$_REQUEST[$fieldname], $value) ?>/>
<label for="<?php echo $id ?>"><?php echo htmlencode($label) ?></label>
<br />
<?php endforeach ?>
</div>
</div>

Best,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php