Permalink
Browse files

Add 'required_without' validation rule

The field under validation must be present only if the other
specified fields are not present.
  • Loading branch information...
1 parent 78a494e commit afc5fa6995e99d9879a21223a5e6a91e8a0afd00 @andershammar andershammar committed Apr 3, 2013
Showing with 33 additions and 1 deletion.
  1. +33 −1 src/Illuminate/Validation/Validator.php
@@ -90,7 +90,7 @@ class Validator implements MessageProviderInterface {
*
* @var array
*/
- protected $implicitRules = array('Required', 'RequiredWith', 'Accepted');
+ protected $implicitRules = array('Required', 'RequiredWith', 'RequiredWithout', 'Accepted');
/**
* Create a new Validator instance.
@@ -339,6 +339,24 @@ protected function validateRequiredWith($attribute, $value, $parameters)
}
/**
+ * Validate that an attribute exists when another attribute does not exists
+ *
+ * @param string $attribute
+ * @param mixed $value
+ * @param mixed $parameters
+ * @return bool
+ */
+ protected function validateRequiredWithout($attribute, $value, $parameters)
+ {
+ if ($this->anyFailingRequired($parameters))
+ {
+ return $this->validateRequired($attribute, $value);
+ }
+
+ return true;
+ }
+
+ /**
* Get the number of attributes in a list that are present.
*
* @param array $attributes
@@ -1199,6 +1217,20 @@ protected function replaceRequiredWith($message, $attribute, $rule, $parameters)
}
/**
+ * Replace all place-holders for the required_without rule.
+ *
+ * @param string $message
+ * @param string $attribute
+ * @param string $rule
+ * @param array $parameters
+ * @return string
+ */
+ protected function replaceRequiredWithout($message, $attribute, $rule, $parameters)
+ {
+ return str_replace(':values', implode(' / ', $parameters), $message);
+ }
+
+ /**
* Replace all place-holders for the same rule.
*
* @param string $message

0 comments on commit afc5fa6

Please sign in to comment.