Dc3e4 new matrix field (#484)

* fix password reset bug

* wip: matrix input

* wip: matrix input

* wip: matrix input

* Fixed matric input component logic

* matrix input cleanup

* fix lint errors

* table border and radius

* cleanup, linting

* fix component methos

* wip matrix input

* matrix condition for contains and not contain

* patch matrix input condition logic

* linting

* refactor and cleanup

* fix syntax error

* Polished the matrix input

* Fix linting

---------

Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
Favour Olayinka
2024-08-23 14:28:21 +01:00
committed by GitHub
parent fedc382594
commit 1adac8e00f
25 changed files with 919 additions and 85 deletions

View File

@@ -73,6 +73,34 @@ class FormPropertyLogicRule implements DataAwareRule, ValidationRule
],
],
],
'matrix' => [
'comparators' => [
'equals' => [
'expected_type' => 'object',
'format' => [
'type' => 'object',
],
],
'does_not_equal' => [
'expected_type' => 'object',
'format' => [
'type' => 'object',
],
],
'contains' => [
'expected_type' => 'object',
'format' => [
'type' => 'object',
],
],
'does_not_contain' => [
'expected_type' => 'object',
'format' => [
'type' => 'object',
],
],
],
],
'url' => [
'comparators' => [
'equals' => [

View File

@@ -0,0 +1,61 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class MatrixValidationRule implements ValidationRule
{
protected $field;
protected $isRequired;
public function __construct(array $field, bool $isRequired)
{
$this->field = $field;
$this->isRequired = $isRequired;
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!$this->isRequired && empty($value)) {
return; // If not required and empty, validation passes
}
if (!is_array($value)) {
$fail('The Matrix field must be an array.');
return;
}
$rows = $this->field['rows'];
$columns = $this->field['columns'];
foreach ($rows as $row) {
if (!array_key_exists($row, $value)) {
if ($this->isRequired) {
$fail("Missing value for row '{$row}'.");
}
continue;
}
$cellValue = $value[$row];
if ($cellValue === null) {
if ($this->isRequired) {
$fail("Value for row '{$row}' is required.");
}
continue;
}
if (!in_array($cellValue, $columns)) {
$fail("Invalid value '{$cellValue}' for row '{$row}'.");
}
}
// Check for extra rows that shouldn't be there
$extraRows = array_diff(array_keys($value), $rows);
foreach ($extraRows as $extraRow) {
$fail("Unexpected row '{$extraRow}' in the matrix.");
}
}
}