Is there way to use custom validation on an advanced custom field? For example, I might not want a textfield to have a value that starts with certain prefix, or I might want a number field to have a value greater than 'x'. I might also want to run a regex against a value and return an error if it doesn't match.
-
What is an advanced custom field exactly?– GhostToastCommented Sep 26, 2012 at 19:09
-
2wordpress.stackexchange.com/questions/65889/…– MiloCommented Sep 26, 2012 at 19:12
-
Another thought for client side validation would be to use something like the bassistance.de jQuery validation plugin and enqueue that for the admin section along with your own admin javascript file that defines the fields and types.– Jared CobbCommented Sep 26, 2012 at 19:16
4 Answers
There is now, I just posted a plugin that I wrote to do validation for Advanced Custom Fields to the Wordpress repository. It lets you do server side validation using either PHP code or regex, jQuery masked inputs, as well as unique value settings.
http://wordpress.org/extend/plugins/validated-field-for-acf/
-
-
It has been reported that the version on the WP repo does not work with the latest version of ACF, but I have not confirmed it. There is a new version that I have been working on but it is not complete, and I am very short on time. Feel free to contact me if you would like to help with the development. Commented Sep 12, 2017 at 15:58
-
1It hasn't been uptaded in three years and isn't compatible with the latest version of ACF. Commented Jul 9, 2018 at 16:48
I did it by plugging into acf/pre_save_post
. Using that, you can test the $_POST data. And if you don't like something, you can change $post_id to "error" when you return it. This will keep the form from processing since the post_id is not correct. You can also plug in to acf/save_post
to make sure to unset the "return" or update messages from the form.
It is all a little complicated, but I will try to give a simplified example of what I used.
$submitted_fields = '';
$validation_errors = '';
add_filter( 'acf/pre_save_post', 'custom_validation' );
function custom_validation( $post_id )
{
// Load the fields from $_POST
if ( empty($_POST['fields']) || ! is_array($_POST['fields']) )
{
$validation_errors['empty'] = "One or more fields below are required";
}
else
{
foreach( $_POST['fields'] as $k => $v )
{
// get field
$f = apply_filters('acf/load_field', false, $k );
$f['value'] = $v;
$submitted_fields[$f['name']] = $f;
}
}
// Test stuff...
if ( empty($submitted_fields['foo']) || 'bar' != $submitted_fields['foo'] )
{
$validation_errors['foo'] = "Foo did not equal bar";
}
// If there are errors, output them, keep the form from processing, and remove any redirect
if ( $validation_errors )
{
// Output the messges area on the form
add_filter( 'acf/get_post_id', array(__CLASS__, 'add_error') );
// Turn the post_id into an error
$post_id = 'error';
// Add submitted values to fields
add_action('acf/create_fields', array(__CLASS__, 'acf_create_fields'), 10, 2 );
}
else
{
// Do something... do nothing... ?
}
// return the new ID
return $post_id;
}
function acf_create_fields( $fields, $post_id )
{
foreach ( $fields as &$field )
{
if ( array_key_exists($field['name'], $submitted_fields) )
$field['value'] = $submitted_fields[$field['name']];
}
return $fields;
}
function add_error()
{
echo '<div id="message" class="error">';
foreach ( $validation_errors as $key => $error )
{
echo '<p class="' . $key . '">' . $error . '</p>';
}
echo '</div>';
}
add_action('acf/save_post', 'custom_handle_error', 1);
function custom_handle_error( $post_id )
{
if ( 'error' == $post_id )
{
unset($_POST['return']);
}
}
This doesn't allow to highlight fields that returned an error, but you could actually do it pretty easily with javascript using the classes from the errors in #message
div.
-
1Currently this only works if it's validating form fields added to the front end - the initial
pre_save_post
filter is called in theacf_form_head
function which isn't actually called when loading admin pages.– JenCommented Nov 3, 2014 at 23:46 -
That is correct. I was validating on the front end, didn't occur to me that wouldn't work on the back end. Either way, you should use the
acf-validate_value
filter if you are using the latest version 5 plugin.– JakeCommented Nov 4, 2014 at 16:28
Since version 5.0, you can use the acf/validate_value
filter - see the official documentation.
you can use this code
add_filter('acf/validate_value/name=validate_this_image', 'my_acf_validate_value', 10, 4);
function my_acf_validate_value( $valid, $value, $field, $input ){
// bail early if value is already invalid
if( !$valid ) {
return $valid;
}
// load image data
$data = wp_get_attachment_image_src( $value, 'full' );
$width = $data[1];
$height = $data[2];
if( $width < 960 ) {
$valid = 'Image must be at least 960px wide';
}
// return
return $valid;
}
result Documentation