Skip to content

Commit

Permalink
Added slick new 'sometimes' method to Validator for conditionally add…
Browse files Browse the repository at this point in the history
…ing rules.
  • Loading branch information
taylorotwell committed Oct 3, 2013
1 parent 5bd6b2b commit ffa70d3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Illuminate/Foundation/changes.json
Expand Up @@ -53,6 +53,7 @@
{"message": "Added --bench option to controller:make Artisan command.", "backport": null},
{"message": "Moved newPivot method into model for custom Pivot model instances.", "backport": null},
{"message": "Added 'shared' method to View to pull a single shared item out.", "backport": null},
{"message": "Added assertHasOldInput test assertion.", "backport": null}
{"message": "Added assertHasOldInput test assertion.", "backport": null},
{"message": "Added slick new 'sometimes' method to Validator for conditionally adding rules.", "backport": null}
]
}
31 changes: 31 additions & 0 deletions src/Illuminate/Validation/Validator.php
Expand Up @@ -2,6 +2,7 @@

use Closure;
use DateTime;
use Illuminate\Support\Fluent;
use Illuminate\Support\MessageBag;
use Illuminate\Container\Container;
use Symfony\Component\HttpFoundation\File\File;
Expand Down Expand Up @@ -160,6 +161,36 @@ protected function explodeRules($rules)
return $rules;
}

/**
* Add conditions to a given field based on a Closure.
*
* @param string $attribute
* @param string|array $rules

This comment has been minimized.

Copy link
@franzliedke

franzliedke Oct 3, 2013

Contributor

Missing the third param description and the return value here.

* @param
*/
public function sometimes($attribute, $rules, $callback)
{
$payload = new Fluent(array_merge($this->data, $this->files));

if (call_user_func($callback, $payload)) $this->mergeRules($attribute, $rules);
}

/**
* Merge additional rules into a given attribute.
*
* @param string $attribute
* @param string|array $rules
* @return void
*/
protected function mergeRules($attribute, $rules)
{
$current = array_get($this->rules, $attribute, array());

$merge = head($this->explodeRules(array($rules)));

$this->rules[$attribute] = array_merge($current, $merge);
}

/**
* Determine if the data passes the validation rules.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Expand Up @@ -745,6 +745,30 @@ public function testBeforeAndAfter()
}


public function testSoemtimesAddingRules()
{
$trans = $this->getRealTranslator();
$v = new Validator($trans, array('x' => 'foo'), array('x' => 'Required'));
$v->sometimes('x', 'Confirmed', function($i) { return $i->x == 'foo'; });
$this->assertEquals(array('x' => array('Required', 'Confirmed')), $v->getRules());

$trans = $this->getRealTranslator();
$v = new Validator($trans, array('x' => 'foo'), array('x' => 'Required'));
$v->sometimes('x', 'Confirmed', function($i) { return $i->x == 'bar'; });
$this->assertEquals(array('x' => array('Required')), $v->getRules());

$trans = $this->getRealTranslator();
$v = new Validator($trans, array('x' => 'foo'), array('x' => 'Required'));
$v->sometimes('x', 'Foo|Bar', function($i) { return $i->x == 'foo'; });
$this->assertEquals(array('x' => array('Required', 'Foo', 'Bar')), $v->getRules());

$trans = $this->getRealTranslator();
$v = new Validator($trans, array('x' => 'foo'), array('x' => 'Required'));
$v->sometimes('x', array('Foo', 'Bar:Baz'), function($i) { return $i->x == 'foo'; });
$this->assertEquals(array('x' => array('Required', 'Foo', 'Bar:Baz')), $v->getRules());
}


public function testCustomValidators()
{
$trans = $this->getRealTranslator();
Expand Down

0 comments on commit ffa70d3

Please sign in to comment.