WP_Customize_Setting::validate PHP Method

validate() public method

Validates an input.
See also: WP_REST_Request::has_valid_params()
Since: 4.6.0
public validate ( mixed $value ) : true | WP_Error
$value mixed Value to validate.
return true | WP_Error True if the input was validated, otherwise WP_Error.
    public function validate($value)
    {
        if (is_wp_error($value)) {
            return $value;
        }
        if (is_null($value)) {
            return new WP_Error('invalid_value', __('Invalid value.'));
        }
        $validity = new WP_Error();
        /**
         * Validates a Customize setting value.
         *
         * Plugins should amend the `$validity` object via its `WP_Error::add()` method.
         *
         * The dynamic portion of the hook name, `$this->ID`, refers to the setting ID.
         *
         * @since 4.6.0
         *
         * @param WP_Error             $validity Filtered from `true` to `WP_Error` when invalid.
         * @param mixed                $value    Value of the setting.
         * @param WP_Customize_Setting $this     WP_Customize_Setting instance.
         */
        $validity = apply_filters("customize_validate_{$this->id}", $validity, $value, $this);
        if (is_wp_error($validity) && empty($validity->errors)) {
            $validity = true;
        }
        return $validity;
    }

Usage Example

 /**
  * Test validate.
  *
  * @see WP_Customize_Setting::validate()
  */
 public function test_validate()
 {
     $setting = new WP_Customize_Setting($this->manager, 'name', array('type' => 'key', 'validate_callback' => array($this, 'filter_validate_for_test_validate')));
     $validity = $setting->validate('BAD!');
     $this->assertInstanceOf('WP_Error', $validity);
     $this->assertEquals('invalid_key', $validity->get_error_code());
 }
All Usage Examples Of WP_Customize_Setting::validate