From 1f6c85b96fcc5ba0267dca44e9a6c9669c5c184b Mon Sep 17 00:00:00 2001 From: Szymon Krajewski Date: Fri, 13 Jun 2014 23:19:39 +0200 Subject: [PATCH 1/3] Added ability to set field names --- src/Laracasts/Validation/FormValidator.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Laracasts/Validation/FormValidator.php b/src/Laracasts/Validation/FormValidator.php index 3f88db4..14520be 100644 --- a/src/Laracasts/Validation/FormValidator.php +++ b/src/Laracasts/Validation/FormValidator.php @@ -20,6 +20,11 @@ abstract class FormValidator { */ protected $messages = []; + /** + * @var array + */ + protected $names; + /** * @param ValidatorFactory $validator */ @@ -43,6 +48,11 @@ public function validate(array $formData) $this->getValidationMessages() ); + if(is_array($this->getFieldNames())) + { + $this->validation->setAttributeNames($this->getFieldNames()); + } + if ($this->validation->fails()) { throw new FormValidationException('Validation failed', $this->getValidationErrors()); @@ -75,4 +85,12 @@ public function getValidationMessages() return $this->messages; } + /** + * @return array + */ + public function getFieldNames() + { + return $this->names; + } + } From 4a02d714899c4fe9bffbe697dc86aa1003d3abed Mon Sep 17 00:00:00 2001 From: szykra Date: Fri, 13 Jun 2014 23:30:20 +0200 Subject: [PATCH 2/3] Update readme.md --- readme.md | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 19684ee..ffa3b81 100644 --- a/readme.md +++ b/readme.md @@ -100,4 +100,38 @@ Now, just inject this object into your controller or application service, and ca ```php $this->registrationForm->validate(Input::all()); -``` \ No newline at end of file +``` + +To overwrite field names e.g. in error messages put `$names` property to your Validator something like: + +```php + 'required|max:255', + 'q2' => 'required|max:255', + 'q3' => 'required|max:100', + ]; + + /** + * Custom field names + * + * @var array + */ + protected $names = [ + 'q1' => 'Question No. one', + 'q2' => 'Question No. two', + 'q3' => 'Question No. three', + ]; + +} +``` From 0ec9750cb057b408dc9ba2011f398e6be9a7c072 Mon Sep 17 00:00:00 2001 From: szykra Date: Fri, 13 Jun 2014 23:41:11 +0200 Subject: [PATCH 3/3] Fix indentation --- src/Laracasts/Validation/FormValidator.php | 30 +++++++++++----------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Laracasts/Validation/FormValidator.php b/src/Laracasts/Validation/FormValidator.php index 14520be..5efceca 100644 --- a/src/Laracasts/Validation/FormValidator.php +++ b/src/Laracasts/Validation/FormValidator.php @@ -20,10 +20,10 @@ abstract class FormValidator { */ protected $messages = []; - /** - * @var array - */ - protected $names; + /** + * @var array + */ + protected $names; /** * @param ValidatorFactory $validator @@ -48,10 +48,10 @@ public function validate(array $formData) $this->getValidationMessages() ); - if(is_array($this->getFieldNames())) - { - $this->validation->setAttributeNames($this->getFieldNames()); - } + if (is_array($this->getFieldNames())) + { + $this->validation->setAttributeNames($this->getFieldNames()); + } if ($this->validation->fails()) { @@ -85,12 +85,12 @@ public function getValidationMessages() return $this->messages; } - /** - * @return array - */ - public function getFieldNames() - { - return $this->names; - } + /** + * @return array + */ + public function getFieldNames() + { + return $this->names; + } }