Viewing 2 reply threads
  • Author
    Posts
    • #7613
      stham
      Member

      Hi there,

      I am trying to add my own custom validation rules (using piklist_validation_rules filter) 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. $field returns the chosen option. However, no matter what I choose or not choose, $value always seems to pass an int 0 and $arguments always seems to be an empty array.

      My question is, what are those two parameters ($value and $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!

    • #7614
      stham
      Member

      Solved it! But I’ll explain here in case it might help someone else out. I basically referred to the validate_email function in class-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' );
      
      }
    • #7631
      Steve
      Keymaster
Viewing 2 reply threads
  • The topic ‘Parameters in custom validation rules callback returning 0 and empty array’ is closed to new replies.