wordpress-fieldmanager icon indicating copy to clipboard operation
wordpress-fieldmanager copied to clipboard

Impossible to perform validation on Fieldmanager_Media

Open aaires opened this issue 8 years ago • 2 comments

Hi there,

I am defining an image field with the Fieldmanager_Media with param validation_rules, in order to make it required.

/* Featured Image */
    $featured_image = new Fieldmanager_Media( false, array(
        'name'               => 'featured_image',
        'description'        => 'Feature Image(16:9) min size 800x450',
        'preview_size'       => 'medium',
        'label'              => 'Featured Image',
        'button_label'       => 'Add Image',
        'validation_rules' => array('required'=> true ),
            'validation_messages' => array('required'=> 'Featured image is mandatory' ),
    ) );

This required validation rule is not running since in the code before adding the Fieldmanager validation script hidden fields are ignored on purpose:

class Fieldmanager_Util_Validation {
...
    // Fields that should always be ignored
    $ignore[] = ".fm-autocomplete";
    $ignore[] = "input[type='button']";
    $ignore[] = ":hidden";

Is it really necessary to include this hidden here? As far as I understand validation will only occur if validation_rules are specified. If this hidden is added to the $ignore array field of Fieldmanager_Media can't be required or have any other rules.

I am thinking of removing the ":hidden" from the $ignore array but would like some feedback in case you think this is compromising Fieldmanager in some way I am not currently foreseeing.

Thank you in advance.

aaires avatar Aug 26 '16 11:08 aaires

@aaires :hidden is probably mostly there to address display_if situations where fields are only visible based on other field values.

I'm sure though how this related to your case since your field isn't hidden as per the definition above, but you can however amend :hidden to :hidden:not( .some_class ) or something if that's the case, OR override it to a JS function and pass true for whatever fields you need to validate.

shadyvb avatar Aug 30 '16 15:08 shadyvb

@aaires I actually had to do this today, the snippets is:

$( 'form#post' ).data( 'validator' ).settings.ignore = $( 'form#post' ).data( 'validator' ).settings.ignore.replace( /:hidden/, ":hidden:not(.fm-media-id)" );

shadyvb avatar Aug 31 '16 09:08 shadyvb