| | |
| | | <?php |
| | | namespace JVBase\meta; |
| | | |
| | | use DateTime; |
| | | use JVBase\meta\MetaTypeManager; |
| | | use WP_Error; |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | protected function validateNumber(float $value, array $field_config):bool|WP_Error |
| | | { |
| | | if (empty($value)) { |
| | | if (!empty($field['required'])) { |
| | | return new WP_Error('required_field', 'This field is required'); |
| | | } |
| | | return true; |
| | | } |
| | | if (!is_numeric($value)) { |
| | | $this->addError($field_config['name'], __('Must be a number', 'jvb')); |
| | | return false; |
| | | } |
| | | protected function validateNumber(float $value, array $field_config):bool|WP_Error |
| | | { |
| | | if (empty($value)) { |
| | | if (!empty($field_config['required'])) { // ✅ Correct variable |
| | | return new \WP_Error('required_field', 'This field is required'); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | if (array_key_exists('min', $field_config) && $value < $field_config['min']) { |
| | | $this->addError( |
| | | $field_config['name'], |
| | | sprintf(__('Must be at least %s', 'jvb'), $field_config['min']) |
| | | ); |
| | | return false; |
| | | } |
| | | if (!is_numeric($value)) { |
| | | $this->addError($field_config['name'], __('Must be a number', 'jvb')); |
| | | return false; |
| | | } |
| | | |
| | | if (array_key_exists('max', $field_config) && $value > $field_config['max']) { |
| | | $this->addError( |
| | | $field_config['name'], |
| | | sprintf(__('Must not exceed %s', 'jvb'), $field_config['max']) |
| | | ); |
| | | return false; |
| | | } |
| | | if (array_key_exists('min', $field_config) && $value < $field_config['min']) { |
| | | $this->addError( |
| | | $field_config['name'], |
| | | sprintf(__('Must be at least %s', 'jvb'), $field_config['min']) |
| | | ); |
| | | return false; |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | if (array_key_exists('max', $field_config) && $value > $field_config['max']) { |
| | | $this->addError( |
| | | $field_config['name'], |
| | | sprintf(__('Must not exceed %s', 'jvb'), $field_config['max']) |
| | | ); |
| | | return false; |
| | | } |
| | | |
| | | protected function validateEmail(string $value, array $config):bool|WP_Error |
| | | { |
| | | if (empty($value)) { |
| | | if (!empty($field['required'])) { |
| | | return new WP_Error('required_field', 'This field is required'); |
| | | } |
| | | return true; |
| | | } |
| | | $check = is_email($value); |
| | | if (!$check) { |
| | | $this->addError( |
| | | $config['name'], |
| | | __('Must be a valid email', 'jvb') |
| | | ); |
| | | } |
| | | return $check; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | protected function validateEmail(string $value, array $config):bool|WP_Error |
| | | { |
| | | if (empty($value)) { |
| | | if (!empty($config['required'])) { // ✅ Correct variable |
| | | return new \WP_Error('required_field', 'This field is required'); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | // Validate email format |
| | | if (!is_email($value)) { |
| | | $this->addError($config['name'], __('Invalid email address', 'jvb')); |
| | | return false; |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | |
| | | protected function validateGroup(array $value, array $config):bool |
| | | { |
| | | if (empty($value)) { |
| | | if (empty($value) || !is_array($value)) { |
| | | if (!empty($config['required'])) { |
| | | $this->addError($config['name'], __('This field is required', 'jvb')); |
| | | return false; |
| | |
| | | return true; |
| | | } |
| | | |
| | | if (!is_array($value)) { |
| | | $this->addError($config['name'], __('Group field must be an array', 'jvb')); |
| | | return false; |
| | | } |
| | | |
| | | if (!isset($config['fields']) || !is_array($config['fields'])) { |
| | | return true; // No subfields to validate |
| | | } |
| | | |
| | | $all_valid = true; |
| | | |
| | | foreach ($config['fields'] as $field_name => $field_config) { |
| | | if (!isset($value[$field_name])) { |
| | | continue; // Skip missing fields unless required |
| | | } |
| | | |
| | | $field_config['name'] = $config['name'] . '[' . $field_name . ']'; // For error messages |
| | | |
| | | if (!$this->validate($value[$field_name], $field_config)) { |
| | | $all_valid = false; |
| | | // Validate each sub-field |
| | | if (!empty($config['fields']) && is_array($config['fields'])) { |
| | | foreach ($config['fields'] as $subFieldName => $subFieldConfig) { |
| | | if (isset($value[$subFieldName])) { |
| | | $subFieldConfig['name'] = $subFieldName; |
| | | $isValid = $this->validate($value[$subFieldName], $subFieldConfig); |
| | | if (!$isValid) { |
| | | return false; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | return $all_valid; |
| | | return true; |
| | | } |
| | | |
| | | protected function validateGallery(array|string $value, array $field):bool|WP_Error |
| | |
| | | return true; |
| | | } |
| | | |
| | | protected function validateTagList(array $value, array $config): bool |
| | | { |
| | | if (empty($value)) { |
| | | if (!empty($config['required'])) { |
| | | $this->addError($config['name'], __('This field is required', 'jvb')); |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | if (!is_array($value)) { |
| | | $this->addError($config['name'], __('Invalid data format', 'jvb')); |
| | | return false; |
| | | } |
| | | |
| | | // Check min/max items |
| | | if (isset($config['min_items']) && count($value) < $config['min_items']) { |
| | | $this->addError( |
| | | $config['name'], |
| | | sprintf(__('Minimum of %d items required', 'jvb'), $config['min_items']) |
| | | ); |
| | | return false; |
| | | } |
| | | |
| | | if (isset($config['max_items']) && count($value) > $config['max_items']) { |
| | | $this->addError( |
| | | $config['name'], |
| | | sprintf(__('Maximum of %d items allowed', 'jvb'), $config['max_items']) |
| | | ); |
| | | return false; |
| | | } |
| | | |
| | | // Validate each item's fields |
| | | if (!isset($config['fields']) || !is_array($config['fields'])) { |
| | | return true; |
| | | } |
| | | |
| | | foreach ($value as $index => $row) { |
| | | if (!is_array($row)) { |
| | | continue; |
| | | } |
| | | |
| | | foreach ($config['fields'] as $field_name => $field_config) { |
| | | if (!isset($row[$field_name])) { |
| | | continue; |
| | | } |
| | | |
| | | $field_config['name'] = "{$config['name']}[{$index}][{$field_name}]"; |
| | | |
| | | if (!$this->validate($row[$field_name], $field_config)) { |
| | | return false; |
| | | } |
| | | } |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | protected function validateRepeater(array $value, array $config):bool|WP_Error |
| | | { |
| | | if (empty($value)) { |