From 641826a5a408b00a6adb04847d178925bbe0e574 Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Mon, 25 Sep 2023 20:46:58 +0300 Subject: Improve API --- README.md | 11 ++++++++--- src/Hird.php | 12 +++++------- tests/HirdTest.php | 8 ++------ 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 1e39400..08ef693 100644 --- a/README.md +++ b/README.md @@ -34,9 +34,8 @@ An example usage of Hird looks like this: use Askonomm\Hird\Hird; $fields = ['email' => 'asko@asko.dev']; -$fieldNames = ['email' => 'E-mail']; $rules = ['email' => 'required|email|len:5']; -$hird = new Hird($fields, $rules, $fieldNames); +$hird = new Hird($fields, $rules); if ($hird->fails()) { return $hird->errors(); @@ -49,7 +48,13 @@ You can also get the first error rather than all errors by using the method `$hi If you wish to run the validation without needing to call `$hird->fails()`, you can instead call `$hird->validate()`. -Another thing you may notice is the presence of `$fieldNames`, which is a way overwriting the field names for use within the error messages, so that `email` could become `E-mail` when shown to the user. If you don't care about this then you can entirely skip this as only the `$fields` and `$rules` are required for Hird to work. +#### Overwrite field names in validation messages + +You can use `$hird->setFieldNames` to overwrite the field names for use within the error messages, so that `email` could become `E-mail` when shown to the user. Simply provide an array where the keys are the actual field names you instantiated Hird with and the values the names you'd like used in validation messages, like so: + +```php +$hird->setFieldNames(['email' => 'E-mail']); +``` ## Built-in validators diff --git a/src/Hird.php b/src/Hird.php index f60eca8..956d7d6 100644 --- a/src/Hird.php +++ b/src/Hird.php @@ -52,9 +52,8 @@ class Hird public function __construct( private array $fields, private array $rules, - array $fieldNames = [] ) { - $this->composeFieldNames($fieldNames); + $this->setFieldNames([]); $this->registerDefaultValidators(); } @@ -77,15 +76,14 @@ class Hird * @param array $fieldNames * @return void */ - private function composeFieldNames(array $fieldNames): void + public function setFieldNames(array $fieldNames): void { - $updatedFieldNames = []; - foreach ($this->fields as $field => $value) { - $updatedFieldNames[$field] = $fieldNames[$field] ?? $field; + $this->fieldNames[$field] = $fieldNames[$field] ?? $field; } - $this->fieldNames = $updatedFieldNames; + // Re-register the default validators to pass the field names. + $this->registerDefaultValidators(); } /** diff --git a/tests/HirdTest.php b/tests/HirdTest.php index 35f4895..8083562 100644 --- a/tests/HirdTest.php +++ b/tests/HirdTest.php @@ -94,12 +94,8 @@ test('Validate using a overwritten field name', function () { $fieldNames = ['date' => 'Date']; $rules = ['date' => 'date-format:Y-m-d H:i:s']; - $hird = new Hird( - fields: $fields, - rules: $rules, - fieldNames: $fieldNames - ); - + $hird = new Hird($fields, $rules); + $hird->setFieldNames($fieldNames); $hird->fails(); expect($hird->errors())->toBe([ -- cgit v1.2.3