Tagged: callback, custom validation, validation
- This topic has 2 replies, 2 voices, and was last updated 5 years, 2 months ago by
Steve.
-
AuthorPosts
-
-
December 5, 2016 at 3:47 am #7613
sthamMemberHi there,
I am trying to add my own custom validation rules (using
piklist_validation_rulesfilter) to validate that an option selected (whether checkboxes, radio or select fields) is a valid one from the choices available.Based on the docs, there are 3 parameters in the callback –
$value,$field,$arguments.$fieldreturns the chosen option. However, no matter what I choose or not choose,$valuealways seems to pass an int 0 and$argumentsalways seems to be an empty array.My question is, what are those two parameters (
$valueand$arguments) supposed to represent?I have code for this that I’ve used with earlier versions of Piklist and it worked beautifully up until 0.9.4.31. I (finally) upgraded to v0.9.9.9 and it doesn’t work anymore. I don’t have any of the in-between versions.
Here’s what I used to use:
function smm_validate_single_choice( $value, $field ) { if ( is_array( $value ) ) { $value = $value[0]; } return ( array_key_exists( (string) $value, $field['choices'] ) ) ? true : __( 'does not have a valid selection.', 'smm' ); }Any help is greatly appreciated. Thank you very much!
-
December 6, 2016 at 3:41 am #7614
sthamMemberSolved it! But I’ll explain here in case it might help someone else out. I basically referred to the
validate_emailfunction inclass-piklist-validate.php(line 992) to do it.There are 5 parameters, not 3 –
$index,$value,$options,$field,$fields. Based on the documentation, these parameters are:@param int $index The field index being checked. @param mixed $value The value of the field. @param array $options The options. @param array $field The field object. @param array $fields Collection of fields.So my code now looks like this:
function smm_validate_single_choice( $index, $value, $options, $field, $fields ) { if ( is_array( $value ) ) { $value = $value[0]; } return ( array_key_exists( (string) $value, $field['choices'] ) ) ? true : __( 'does not have a valid selection.', 'smm' ); } -
December 9, 2016 at 12:48 am #7631
SteveKeymasterDocs updated: https://piklist.com/learn/doc/piklist_validation_rules/
Thanks!
-
-
AuthorPosts
- The topic ‘Parameters in custom validation rules callback returning 0 and empty array’ is closed to new replies.