diff --git a/application/language/en/validation.php b/application/language/en/validation.php index 71553c5a..adde4e03 100644 --- a/application/language/en/validation.php +++ b/application/language/en/validation.php @@ -47,9 +47,11 @@ ), "confirmed" => "The :attribute confirmation does not match.", "email" => "The :attribute format is invalid.", + "exists" => "The selected :attribute is invalid.", "image" => "The :attribute must be an image.", "in" => "The selected :attribute is invalid.", "integer" => "The :attribute must be an integer.", + "ip" => "The :attribute must be a valid IP address.", "max" => array( "numeric" => "The :attribute must be less than :max.", "file" => "The :attribute must be less than :max kilobytes.", diff --git a/laravel/validator.php b/laravel/validator.php index d00b101c..4cc65036 100644 --- a/laravel/validator.php +++ b/laravel/validator.php @@ -406,7 +406,7 @@ protected function validate_not_in($attribute, $value, $parameters) /** * Validate the uniqueness of an attribute value on a given database table. * - * If a database column is not specified, the attribute name will be used. + * If a database column is not specified, the attribute will be used. * * @param string $attribute * @param mixed $value @@ -417,7 +417,7 @@ protected function validate_unique($attribute, $value, $parameters) { // We allow the table column to be specified just in case the column does // not have the same name as the attribute. It must be within the second - // parameter position, right after the databse table name. + // parameter position, right after the database table name. if (isset($parameters[1])) $attribute = $parameters[1]; $query = $this->db()->table($parameters[0])->where($attribute, '=', $value); @@ -432,7 +432,41 @@ protected function validate_unique($attribute, $value, $parameters) return $query->count() == 0; } - + + /** + * Validate the existence of an attribute value in a database table. + * + * @param string $attribute + * @param mixed $value + * @param array $parameters + * @return bool + */ + protected function validate_exists($attribute, $value, $parameters) + { + if (isset($parameters[1])) $attribute = $parameters[1]; + + // Grab the number of elements we are looking for. If the given value is + // in array, we'll count all of the values in the array, otherwise we + // can just make sure the count is greater or equal to one. + $count = (is_array($value)) ? count($value) : 1; + + $query = $this->db()->table($parameters[0]); + + // If the given value is an array, we will check for the existence of + // all the values in the database, otherwise we'll check for the + // presence of the single given value in the database. + if (is_array($value)) + { + $query = $query->where_in($attribute, $value); + } + else + { + $query = $query->where($attribute, '=', $value); + } + + return $query->count() >= $count; + } + /** * Validate that an attribute is a valid IP. *