Wordpress - Advanced Custom Fields Validation

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.


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/

Tags:

Custom Field