Validation failing for repeating groups

aijosh

Member
Hello,

I have a validation that compares two dates to ensure start date is less that end date.

If I have only one group of records repeating, the validation works ok

But when I click + to add more repeats, the validation fails for all.

I'm guessing the element values are all mixed up once there are more than one on the page.

See my php validation in case it can provide any hint: It works ok with only one record on the group
$fro = '01/' . '{#_##_6_repeat___fro_month_raw}' . '/' . '{#_##_6_repeat___fro_year_raw}';
$to = '01/' . '{#_##_6_repeat___to_month_raw}' . '/' . '{#_##_6_repeat___to_year_raw}';
return strtotime($to) > strtotime($fro);
 
Last edited:
Using placeholders in repeated groups can be a little problematic, as being name based they don't fit very well with the repeating data structures, where each named input becomes an array with multiple values, and there isn't really a single {yourtable___foo} placeholder, there's multiple {yourtable___foo_0}, {yourtable___foo_1} etc. In many places we work round this by providing a singular placeholder for the instance of the repeat being processed at the point we call your code ... but we don't (yet) do that in the PHP validation.

The easiest (well, only) way round this is to access the submitted data directly, using $repeatCounter as the index into the repeat. For example ...

Code:
$fro = '01/' . $formModel->formData['#_##_6_repeat___fro_month_raw'][$repeatCounter] . '/' . $formModel->formData['#_##_6_repeat___fro_year_raw'][$repeatCounter] ;

Wherever eval'ed code is executed for element related functionality during submission, we provide that $repeatCounter, so you can access the form data directly.

One other wrinkle to watch for is that when using the formData array directly, the data may not actually be a string. For some element types, it might still be an array of the individual parts of the submitted data, or the form input type itself might be an array (like dropdowns, radios, checkboxs, etc). So if you still get unexpected results, try dumping the data to see what it looks like ...

Code:
var_dump($formModel->formData['#_##_6_repeat___fro_month_raw']); exit;

So for example, if your fro_month element is a dropdown, then the formData for it will be an array with a single entry, so you'd actually need $formModel->formData['#_##_6_repeat___fro_month_raw'][$repeatCounter] [0].

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top