Home › Forums › General Issues › How to avoid validation function duplicate?
Hi,
I’m using acf/validate_value/name= filter to validate some repeater fields.
I have 8 fields to validate. Because I need to get specific IDs in those fields, I have 8 functions like this (I’ve splited some code) :
function validate_col1( $valid, $value, $field, $input )
{
if( !$valid )
{
return $valid;
}
if(count($value > 0))
{
foreach($value as $aLine)
{
$iWidth = 0;
foreach($aLine['field_5aa9111c39818'] as $aProperties)
{
$iWidth += $aProperties['field_5aa9113039819'];
}
}
}
return $valid;
}
function validate_col2( $valid, $value, $field, $input )
{
if( !$valid )
{
return $valid;
}
if(count($value > 0))
{
foreach($value as $aLine)
{
$iWidth = 0;
foreach($aLine['field_5ace04f2c8c93'] as $aProperties)
{
$iWidth += $aProperties['field_5ace04f2c8c94'];
}
}
}
return $valid;
}
How can I avoid to do this ugly thing ? Is it possible to have only one function to validate all my fields ?
I tried to add arguments for the specific IDs I need in the function but, as the acf_validate_value take 4 parameters, I can’t add more.
Thanks for your help
Lena

Yes, you can do this in one function, but you’ll still need to do some hard coding. Your sub field keys are different.
The 3rd argument to your filter is $field. This contains all the information on the current field being validated. You could check $field[‘key’] to see what field is being validated and then set your other values based on that check.
While combining the validation filters will reduce some of the duplication of code it will also mean that you’ll need to add additional code to deal with the differences of each field. Where on the other hand, creating a different filter for each field means that you don’t need to worry about those differences.
The topic ‘How to avoid validation function duplicate?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.