Skip to content

Commit

Permalink
Add better support for custom messages for `Orchestra\Support\Validat…
Browse files Browse the repository at this point in the history
…or`.

Closes #27.

Signed-off-by: crynobone <crynobone@gmail.com>
  • Loading branch information
crynobone committed Jan 7, 2014
1 parent ba2c29a commit b385c6d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
6 changes: 6 additions & 0 deletions docs/changes.md
Expand Up @@ -11,11 +11,17 @@ title: Support Change Log
* Allow `Orchestra\Support\Nesty` to prepend an item without knowing the current first item.
* Add `Orchestra\Support\Messages::extend()` and tweak how Messages notification can be manipulated on current request.
* Add `Orchestra\Support\Nesty::is()` to return instance of `Illuminate\Support\Fluent` to allow further chaining of the instance.
* Add `Orchestra\Support\Str::searchable()` for better pattern matching.
* Add `Orchestra\Support\Relic`.
* Add `Orchestra\Support\Str::replace()`.
* `Illuminate\Support\Str::title()` is implemented, remove duplicate method.
* Add support to use `Orchestra\Support\Validator::extendScope()`, useful to have when need to deal with conditional rules <http://laravel.com/docs/validation#conditionally-adding-rules>.
* Refactor `Orchestra\Support\Str::streamGetContents()`.
* Implement [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) coding standard.
* Refactor `Orchestra\Support\Validator::getBindedRules()` to use `Orchestra\Support\Str::replace()`.
* Refactor `Orchestra\Support\Nesty::decendants()`.
* Return `Orchestra\Support\Nesty::getItems()` as instance of `Illuminate\Support\Collection`.
* Add support for custom messages on `Orchestra\Support\Validator` using `$phrases` protected property.
* Add following facades:
- `Orchestra\Support\Facades\Debug`
- `Orchestra\Support\Facades\Notifier`
Expand Down
30 changes: 21 additions & 9 deletions src/Orchestra/Support/Validator.php
Expand Up @@ -13,6 +13,13 @@ abstract class Validator
*/
protected $resolver;

/**
* List of phrases.
*
* @var array
*/
protected $phrases = array();

/**
* List of rules.
*
Expand Down Expand Up @@ -84,16 +91,16 @@ public function bind(array $bindings)
*
* @param array $input
* @param string|array $event
* @param array $messages
* @param array $phrases
* @return \Illuminate\Validation\Validator
*/
public function with(array $input, $events = array(), $messages = array())
public function with(array $input, $events = array(), array $phrases = array())
{
$this->runQueuedOn();

$rules = $this->runValidationEvents($events);
list($rules, $phrases) = $this->runValidationEvents($events, $phrases);

$this->resolver = V::make($input, $rules, $messages);
$this->resolver = V::make($input, $rules, $phrases);

$this->runQueuedExtend($this->resolver);

Expand Down Expand Up @@ -144,12 +151,13 @@ protected function runQueuedExtend($resolver)
}

/**
* Run validation events and return the finalize rules.
* Run validation events and return the finalize rules and phrases.
*
* @param array|string $events
* @param array $phrases
* @return array
*/
protected function runValidationEvents($events)
protected function runValidationEvents($events, array $phrases)
{
is_array($events) or $events = (array) $events;

Expand All @@ -158,12 +166,16 @@ protected function runValidationEvents($events)

// Convert rules array to Fluent, in order to pass it by references
// in all event listening to this validation.
$rules = new Fluent($this->getBindedRules());
$rules = new Fluent($this->getBindedRules());
$phrases = new Fluent(array_merge($this->phrases, $phrases));

foreach ((array) $events as $event) {
Event::fire($event, array(& $rules));
Event::fire($event, array(& $rules, & $phrases));
}

return $rules->getAttributes();
return array(
$rules->getAttributes(),
$phrases->getAttributes(),
);
}
}
12 changes: 9 additions & 3 deletions tests/ValidatorTest.php
Expand Up @@ -38,9 +38,10 @@ public function testValidation()
$event = m::mock('\Illuminate\Events\Dispatcher[fire]');
$validator = m::mock('\Illuminate\Validation\Factory');
$rules = array('email' => array('email', 'foo:2'), 'name' => 'any');
$phrases = array('email.required' => 'Email required');

$event->shouldReceive('fire')->once()->with('foo.event', m::any())->andReturn(null);
$validator->shouldReceive('make')->once()->with(array(), $rules, array())
$validator->shouldReceive('make')->once()->with(array(), $rules, $phrases)
->andReturn(m::mock('\Illuminate\Validation\Validator'));

\Illuminate\Support\Facades\Event::swap($event);
Expand All @@ -66,8 +67,9 @@ public function testValidationWithoutAScope()
$event = m::mock('\Illuminate\Events\Dispatcher[fire]');
$validator = m::mock('\Illuminate\Validation\Factory');
$rules = array('email' => array('email', 'foo:2'), 'name' => 'any');
$phrases = array('email.required' => 'Email required', 'name' => 'Any name');

$validator->shouldReceive('make')->once()->with(array(), $rules, array())
$validator->shouldReceive('make')->once()->with(array(), $rules, $phrases)
->andReturn(m::mock('\Illuminate\Validation\Validator'));

\Illuminate\Support\Facades\Event::swap($event);
Expand All @@ -76,7 +78,7 @@ public function testValidationWithoutAScope()
$stub = new FooValidator;
$stub->bind(array('id' => '2'));

$validation = $stub->with(array());
$validation = $stub->with(array(), null, array('name' => 'Any name'));

$this->assertInstanceOf('\Illuminate\Validation\Validator', $validation);
}
Expand All @@ -89,6 +91,10 @@ class FooValidator extends \Orchestra\Support\Validator
'name' => 'any',
);

protected $phrases = array(
'email.required' => 'Email required',
);

protected function onFoo($value)
{
$_SERVER['validator.onFoo'] = $value;
Expand Down

0 comments on commit b385c6d

Please sign in to comment.