Validate a value before it's saved
public static validate ( string $type, mixed $value, string $name = null, array $options = null, array $fields = null, array $pod = null, integer $id = null, array | object $params = null ) | ||
$type | string | |
$value | mixed | |
$name | string | |
$options | array | |
$fields | array | |
$pod | array | |
$id | integer | |
$params | array | object |
/** * @see PodsForm:validate * * Validates the value of a field. * * @param mixed $value The value to validate * @param string $field Field to use for validation * @param array $object_fields Fields of the object we're validating * @param array $fields Array of all fields data * @param array|Pods $pod Array of pod data (or Pods object) * @param array|object $params Extra parameters to pass to the validation function of the field. * * @return array|bool * * @uses PodsForm::validate * * @since 2.0 */ public function handle_field_validation(&$value, $field, $object_fields, $fields, $pod, $params) { $tableless_field_types = PodsForm::tableless_field_types(); $fields = array_merge($fields, $object_fields); $options = $fields[$field]; $id = is_object($params) ? $params->id : (is_object($pod) ? $pod->id() : 0); if (is_object($pod)) { $pod = $pod->pod_data; } $type = $options['type']; $label = $options['label']; $label = empty($label) ? $field : $label; // Verify required fields if (1 == pods_var('required', $options['options'], 0) && 'slug' != $type) { if ('' == $value || null === $value || array() === $value || 0 === $value || '0' === $value || 0.0 === $value || '0.00' === $value) { return pods_error(sprintf(__('%s is empty', 'pods'), $label), $this); } if ('multi' == pods_var('pick_format_type', $options['options']) && 'autocomplete' != pods_var('pick_format_multi', $options['options'])) { $has_value = false; $check_value = (array) $value; foreach ($check_value as $val) { if ('' != $val && null !== $val && 0 !== $val && '0' !== $val) { $has_value = true; continue; } } if (!$has_value) { return pods_error(sprintf(__('%s is required', 'pods'), $label), $this); } } } // @todo move this to after pre-save preparations // Verify unique fields if (1 == pods_var('unique', $options['options'], 0) && '' !== $value && null !== $value && array() !== $value && 0 !== $value && '0' !== $value && 0.0 !== $value && '0.00' !== $value) { if (empty($pod)) { return false; } if (!in_array($type, $tableless_field_types)) { $exclude = ''; if (!empty($id)) { $exclude = "AND `id` != {$id}"; } $check = false; $check_value = pods_sanitize($value); // @todo handle meta-based fields // Trigger an error if not unique if ('table' == $pod['storage']) { $check = pods_query("SELECT `id` FROM `@wp_pods_" . $pod['name'] . "` WHERE `{$field}` = '{$check_value}' {$exclude} LIMIT 1", $this); } if (!empty($check)) { return pods_error(sprintf(__('%s needs to be unique', 'pods'), $label), $this); } } else { // @todo handle tableless check } } $validate = PodsForm::validate($options['type'], $value, $field, array_merge($options, pods_var('options', $options, array())), $fields, $pod, $id, $params); $validate = $this->do_hook('field_validation', $validate, $value, $field, $object_fields, $fields, $pod, $params); return $validate; }