The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group

Slack Signup
Newsletter Optin
Help Desk

Validate repeating fields for one specefic entry

Labels

This Discussion is public

Notifications

Hi,
there is a example code for Validate field in repeating section

add_filter('frm_validate_field_entry', 'my_custom_validation', 10, 4);
function my_custom_validation($errors, $posted_field, $posted_value, $args){
if($posted_field->id == 102){ //change to the ID of the field to validate
if(!in_array($posted_value, array(123456,987654,234567))){ //enter your allowed values
//if it doesn't match up, add an error:
$errors['field'. $args['id']] = 'Not a Valid Serial Number';
}
}
return $errors;
}

But there will only one repeating field after the other be validated. Is there a hook to get all repeating fields of one section and check, if there is one entry used.
Maybe I got the Entries 1,2,3,4,5,6 and the user got to use one time the entry 4. How can I validate, if the user has used this entry?

Greetings, Mathias

 

For your situation, can you just duplicate the in_array/array functionality in line 4 into line 3?

Something like:

add_filter('frm_validate_field_entry', 'my_custom_validation', 10, 4);
function my_custom_validation($errors, $posted_field, $posted_value, $args){
if(!in_array($posted_field->id, array(210,212,214))){ //change to the ID's of the fields to validate
if(!in_array($posted_value, array(123456,987654,234567))){ //enter your allowed values
//if it doesn't match up, add an error:
$errors['field'. $args['id']] = 'Not a Valid Serial Number';
}
}
return $errors;
}

Hi Bobby,

no, I can't to this like that, because the field ID is always 1148, but with multiple entries. And with this code I only can access one entry. The functions runs 5 times, if the user duplicated the field 5 times.

So I need all entries at once for field ID 1148.

I hope it clarified my question.

Greetings, Mathias

So in a repeating section then you have say 4 fields. I suspect that you don't want to allow them to use the same value in any one of those fields where it exists in another. Is that right?

Yes, thats right.

And another check could be, that one value got to be chosen regarding a custom condition.

So then in a certain situation a value would be allowed to show up twice. Is that what you're saying?

No, this doesn't matter. So a person says, he got a car. And in a drop down field he can select his propertys. So he select House and life insurance, but not the value car. And I want the error message that "car" got to be selected, too.

You can set the field to required, but as you likely know, it will always error if it is blank and not based on conditions.

You'll need to use a custom script or js/jquery. Here is an example of the former:

http://form.guide/jquery/validation-using-jquery-examples.html

Pay attention to the javascript section.

So you say the way with a validation in functions.php makes no sense/doesn't work and a I got to go work with js?

You can use PHP if you'd like instead of js/jquery, however js can be live, php wouldn't alert until after you submit.

https://formidableforms.com/knowledgebase/frm_validate_field_entry/#kb-conditionally-require-a-field

And now we are getting back to my first problem. With JS or your mention in php.

Every duplicate of the field would say, it got to be choosen "car".
What I want: He can choose in the first entry "house", in the second "life insurance" and in the third "car". And no error will appear. But if "car" is missing, the error will come up.

It gets quite complicated and isn't something I'm going to be able to produce for free. For repeating sections a dynamic value will have to be used to look through the iterations created by the repeater.

You'll have to collect the values using an array since they are dynamic increments of the field you add on the backend (i.e., field_kl5nv-0, field_kl5nv-1, and so on). You'll then need to use a loop to iterate through the variables and write additional code to check the field value assignments and then produce the errors based on your conditions.

for(var i = 0; i < count($somevarhere); i++){
'field_kl5nv-'+i
}

FYI - The code above is just desk scratch and not actual PHP or JS. It will likely require some form of JS or jQuery as the repeater function uses those languages when firing off new rows of fields. I don't know exactly without testing it all.

Maybe another Pro can provide a solution otherwise, but that is my take on it.

Yes, I thought this direction, too.

But how can I create an array in functions.php, which stays alive, after functions.php has finished?

Maybe with the array will be initialized with a field before my repeating section and destroyed by a field after my repeating section? (EDIT: won't work, because does not stay alive after functions.php has finished)

Thanks for your thoughts :-)

I'm not sure functions.php fires when using ajax in adding fields inline so maybe js/jquery is the only way to go. You figured out anything else?

Discussion closed.