wordpress-fieldmanager
wordpress-fieldmanager copied to clipboard
Impossible to perform validation on Fieldmanager_Media
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 :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.
@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)" );