Permalink
Browse files

Merge branch 'requre-without-all' of https://github.com/anlutro-pull-…

…requests/framework into require_without_all
  • Loading branch information...
taylorotwell committed Nov 2, 2013
2 parents 1b2ff68 + 241a2fd commit 93c1045fdea4d115d4a9dbc7ae64793bc7063d52
@@ -45,7 +45,8 @@
{"message": "Allow the 'name' of belongsTo and belongsToMany to be explictly set.", "backport": null},
{"message": "New Cache::tags feature that allows tagging cached items and flushing them by any tag.", "backport": null},
{"message": "New FrameGuard middleware sends SAMEORIGIN X-Frame-Options header on each response by default.", "backport": null},
{"message": "Added 'joinWhere' and 'leftJoinWhere' to query builder for joins with bindings.", "backport": null}
{"message": "Added 'joinWhere' and 'leftJoinWhere' to query builder for joins with bindings.", "backport": null},
{"message": "Added 'require_without_all' validation rule, which enables validation that at least one of N fields are present", "backport":null}
],
"4.0.x": [
{"message": "Added implode method to query builder and Collection class.", "backport": null},
@@ -108,7 +108,7 @@ class Validator implements MessageProviderInterface {
*
* @var array
*/
protected $implicitRules = array('Required', 'RequiredWith', 'RequiredWithout', 'RequiredIf', 'Accepted');
protected $implicitRules = array('Required', 'RequiredWith', 'RequiredWithout', 'RequiredWithoutAll', 'RequiredIf', 'Accepted');
/**
* Create a new Validator instance.
@@ -386,6 +386,25 @@ protected function anyFailingRequired(array $attributes)
return false;
}
/**
* Determine if all of the given attributes fail the required test.
*
* @param array $attributes
* @return bool
*/
protected function allFailingRequired(array $attributes)
{
foreach ($attributes as $key)
{
if ($this->validateRequired($key, $this->getValue($key)))
{
return false;
}
}
return true;
}
/**
* Validate that an attribute exists when another attribute exists
*
@@ -429,6 +448,24 @@ protected function validateRequiredWithout($attribute, $value, $parameters)
return true;
}
/**
* Validate that an attribute exists when all other attributes do not exist
*
* @param string $attribute
* @param mixed $value
* @param mixed $parameters
* @return bool
*/
protected function validateRequiredWithoutAll($attribute, $value, $parameters)
{
if ($this->allFailingRequired($parameters))
{
return $this->validateRequired($attribute, $value);
}
return true;
}
/**
* Validate that an attribute exists when another attribute has a given value.
*
@@ -1434,6 +1471,22 @@ protected function replaceRequiredWithout($message, $attribute, $rule, $paramete
return str_replace(':values', implode(' / ', $parameters), $message);
}
/**
* Replace all place-holders for the required_without_all rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @return string
*/
protected function replaceRequiredWithoutAll($message, $attribute, $rule, $parameters)
{
$parameters = $this->getAttributeList($parameters);
return str_replace(':values', implode(' / ', $parameters), $message);
}
/**
* Replace all place-holders for the required_if rule.
*
@@ -215,6 +215,78 @@ public function testValidateRequiredWithout()
}
public function testRequiredWithoutMultiple()
{
$trans = $this->getRealTranslator();
$rules = array(
'f1' => 'required_without:f2,f3',
'f2' => 'required_without:f1,f3',
'f3' => 'required_without:f1,f2',
);
$v = new Validator($trans, array(), $rules);
$this->assertTrue($v->fails());
$v = new Validator($trans, array('f1' => 'foo'), $rules);
$this->assertTrue($v->fails());
$v = new Validator($trans, array('f2' => 'foo'), $rules);
$this->assertTrue($v->fails());
$v = new Validator($trans, array('f3' => 'foo'), $rules);
$this->assertTrue($v->fails());
$v = new Validator($trans, array('f1' => 'foo', 'f2' => 'bar'), $rules);
$this->assertTrue($v->passes());
$v = new Validator($trans, array('f1' => 'foo', 'f3' => 'bar'), $rules);
$this->assertTrue($v->passes());
$v = new Validator($trans, array('f2' => 'foo', 'f3' => 'bar'), $rules);
$this->assertTrue($v->passes());
$v = new Validator($trans, array('f1' => 'foo', 'f2' => 'bar', 'f3' => 'baz'), $rules);
$this->assertTrue($v->passes());
}
public function testRequiredWithoutAll()
{
$trans = $this->getRealTranslator();
$rules = array(
'f1' => 'required_without_all:f2,f3',
'f2' => 'required_without_all:f1,f3',
'f3' => 'required_without_all:f1,f2',
);
$v = new Validator($trans, array(), $rules);
$this->assertTrue($v->fails());
$v = new Validator($trans, array('f1' => 'foo'), $rules);
$this->assertTrue($v->passes());
$v = new Validator($trans, array('f2' => 'foo'), $rules);
$this->assertTrue($v->passes());
$v = new Validator($trans, array('f3' => 'foo'), $rules);
$this->assertTrue($v->passes());
$v = new Validator($trans, array('f1' => 'foo', 'f2' => 'bar'), $rules);
$this->assertTrue($v->passes());
$v = new Validator($trans, array('f1' => 'foo', 'f3' => 'bar'), $rules);
$this->assertTrue($v->passes());
$v = new Validator($trans, array('f2' => 'foo', 'f3' => 'bar'), $rules);
$this->assertTrue($v->passes());
$v = new Validator($trans, array('f1' => 'foo', 'f2' => 'bar', 'f3' => 'baz'), $rules);
$this->assertTrue($v->passes());
}
public function testRequiredIf()
{
$trans = $this->getRealTranslator();

0 comments on commit 93c1045

Please sign in to comment.