Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add new 'required_without' validation rule #824

Merged
merged 2 commits into from

2 participants

@andershammar

Add new 'required_without' validation rule that verifies that the field under validation must be present only if the other specified fields are not present.

This is the opposite of the 'required_with' validation rule. It makes sense to have a rule like this since we already have the 'same' and 'different' validation rules which are each other's counterparts. I also need a rule like this for a personal project that I am working on. :-)

@andershammar andershammar Add 'required_without' validation rule
The field under validation must be present only if the other
specified fields are not present.
afc5fa6
@andershammar

Seems like the Travis job that runs for PHP 5.3 failed because Composer could not download the ClassPreloader repository.

@taylorotwell
Owner

Would you mind contributing unit tests for the rules?

@andershammar

Added tests for the new validation rule.

My editor was set to trim trailing white space on save so the commit modified a couple of extra rows. Sorry about that.

@taylorotwell taylorotwell merged commit faae037 into laravel:master
@taylorotwell
Owner

Pretty thorough tests, thanks!

@andershammar

Thanks for merging the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 3, 2013
  1. @andershammar

    Add 'required_without' validation rule

    andershammar authored
    The field under validation must be present only if the other
    specified fields are not present.
Commits on Apr 4, 2013
  1. @andershammar
This page is out of date. Refresh to see the latest.
View
34 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
View
62 tests/Validation/ValidationValidatorTest.php
@@ -140,6 +140,60 @@ public function testValidateRequiredWith()
$this->assertFalse($v->passes());
}
+ public function testValidateRequiredWithout()
+ {
+ $trans = $this->getRealTranslator();
+ $v = new Validator($trans, array('first' => 'Taylor'), array('last' => 'required_without:first'));
+ $this->assertTrue($v->passes());
+
+ $v = new Validator($trans, array('first' => 'Taylor', 'last' => ''), array('last' => 'required_without:first'));
+ $this->assertTrue($v->passes());
+
+ $v = new Validator($trans, array('first' => ''), array('last' => 'required_without:first'));
+ $this->assertFalse($v->passes());
+
+ $v = new Validator($trans, array(), array('last' => 'required_without:first'));
+ $this->assertFalse($v->passes());
+
+ $v = new Validator($trans, array('first' => 'Taylor', 'last' => 'Otwell'), array('last' => 'required_without:first'));
+ $this->assertTrue($v->passes());
+
+ $v = new Validator($trans, array('last' => 'Otwell'), array('last' => 'required_without:first'));
+ $this->assertTrue($v->passes());
+
+ $file = new File('', false);
+ $v = new Validator($trans, array('file' => $file), array('foo' => 'required_without:file'));
+ $this->assertFalse($v->passes());
+
+ $foo = new File('', false);
+ $v = new Validator($trans, array('foo' => $foo), array('foo' => 'required_without:file'));
+ $this->assertFalse($v->passes());
+
+ $foo = new File(__FILE__, false);
+ $v = new Validator($trans, array('foo' => $foo), array('foo' => 'required_without:file'));
+ $this->assertTrue($v->passes());
+
+ $file = new File(__FILE__, false);
+ $foo = new File(__FILE__, false);
+ $v = new Validator($trans, array('file' => $file, 'foo' => $foo), array('foo' => 'required_without:file'));
+ $this->assertTrue($v->passes());
+
+ $file = new File(__FILE__, false);
+ $foo = new File('', false);
+ $v = new Validator($trans, array('file' => $file, 'foo' => $foo), array('foo' => 'required_without:file'));
+ $this->assertTrue($v->passes());
+
+ $file = new File('', false);
+ $foo = new File(__FILE__, false);
+ $v = new Validator($trans, array('file' => $file, 'foo' => $foo), array('foo' => 'required_without:file'));
+ $this->assertTrue($v->passes());
+
+ $file = new File('', false);
+ $foo = new File('', false);
+ $v = new Validator($trans, array('file' => $file, 'foo' => $foo), array('foo' => 'required_without:file'));
+ $this->assertFalse($v->passes());
+ }
+
public function testValidateConfirmed()
{
$trans = $this->getRealTranslator();
@@ -279,7 +333,7 @@ public function testValidateSize()
$file->expects($this->any())->method('getSize')->will($this->returnValue(4072));
$v = new Validator($trans, array(), array('photo' => 'Size:3'));
$v->setFiles(array('photo' => $file));
- $this->assertFalse($v->passes());
+ $this->assertFalse($v->passes());
}
@@ -314,7 +368,7 @@ public function testValidateBetween()
$file->expects($this->any())->method('getSize')->will($this->returnValue(4072));
$v = new Validator($trans, array(), array('photo' => 'Between:1,2'));
$v->setFiles(array('photo' => $file));
- $this->assertFalse($v->passes());
+ $this->assertFalse($v->passes());
}
@@ -343,7 +397,7 @@ public function testValidateMin()
$file->expects($this->any())->method('getSize')->will($this->returnValue(4072));
$v = new Validator($trans, array(), array('photo' => 'Min:10'));
$v->setFiles(array('photo' => $file));
- $this->assertFalse($v->passes());
+ $this->assertFalse($v->passes());
}
@@ -372,7 +426,7 @@ public function testValidateMax()
$file->expects($this->any())->method('getSize')->will($this->returnValue(4072));
$v = new Validator($trans, array(), array('photo' => 'Max:2'));
$v->setFiles(array('photo' => $file));
- $this->assertFalse($v->passes());
+ $this->assertFalse($v->passes());
}
Something went wrong with that request. Please try again.