Validation does not work for fields in group

Support General Validation does not work for fields in group

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #6598
    ludovic.meyerludovic.meyer
    Participant

    Hello,

    When fields are in a group, the validation does not works.
    For example, in the metabox below :
    - the litte red star displays next "Légende et crédits de la vidéo" (that's ok)
    - the post can be publish even when "vm_video_legend" is empty
    - "vm_video_link" displays the english validation message "Please enter a valid URL." instead of mine "Ce champ doit être une url valide".

    $meta_boxes[] = array(
        'title'      => __( 'La résidence', 'vm' ),
        'post_types' => array( 'residence' ),
        'context'    => 'normal',
        'priority'   => 'high',
        'id'         => 'vm_residence',
        'fields' => array(
            array(
                'name'    => __( 'Vidéo', 'vm' ),
                'id'      => 'vm_video_group',
                'type'    => 'group',
                'fields' => array(
                    array(
                        'name'    => __( 'Lien de la vidéo (Youtube, Vimeo)', 'vm' ),
                        'id'      => 'vm_video_link',
                        'type'    => 'oembed',
                    ),
                    array(
                        'name'    => __( 'Légende et crédits de la vidéo', 'vm' ),
                        'id'      => 'vm_video_legend',
                        'type'    => 'text',
                    ),
    
                ),
            ),
        ),
        'validation' => array(
            'rules' => array(
                'vm_video_group' => array(
                    "vm_video_link" => array(
                        'url'  => true,
                    ),
                    "vm_video_legend" => array(
                        'required'  => true,
                    ),
                ),
            ),
            'messages' => array(
                'vm_video_group' => array(
                    "vm_video_link" => array(
                        'url'  => __( 'Ce champ doit être une url valide', 'vm' ),
                    ),
                    "vm_video_legend" => array(
                        'required'  => __( 'Ce champ est requis', 'vm' ),
                    ),
                ),
            ),
        ),
    );
    

    I also tried to define validation rules and messages like below with no success

    
    <?php 
    
    'validation' => array(
        'rules' => array(
            'vm_video_group' => array(
                "vm_video_link" => array(
                    'url'  => true,
                ),
                "vm_video_legend" => array(
                    'required'  => true,
                ),
            ),
        ),
        'messages' => array(
            'vm_video_group' => array(
                "vm_video_link" => array(
                    'url'  => __( 'Ce champ doit être une url valide', 'vm' ),
                ),
                "vm_video_legend" => array(
                    'required'  => __( 'Ce champ est requis', 'vm' ),
                ),
            ),
        ),
    ),
    
    ?>
    

    Can you fix this please ?

    Regards,

    #6599
    ludovic.meyerludovic.meyer
    Participant

    The second portion of code didn't display well. Look here :

    'validation' => array(
        'rules' => array(
            'vm_video_group' => array(
                "vm_video_link" => array(
                    'url'  => true,
                ),
                "vm_video_legend" => array(
                    'required'  => true,
                ),
            ),
        ),
        'messages' => array(
            'vm_video_group' => array(
                "vm_video_link" => array(
                    'url'  => __( 'Ce champ doit être une url valide', 'vm' ),
                ),
                "vm_video_legend" => array(
                    'required'  => __( 'Ce champ est requis', 'vm' ),
                ),
            ),
        ),
    ),
    
    #6629
    Truong GiangTruong Giang
    Participant

    Hi,

    jQueryValidation bases on input id to check. So please change the validation param like this:

    'validation' => array(
    			'rules' => array(
    				"vm_video_group_vm_video_link" => array(
    					'url'  => true,
    				),
    				"vm_video_group_vm_video_legend" => array(
    					'required'  => true,
    				),
    			),
    			'messages' => array(
    				"vm_video_group_vm_video_link" => array(
    					'url'  => __( 'Ce champ doit être une url valide', 'vm' ),
    				),
    				"vm_video_group_vm_video_legend" => array(
    					'required'  => __( 'Ce champ est requis', 'vm' ),
    				),
    			),
    		),
    
    #6664
    ttroelsttroels
    Participant

    Here I discovered that the validation does work in groups, but only the first group.
    If I create a cloneable group, so the other groups and fields don't have any validation in them, because the fields now have different id's than defined in our validation rules...

    #6672
    Truong GiangTruong Giang
    Participant

    Hi,

    Sorry because current version of MB Group only supports HTML5 validation via attributes when clone. Please use it if possible.

    Thanks.

    #6678
    ttroelsttroels
    Participant

    Yes, I use the basic HTML5 validation as well, but I would like to implement both HTML5 and JS validation. Seems it don't quite work in cloneable groups. It's just for informing you about the issue 😉

    #6686
    Truong GiangTruong Giang
    Participant

    Hi,

    Thank you for your report. We will consider this problem in the future. ID of field in clonable group is dynamic, so we can't use old mechanism. This is a hard part.

    Thank you very much.

    #17122
    GrupoADSLZoneGrupoADSLZone
    Participant

    +1,

    2019 and the problem persists.

    Certainly the solution of manually specifying the ID of the HTML field is not serious.

    It forces us to implement a logic that metabox has already implemented previously to generate the ID property.

    Other frameworks like carbonfields can manage this task effectively.

    #22307
    JasonJason
    Participant

    I'm also having this problem. I would like to validate fields inside a group.

    
    function register_application_meta_boxes( $meta_boxes ) {
        $prefix = '';
        $meta_boxes[] = array (
            'title' => esc_html__( 'Job Application Form', 'zri-domain' ),
            'id' => 'job-application-form',
            'post_types' => array(
                0 => 'job-application',
            ),
            'context' => 'normal',
            'priority' => 'high',
            'fields' => array(
                array (
                    'id' => $prefix . 'first-name',
                    'type' => 'text',
                    'name' => esc_html__( 'First Name', 'zri-domain' ),
                    'admin_columns' => array(
                        'position' => 'replace title',
                        'title' => 'First Name',
                    ),
                ),
                array (
                    'id' => $prefix . 'experience',
                    'type' => 'group',
                    'name' => esc_html__( 'Employment History', 'zri-domain' ),
                    'fields' => array(
                        array (
                            'id' => $prefix . 'start-date',
                            'type' => 'date',
                            'name' => esc_html__( 'Start Date', 'zri-domain' ),
                            'js_options' => array(
                                'dateFormat'      => 'mm/dd/yy',
                            ),
                        ),
                        array (
                            'id' => $prefix . 'end-date',
                            'type' => 'date',
                            'name' => esc_html__( 'End Date', 'zri-domain' ),
                            'js_options' => array(
                                'dateFormat'      => 'mm/dd/yy',
                            ),
                        )
                    ),
                    'clone' => 1,
                    'default_state' => 'expanded',
                    'add_button' => esc_html__( '+ Add Employment', 'zri-domain' ),
                    'collapsible' => true,
                    'group_title' => 'Employment {#}',
                )
            ),
            'validation' => array(
                'rules'  => array(
                    'experience' => array(
                        'start-date' => array(
                            'required'  => true,
                            'date' => true
                        ),
                        'end-date' => array(
                            'required'  => true,
                            'date' => true
                        )
                    )
                ),
            )
        );
        return $meta_boxes;
    }
    
    #22308
    Long NguyenLong Nguyen
    Moderator

    Hi Jason,

    We are working to cover this case, please use the HTML validation to validate sub-fields inside the cloneable group.

    array (
        'id' => $prefix . 'experience',
        'type' => 'group',
        'name' => esc_html__( 'Employment History', 'zri-domain' ),
        'fields' => array(
            array (
                'id' => $prefix . 'start-date',
                'type' => 'date',
                'name' => esc_html__( 'Start Date', 'zri-domain' ),
                'js_options' => array(
                    'dateFormat'      => 'mm/dd/yy',
                ),
                'required' => true //This
            ),
            array (
                'id' => $prefix . 'end-date',
                'type' => 'date',
                'name' => esc_html__( 'End Date', 'zri-domain' ),
                'js_options' => array(
                    'dateFormat'      => 'mm/dd/yy',
                ),
                'required' => true //This
            )
        ),
        'clone' => 1,
        'default_state' => 'expanded',
        'add_button' => esc_html__( '+ Add Employment', 'zri-domain' ),
        'collapsible' => true,
        'group_title' => 'Employment {#}',
    )
    #31300
    Austin PassyAustin Passy
    Participant

    Any update on this? Field group validations would be a great addition.

    #38839
    PiotrPiotr
    Participant

    Ditto. Any plans to fix it?

    #41612

    Still not fixed... wow!

    #41613

    You should update the validation doc about it... this aint working for any fields inside a group! Wasted my time again!

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.