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', + ]; + +} +``` diff --git a/src/Laracasts/Validation/FormValidator.php b/src/Laracasts/Validation/FormValidator.php index 3f88db4..5efceca 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; + } + }