From 3acb42faee66868a76e653a34ef35de13ddf734f Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Thu, 01 Jan 2026 23:00:11 +0000
Subject: [PATCH] Merge branch 'main' of https://github.com/jakevdwerf/jvb
---
inc/meta/MetaValidator.php | 185 +++++++++++++++++++++++++++++----------------
1 files changed, 118 insertions(+), 67 deletions(-)
diff --git a/inc/meta/MetaValidator.php b/inc/meta/MetaValidator.php
index 40b1725..1d0236a 100644
--- a/inc/meta/MetaValidator.php
+++ b/inc/meta/MetaValidator.php
@@ -1,6 +1,7 @@
<?php
namespace JVBase\meta;
+use DateTime;
use JVBase\meta\MetaTypeManager;
use WP_Error;
@@ -41,59 +42,61 @@
}
- 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;
@@ -101,30 +104,20 @@
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
@@ -299,6 +292,64 @@
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)) {
--
Gitblit v1.10.0