Skip to content

Make arrays and its contents validatable #255

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 3 commits into from
Closed
Show file tree
Hide file tree
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
71 changes: 59 additions & 12 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,35 @@ 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 (is_array($value))
{
// In case if array is empty, we add one NULL element in order to
// continue with validation. Otherwise validator will just skip
// that attribute without checking even for required.
$value = (empty($value) ? array(null) : $value);

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

$method = "validate{$rule}";
$method = "validate{$rule}";

if ($validatable and ! $this->$method($attribute, $value, $parameters, $this))
if ($validatable and ! $this->$method($attribute, $element, $parameters, $this))
{
$this->addError($attribute, $rule, $parameters);
}
}
}
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 @@ -211,13 +233,14 @@ protected function validate($attribute, $rule)
*/
protected function getValue($attribute)
{
if (array_key_exists($attribute, $this->data))
// Use array_get to search in array with dot notation
if ( ! is_null($value = array_get($this->data, $attribute)) )
{
return $this->data[$attribute];
return $value;
}
elseif (array_key_exists($attribute, $this->files))
elseif ( ! is_null($value = array_get($this->files, $attribute)) )
{
return $this->files[$attribute];
return $value;
}
}

Expand Down Expand Up @@ -275,7 +298,7 @@ protected function validateRequired($attribute, $value)
{
return false;
}
elseif (is_string($value) and trim($value) === '')
elseif ((is_string($value) and trim($value) === '') or (is_array($value) and empty($value)) )
{
return false;
}
Expand Down Expand Up @@ -347,6 +370,10 @@ protected function getPresentCount($attributes)
{
$count++;
}
elseif ( ! is_null(array_get($this->data, $key)) or ! is_null(array_get($this->files, $key)) )
{
$count++;
}
}

return $count;
Expand Down Expand Up @@ -376,7 +403,14 @@ protected function validateSame($attribute, $value, $parameters)
{
$other = $parameters[0];

return isset($this->data[$other]) and $value == $this->data[$other];
if ( is_array($arrayValue = array_get($this->data, $attribute)) )
{
return ! is_null($otherValue = array_get($this->data, $other)) and $arrayValue == $otherValue;
}
else
{
return isset($this->data[$other]) and $value == $this->data[$other];
}
}

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

return isset($this->data[$other]) and $value != $this->data[$other];
if ( is_array($arrayValue = array_get($this->data, $attribute)) )
{
return ! is_null($otherValue = array_get($this->data, $other)) and $arrayValue != $otherValue;
}
else
{
return isset($this->data[$other]) and $value != $this->data[$other];
}
}

/**
Expand Down Expand Up @@ -531,7 +572,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 Expand Up @@ -650,6 +691,12 @@ protected function validateExists($attribute, $value, $parameters)
// that the columns being "verified" shares the given attribute's name.
$column = isset($parameters[1]) ? $parameters[1] : $attribute;

// Search for the array by attribute in data/files, to use in whereIn.
if ( is_array($arrayValue = array_get($this->data, $attribute)) )
{
$value = $arrayValue;
}

$expected = (is_array($value)) ? count($value) : 1;

return $this->getExistCount($table, $column, $value) >= $expected;
Expand Down
Loading