From bccd6ea4bb3d4388af4675dd4dc84fa79274f5f3 Mon Sep 17 00:00:00 2001 From: Ivano Matteo Date: Sat, 12 Sep 2020 18:35:59 +0200 Subject: [PATCH] fix --- README.md | 2 +- src/LaravelCodiceFiscale.php | 41 ++++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index dac0307..5b63d0d 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ $validated = Request::validate( [ 'cityCode' => 'required', // all rules on fiscal code - 'fiscalCode' => 'required|codfisc:first_name=name,last_name=familyName,dob=dateOfBirth,sex,cityCode', + 'fiscalCode' => 'required|codfisc:first_name=name,last_name=familyName,dob=dateOfBirth,sex=sex,cityCode=cityCode', ]); $validated = Request::validate( [ diff --git a/src/LaravelCodiceFiscale.php b/src/LaravelCodiceFiscale.php index 1605d82..54af329 100644 --- a/src/LaravelCodiceFiscale.php +++ b/src/LaravelCodiceFiscale.php @@ -61,6 +61,7 @@ public function registerValidator() $msg = null; $map = []; // create parameters map + foreach ($parameters as $i => $p) { if ($i === 0 && strpos($p, '=') === false) { $codfisc = $p; @@ -69,7 +70,7 @@ public function registerValidator() $attr = $p; } else { $p = explode('=', $p); - $map[$p[0]] = $p[1] ?? $p[0]; + $map[$p[0]] = $p[1]; } } @@ -94,29 +95,33 @@ public function registerValidator() $map = [$attr => $attribute]; } - $matchData = array_intersect_key($reqData, array_flip($map)); - $errs = $cf->validate($matchData, $map, true); + if (!empty($map)) { - if (!empty($errs)) { - if ($attr) { - $msg .= __('laravel-codice-fiscale::codfisc.field-do-not-match'); - } else { + $matchData = array_intersect_key($reqData, array_flip($map)); + $errs = $cf->validate($matchData, $map, true); - if (!empty($msg)) { - $msg .= ', '; - } - $msg .= '('; - $err_fields = ''; - foreach ($errs as $f) { - if (!empty($err_fields)) { - $err_fields .= ', '; + if (!empty($errs)) { + if ($attr) { + $msg .= __('laravel-codice-fiscale::codfisc.field-do-not-match'); + } else { + + if (!empty($msg)) { + $msg .= ', '; + } + $msg .= '('; + $err_fields = ''; + foreach ($errs as $f) { + if (!empty($err_fields)) { + $err_fields .= ', '; + } + $err_fields .= __('laravel-codice-fiscale::codfisc.' . $f); } - $err_fields .= __('laravel-codice-fiscale::codfisc.' . $f); - } - $msg .= $err_fields . ') ' . __('laravel-codice-fiscale::codfisc.do-not-match'); + $msg .= $err_fields . ') ' . __('laravel-codice-fiscale::codfisc.do-not-match'); + } } } + } else { if ($attr) { $msg .= __('laravel-codice-fiscale::codfisc.impossible-to-match') . ': ' . $cf['err'];