validation of multitude checkboxes in one group

Status
Not open for further replies.

susannanam

Member
hello all,

i have a few checkboxes in one group. as i had to take over the database, each checkbox is related to a separate db-field. multiple checkboxes can be checked. BUT is there a way to validate that AT LEAST 1 checkbox has been checked when leaving the group (multipage)?

checkboxes.JPG
thanks
susanne
 
Personally, I'd massge the database you are taking over to merge the original separate columns into one.

I just did this for someone else, with some custom PHP in a "one off" cron job. It would need some fettling, as it's specific to their situation, but it would give you an idea of what needs to be done.

If you don't want to do that, you'll have to write a custom PHP validation that check all the elements involved, and put it on all of them. Well, strictly speaking it only needs to go on one of them, but it's swings and roundabouts as to whether you only want one to show the error msg, or all of them. Makes more sense to me if they all show something like "One of these must be checked".

The element data should be in $formModel->formData[];

-- hugh
 
Actually you might not need the PHP to massage the data in this case, you could just concat into a JSON string, which is what the checkbox element uses.

The query would be a bit long, but not too hard to build. You'd create the new checkbox element and assign your values to the options (1, 2, 3, etc). Then run a query that updates using a CONCAT that uses a series IF THEN ELSE to set each component of the JSON according to the original field's value.



-- hugh
 
OK, here's the code I wrote to do something similar. Not totally helpful, as I was dealing with a very specific situation, with 21 fields showing "availability", Mon_Mor, Mon_Aft, Mon_Eve through Sun_Eve, and moving it to seven per-day checkboxes with three options, for morning, afternoon and evening.

Code:
$myDb = FabrikWorker::getDbo();
$myQuery = $myDb->getQuery(true);

foreach ($data[0] as $row) {
  //var_dump($row);exit;
  $myQuery->clear();
  $myQuery->update('vol_volunteers');
  foreach(array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun') as $day) {
    $dayArray = array();
    foreach (array('Mor' => '1', 'Aft' => '2', 'Eve' => '3') as $shift => $key) {
      $fieldName = $day . '_' . $shift;
      $elName = 'vol_volunteers___' . $fieldName;
      //var_dump($elName);
      if ($row->$elName == '1') {
        $dayArray[] = $key;
      }
    }
    $dayJSON = json_encode($dayArray);
    $dayField = 'avail_' . strtolower($day);
    $myQuery->set($dayField . ' = ' . $myDb->quote($dayJSON));
  }
  $myQuery->where('v_ID = ' . $myDb->quote($row->vol_volunteers___v_ID));
  $myDb->setQuery($myQuery);
  $myDb->execute();
}

But it shows the principle, of taking your data, and if the old field is set, add the coresponding option value in the new checkbox, in a JSON string. Your code wouldn't need the double foreach (where I was looping round the day part, and inside that adding the "shift" part). You would just need one loop.

But you could still use the principle of having an array of old names to new values ...

Code:
$optionMap = array('pre_primary' => '1', 'primary' => 2', 'secondary' => '3', etc);

... then foreach round that as $oldFieldName => $key, look up the old field name in the $row data, if it's set add the $key to an array, then json_encode the array and write it out.

If you need some help, just shout.

-- hugh
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top