From 57518b03cbf19b8a714d6fcb573f71cb921005e3 Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Mon, 25 Sep 2023 17:29:52 +0300 Subject: Implement `$fieldNames`, a way to overwrite field names to be more human friendly in error messages. --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'README.md') diff --git a/README.md b/README.md index 0eec3d4..1e39400 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,9 @@ 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); +$hird = new Hird($fields, $rules, $fieldNames); if ($hird->fails()) { return $hird->errors(); @@ -48,6 +49,8 @@ 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. + ## Built-in validators There are a number of built-in validators available for use by default. If you want to remove a built-in validator, you can remove one using the `$hird->removeValidator('rule-name')` method. @@ -113,6 +116,12 @@ use Asko\Hird\Validators\Validator; class EmailValidator implements Validator { + public function __construct( + private array $fields, // all fields data + private array $fieldNames, // names of fields + ){ + } + /** * Returns a boolean `true` when given `$value` is a valid e-mail * address. Returns `false` otherwise. -- cgit v1.2.3