I've been struggling for 3 days now trying to get some validations working in a form in a repeat group.
I thought I could work my way around the known problems (where the validation plugins are not tracking a RepeatCounter) by using the php validation plugin as has been recommended in other threads on this issue.
But my 'brainstorm' turned out to be more like a smelly fart.
In the form in question there is a databasejoin element with 4 options that trigger the display of a few other fields.
So my workaround for that (I thought) was to use the php validation plugin instead and write some code that would only run the validation rule if the index level of the element matched the appropriate 'trigger' value selected in the databasejoin element.
Because the {placeholder} returns all of the keys in the repeat group as an imploded (comma delimited) string, I thought I could just turn the placeholder values back into an array and match up the array indexes between the controlling database join element and the element I was validating.
For example here's the code used for one element in the repeat group. ( 'facility_type_id' is the database join element that triggers the show/hide of the other elements - and my debugging code is also included ) ...
I thought that would solve my problem until I found that the {fb_organizations___hospital_gross_income_raw} placeholder was returning this in the error log...
facility_type: 1,2
hospital_gross_income: 54,765,477,0
I was expecting...
facility_type: 1,2
hospital_gross_income: 54765477,0
In that case facilty[0] (val 1) validates ok because hospital_gross_income[0] > 0
and facilty[1] (val 2) validates ok because I'm only concerned if the facility_id is 1
To explain what is happening here-
The actual facility_type values in the repeat tables are 1 & 2 (consecutively)
The actual hospital_gross_income values are 54765477 & 0 (consecutively)
BUT because I have the element set to 'numeric format' with thousands separator and zero decimals - when hospital_gross_income[0] and hospital_gross_income[1] are turned into a placeholder - {fb_organizations___hospital_gross_income_raw} became 54,765,477,0
UGHHH!
That means my validation would only work if the 1st repeat group has a facility_type of 1.
What if there were 4 repeat groups instead of 2 and the facility_types were entered in the order 4,2,3,1 instead of 1,2 - and the hospital Gross income was $54,000,477?
My code would then return...
facility_type: 4,2,3, 1
hospital_gross_income: 0,0,54,000,477
and the validation would fail because it would see $ftypes[3] as '000'
What a mess.
Also, this would only work if the field you are validating returns a single value - not a multiselect. In that case the placeholder would really get crazy because it just truncates all the values together into one long comma delimited string. How would you even know what set belonged to what repeat index?
I think we need to use something other than a comma here to separate the repeat group values, no? Or put each value set in brackets. So instead of a placeholder returning something like 5,6,8,2,4,5,9,5,3,2,45,32 - which takes a little work to figure it - it should return either 5,6,8|2,4,5|9,5,3|2,45,32 -or- [5,6,8],[2,4,5],[9,5,3],[2,45,32]
Another setback was discovering that for repeat group elements that had validation rules set -whether the elements are hidden or not, validation will be triggered on all of them at once - and all will show as invalid (in red), if even one fails the validation rule. Talk about confusing to the end-user.
I went so far as to write some javascript to toggle the 'disabled' attribute of the element in the DOM for the hidden inputs as appropriate - but the ajax validation is still triggered on every element in the repeat group even if the element has been disabled via javascript.
Until there is a way to handle validation in repeat groups I'm pretty much stuck with doing it all via javascript, I guess.
What is the timetable for fabrik v4 alpha?
I thought I could work my way around the known problems (where the validation plugins are not tracking a RepeatCounter) by using the php validation plugin as has been recommended in other threads on this issue.
But my 'brainstorm' turned out to be more like a smelly fart.
In the form in question there is a databasejoin element with 4 options that trigger the display of a few other fields.
So my workaround for that (I thought) was to use the php validation plugin instead and write some code that would only run the validation rule if the index level of the element matched the appropriate 'trigger' value selected in the databasejoin element.
Because the {placeholder} returns all of the keys in the repeat group as an imploded (comma delimited) string, I thought I could just turn the placeholder values back into an array and match up the array indexes between the controlling database join element and the element I was validating.
For example here's the code used for one element in the repeat group. ( 'facility_type_id' is the database join element that triggers the show/hide of the other elements - and my debugging code is also included ) ...
PHP:
error_log ( 'facility_type: {fb_organizations___facility_type_id_raw}' );
error_log ( 'physician count: {fb_organizations___hospital_gross_income_raw}' );
$ftypes = array({fb_organizations___facility_type_id_raw});
$vals = array({fb_organizations___hospital_gross_income_raw});
$ok = true;
for ( $x=0; $x<count($ftypes); $x++ )
{
error_log( (int)$ftypes[$x] . ':' . (int)$vals[$x] );
// if facility_type_id is 1 then run this validation ( must be greater than 0 )
if ( (int)$ftypes[$x] == 1 && (int)$vals[$x] < 1 ) $ok = false;
}
return $ok;
facility_type: 1,2
hospital_gross_income: 54,765,477,0
I was expecting...
facility_type: 1,2
hospital_gross_income: 54765477,0
In that case facilty[0] (val 1) validates ok because hospital_gross_income[0] > 0
and facilty[1] (val 2) validates ok because I'm only concerned if the facility_id is 1
To explain what is happening here-
The actual facility_type values in the repeat tables are 1 & 2 (consecutively)
The actual hospital_gross_income values are 54765477 & 0 (consecutively)
BUT because I have the element set to 'numeric format' with thousands separator and zero decimals - when hospital_gross_income[0] and hospital_gross_income[1] are turned into a placeholder - {fb_organizations___hospital_gross_income_raw} became 54,765,477,0
UGHHH!
That means my validation would only work if the 1st repeat group has a facility_type of 1.
What if there were 4 repeat groups instead of 2 and the facility_types were entered in the order 4,2,3,1 instead of 1,2 - and the hospital Gross income was $54,000,477?
My code would then return...
facility_type: 4,2,3, 1
hospital_gross_income: 0,0,54,000,477
and the validation would fail because it would see $ftypes[3] as '000'
What a mess.
Also, this would only work if the field you are validating returns a single value - not a multiselect. In that case the placeholder would really get crazy because it just truncates all the values together into one long comma delimited string. How would you even know what set belonged to what repeat index?
I think we need to use something other than a comma here to separate the repeat group values, no? Or put each value set in brackets. So instead of a placeholder returning something like 5,6,8,2,4,5,9,5,3,2,45,32 - which takes a little work to figure it - it should return either 5,6,8|2,4,5|9,5,3|2,45,32 -or- [5,6,8],[2,4,5],[9,5,3],[2,45,32]
Another setback was discovering that for repeat group elements that had validation rules set -whether the elements are hidden or not, validation will be triggered on all of them at once - and all will show as invalid (in red), if even one fails the validation rule. Talk about confusing to the end-user.
I went so far as to write some javascript to toggle the 'disabled' attribute of the element in the DOM for the hidden inputs as appropriate - but the ajax validation is still triggered on every element in the repeat group even if the element has been disabled via javascript.
Until there is a way to handle validation in repeat groups I'm pretty much stuck with doing it all via javascript, I guess.
What is the timetable for fabrik v4 alpha?