remote validation in group Fields possible

Support General remote validation in group Fields possible

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #45899
    cm1cm1
    Participant

    Hi,

    i´ve tried to use remote validation on a group field, but no ajax call is triggerd when i save the post.
    I´ve used your example

    'validation' => [
        'rules' => [
            'field_id1' => [
                'remote' => admin_url( 'admin-ajax.php?action=my_action1' ),
            ],
        ],
        'messages' => [
            'field_id1' => [
                'remote'  => 'Value is not valid.',
            ],
        ],
    ],
    

    Is is possible?

    Best Regards Thomas

    #45906
    PeterPeter
    Moderator

    Hello,

    Can you share the code that creates the group field? You should add the subfield ID to the remote validation. Here is an example:

    add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
    
    function your_prefix_function_name( $meta_boxes ) {
        $meta_boxes[] = [
            'title'    => __( 'My Post Meta1', 'your-text-domain' ),
            'id'       => 'my-post-meta1',
            'fields'     => [            
                [
                    'name'            => __( 'Group', 'your-text-domain' ),
                    'id'              => 'group',
                    'type'            => 'group',
                    'fields'          => [
                        [
                            'name'            => __( 'Text', 'your-text-domain' ),
                            'id'              => 'text',
                            'type'            => 'text',           
                        ],
                    ],
                ],
            ],
            'validation' => [
                'rules'    => [
                    'text' => [
                        'remote' => admin_url( 'admin-ajax.php?action=my_action1' )
                    ]
                ],
                'messages' => [
                    'text' => [
                        'remote' => __( 'test remote validation', 'your-text-domain' )
                    ]
                ],
            ],
        ];
    
        return $meta_boxes;
    }
    
    add_action( 'wp_ajax_my_action1', function () {
        if ( $_GET['text'] === 'something' ) {
            echo 'true'; // Valid
        } else {
            echo 'false'; // Invalid
        }
        die;
    } );
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.