PodsAPI::handle_field_validation PHP Method

handle_field_validation() public method

Since: 2.0
public handle_field_validation ( mixed &$value, string $field, array $object_fields, array $fields, array | Pods $pod, array | object $params ) : array | boolean
$value mixed The value to validate
$field string Field to use for validation
$object_fields array Fields of the object we're validating
$fields array Array of all fields data
$pod array | Pods Array of pod data (or Pods object)
$params array | object Extra parameters to pass to the validation function of the field.
return array | boolean
    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) {
                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) {
            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;
    }