Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,38 @@ Now, just inject this object into your controller or application service, and ca

```php
$this->registrationForm->validate(Input::all());
```
```

To overwrite field names e.g. in error messages put `$names` property to your Validator something like:

```php
<?php namespace MyApp\Forms;

use Laracasts\Validation\FormValidator;

class Questionare extends FormValidator {

/**
* Validation rules for questionare
*
* @var array
*/
protected $rules = [
'q1' => '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',
];

}
```
18 changes: 18 additions & 0 deletions src/Laracasts/Validation/FormValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ abstract class FormValidator {
*/
protected $messages = [];

/**
* @var array
*/
protected $names;

/**
* @param ValidatorFactory $validator
*/
Expand All @@ -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());
Expand Down Expand Up @@ -75,4 +85,12 @@ public function getValidationMessages()
return $this->messages;
}

/**
* @return array
*/
public function getFieldNames()
{
return $this->names;
}

}