Permalink
Browse files

Added slick new 'sometimes' method to Validator for conditionally add…

…ing rules.
  • Loading branch information...
taylorotwell committed Oct 3, 2013
1 parent 5bd6b2b commit ffa70d312c8e639ce36e2859d4977dba123a2af6
@@ -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}
]
}
@@ -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;
@@ -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.

@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.
*
@@ -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();

0 comments on commit ffa70d3

Please sign in to comment.