Skip to content

Instantly share code, notes, and snippets.

@juice49
Created November 30, 2012 16:53
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save juice49/4176954 to your computer and use it in GitHub Desktop.
Save juice49/4176954 to your computer and use it in GitHub Desktop.
Using the Laravel Validator, make a field required if another field is set to a given attribute.

Setup

  • Add this file to application/libraries (or, if you are already extending the Validator class, add the contents there).
  • Remove Validator alias from config/application.php in order to avoid conflicts.
  • Add "required_if_attribute" => "The :attribute field is required." to application/language/[language]/validation.php.

Usage

Define the rule using:

required_if_attribute:[field],[operator],[value]

For example:

$rules = array(
    'uses_framework' => 'required',
    'preferred_framework' => 'required_if_attribute:uses_framework,==,1'
);

Supported operators

  • ==
  • !=
  • ===
  • !==
  • <
  • <=
  • =

This is, of course, easily extended.

<?php
/**
* Validator
*
* Extending the Laravel Validator class allows us to define more powerful validation functions than
* with the Validator::register method.
*/
class Validator extends Laravel\Validator {
/**
* Implicit Attributes
*
* Attributes with these rules will be validated even if no value is supplied.
*/
protected $implicit_attributes = array(
'required',
'accepted',
'required_if_attribute'
);
/**
* Implicit
*
* By default Laravel will only validate an attribute if a value was supplied, or if the rule set is 'required' or 'accepted'.
* It'll just skip the validation, which makes 'require' being conditional impossible. Let's overwrite that to be more flexible.
*/
protected function implicit($rule) {
return (in_array($rule, $this->implicit_attributes));
}
/**
* Required if attribute
*
* Validate that a required attribute exists, only if another
* attribute satisfies the supplied conditions.
*
* i.e. 'What is your favourite PHP framework?' is only required if
* 'Do you use a framework?' is set to '1'.
*/
public function validate_required_if_attribute($attribute, $value, $parameters) {
$required = false;
switch($parameters[1]) {
case '==':
$required = $this->attributes[$parameters[0]] == $parameters[2];
case '!=':
$required = $this->attributes[$parameters[0]] != $parameters[2];
case '===':
$required = $this->attributes[$parameters[0]] === $parameters[2];
case '!==':
$required = $this->attributes[$parameters[0]] !== $parameters[2];
case '<':
$required = $this->attributes[$parameters[0]] < $parameters[2];
case '<=':
$required = $this->attributes[$parameters[0]] <= $parameters[2];
case '>':
$required = $this->attributes[$parameters[0]] > $parameters[2];
case '>=':
$required = $this->attributes[$parameters[0]] >= $parameters[2];
}
return $required ? $this->validate_required($attribute, $value) : true;
}
}
?>
@MonrealRyan
Copy link

What About in 4.2? Thank a lot.

@gthuo
Copy link

gthuo commented Feb 24, 2018

Glad it is now covered in Laravel by default: required_if:field,value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment