Skip to content

Add ability to validate arrays explicitly #739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,36 @@ protected function validate($attribute, $rule)
// that the attribute is required, rules are not run for missing values.
$value = $this->getValue($attribute);

$validatable = $this->isValidatable($rule, $attribute, $value);
if (strpos($attribute, '[]') !== false and is_array($value))
{

$value = empty($value) ? array(null) : $value;

foreach ($value as $item)
{
$validatable = $this->isValidatable($rule, $attribute, $item);

$method = "validate{$rule}";

$method = "validate{$rule}";
if ($validatable and ! $this->$method($attribute, $item, $parameters, $this))
{
$this->addError($attribute, $rule, $parameters);
}
}

if ($validatable and ! $this->$method($attribute, $value, $parameters, $this))
}
else
{
$this->addError($attribute, $rule, $parameters);

$validatable = $this->isValidatable($rule, $attribute, $value);

$method = "validate{$rule}";

if ($validatable and ! $this->$method($attribute, $value, $parameters, $this))
{
$this->addError($attribute, $rule, $parameters);
}

}
}

Expand All @@ -218,6 +241,8 @@ protected function validate($attribute, $rule)
*/
protected function getValue($attribute)
{
$attribute = str_replace('[]', '', $attribute);

if ( ! is_null($value = array_get($this->data, $attribute)))
{
return $value;
Expand Down Expand Up @@ -286,6 +311,10 @@ protected function validateRequired($attribute, $value)
{
return false;
}
elseif (is_array($value) and empty($value))
{
return false;
}
elseif ($value instanceof File)
{
return (string) $value->getPath() != '';
Expand Down Expand Up @@ -348,9 +377,9 @@ protected function getPresentCount($attributes)
{
$count = 0;

foreach ($attributes as $key)
foreach ($attributes as $attribute)
{
if (isset($this->data[$key]) or isset($this->files[$key]))
if (! is_null($this->getValue($attribute)))
{
$count++;
}
Expand Down Expand Up @@ -383,7 +412,7 @@ protected function validateSame($attribute, $value, $parameters)
{
$other = $parameters[0];

return isset($this->data[$other]) and $value == $this->data[$other];
return ! is_null($otherValue = $this->getValue($other)) and $value == $otherValue;
}

/**
Expand All @@ -398,7 +427,7 @@ protected function validateDifferent($attribute, $value, $parameters)
{
$other = $parameters[0];

return isset($this->data[$other]) and $value != $this->data[$other];
return ! is_null($otherValue = $this->getValue($other)) and $value != $otherValue;
}

/**
Expand Down Expand Up @@ -540,7 +569,7 @@ protected function getSize($attribute, $value)
// entire length of the string will be considered the attribute size.
if (is_numeric($value) and $hasNumeric)
{
return $this->data[$attribute];
return $value;
}
elseif ($value instanceof File)
{
Expand Down