Skip to content

Commit

Permalink
Merge pull request #9 from Rasalas/v2.x
Browse files Browse the repository at this point in the history
Add support for nested fields in form validation resolves #8
  • Loading branch information
mychidarko authored Nov 12, 2023
2 parents 9fb5aee + 041edd5 commit 07a149c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ composer require leafs/form
<?php

$data = [
'name' => 'Full Name',
'name' => [
'first' => 'Jane',
'last' => 'Doe',
],
'email' => 'example@example.com',
'password' => 'password1234',
];

$success = form()->validate($data, [
'name' => 'required',
'name.first' => 'required',
'name.last' => 'required',
'email' => 'required|email',
'password' => 'required|min:8'
]);
Expand Down
20 changes: 19 additions & 1 deletion src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public static function validate(array $data, array $rules)
$rule[1] = trim($rule[1]);
}

$value = $data[$field] ?? null;
$value = Form::getDotNotatedValue($data, $field);

if (!static::test($rule[0], $value, $rule[1] ?? null, $field)) {
$params = is_string($rule[1] ?? null) ? trim($rule[1], '()') : ($rule[1] ?? null);
Expand All @@ -225,6 +225,24 @@ public static function validate(array $data, array $rules)
return $output;
}

/**
* Get the value from a nested array by key.
*
* @param array $array The array to search in.
* @param string $key The key to search for.
* @return mixed|null The value if found, null otherwise.
*/
public static function getDotNotatedValue($array, $key) {
$keys = explode('.', $key);
foreach ($keys as $k) {
if (!isset($array[$k])) {
return null;
}
$array = $array[$k];
}
return $array;
}

/**
* Add custom validation rule
* @param string $name The name of the rule
Expand Down
56 changes: 56 additions & 0 deletions tests/nesting.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

use Leaf\Form;

test('getDotNotatedValue retrieves simple field', function () {
$array = ['name' => 'Max'];
expect(Form::getDotNotatedValue($array, 'name'))->toBe('Max');
});

test('getDotNotatedValue returns null for non-existent field', function () {
$array = ['name' => 'Max'];
expect(Form::getDotNotatedValue($array, 'age'))->toBeNull();
});

test('getDotNotatedValue retrieves nested field', function () {
$nestedArray = [
'customer' => [
'phone' => '123456789',
'name' => 'Anna'
],
'order' => [
'id' => 1
]
];
expect(Form::getDotNotatedValue($nestedArray, 'customer.phone'))->toBe('123456789');
expect(Form::getDotNotatedValue($nestedArray, 'customer.name'))->toBe('Anna');
expect(Form::getDotNotatedValue($nestedArray, 'order.id'))->toBe(1);
});

test('getDotNotatedValue retrieves deeply nested field', function () {
$deepNestedArray = [
'customer' => [
'details' => [
'address' => [
'city' => 'Berlin'
]
]
]
];
expect(Form::getDotNotatedValue($deepNestedArray, 'customer.details.address.city'))->toBe('Berlin');
});

test('getDotNotatedValue returns null for non-existent nested field', function () {
$deepNestedArray = [
'customer' => [
'details' => [
'address' => [
'city' => 'Berlin'
]
]
]
];
expect(Form::getDotNotatedValue($deepNestedArray, 'customer.details.phone'))->toBeNull();
});

0 comments on commit 07a149c

Please sign in to comment.