continuing to refactor the validator.
This commit is contained in:
parent
f8e63f313d
commit
ca784e9fc2
|
@ -192,9 +192,7 @@ protected function check($attribute, $rule)
|
||||||
*/
|
*/
|
||||||
protected function error($attribute, $rule, $parameters)
|
protected function error($attribute, $rule, $parameters)
|
||||||
{
|
{
|
||||||
$message = $this->get_message($attribute, $rule);
|
$message = $this->format($this->message($attribute, $rule), $attribute, $rule, $parameters);
|
||||||
|
|
||||||
$message = $this->format_message($message, $attribute, $rule, $parameters);
|
|
||||||
|
|
||||||
$this->errors->add($attribute, $message);
|
$this->errors->add($attribute, $message);
|
||||||
}
|
}
|
||||||
|
@ -273,7 +271,7 @@ protected function validate_integer($attribute, $value)
|
||||||
*/
|
*/
|
||||||
protected function validate_size($attribute, $value, $parameters)
|
protected function validate_size($attribute, $value, $parameters)
|
||||||
{
|
{
|
||||||
return $this->get_size($attribute) == $parameters[0];
|
return $this->get_size($attribute, $value) == $parameters[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -286,7 +284,7 @@ protected function validate_size($attribute, $value, $parameters)
|
||||||
*/
|
*/
|
||||||
protected function validate_between($attribute, $value, $parameters)
|
protected function validate_between($attribute, $value, $parameters)
|
||||||
{
|
{
|
||||||
return $this->get_size($attribute) >= $parameters[0] and $this->get_size($attribute) <= $parameters[1];
|
return $this->get_size($attribute, $value) >= $parameters[0] and $this->get_size($attribute, $value) <= $parameters[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -299,7 +297,7 @@ protected function validate_between($attribute, $value, $parameters)
|
||||||
*/
|
*/
|
||||||
protected function validate_min($attribute, $value, $parameters)
|
protected function validate_min($attribute, $value, $parameters)
|
||||||
{
|
{
|
||||||
return $this->get_size($attribute) >= $parameters[0];
|
return $this->get_size($attribute, $value) >= $parameters[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -312,25 +310,28 @@ protected function validate_min($attribute, $value, $parameters)
|
||||||
*/
|
*/
|
||||||
protected function validate_max($attribute, $value, $parameters)
|
protected function validate_max($attribute, $value, $parameters)
|
||||||
{
|
{
|
||||||
return $this->get_size($attribute) <= $parameters[0];
|
return $this->get_size($attribute, $value) <= $parameters[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the size of an attribute.
|
* Get the size of an attribute.
|
||||||
*
|
*
|
||||||
|
* This method will determine if the attribute is a number, string, or file and
|
||||||
|
* return the proper size accordingly. If it is a number, then number itself is
|
||||||
|
* the size; if it is a file, the size is kilobytes in the size; if it is a
|
||||||
|
* string, the length is the size.
|
||||||
|
*
|
||||||
* @param string $attribute
|
* @param string $attribute
|
||||||
|
* @param mixed $value
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
protected function get_size($attribute)
|
protected function get_size($attribute, $value)
|
||||||
{
|
{
|
||||||
if (is_numeric($this->attributes[$attribute]) and $this->has_rule($attribute, $this->numeric_rules))
|
if (is_numeric($value) and $this->has_rule($attribute, $this->numeric_rules))
|
||||||
{
|
{
|
||||||
return $this->attributes[$attribute];
|
return $this->attributes[$attribute];
|
||||||
}
|
}
|
||||||
|
elseif (array_key_exists($attribute, Input::file()))
|
||||||
$value = $this->attributes[$attribute];
|
|
||||||
|
|
||||||
if (array_key_exists($attribute, Input::file()))
|
|
||||||
{
|
{
|
||||||
return $value['size'] / 1024;
|
return $value['size'] / 1024;
|
||||||
}
|
}
|
||||||
|
@ -501,7 +502,7 @@ protected function validate_mimes($attribute, $parameters)
|
||||||
* @param string $rule
|
* @param string $rule
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function get_message($attribute, $rule)
|
protected function message($attribute, $rule)
|
||||||
{
|
{
|
||||||
if (array_key_exists($attribute.'_'.$rule, $this->messages))
|
if (array_key_exists($attribute.'_'.$rule, $this->messages))
|
||||||
{
|
{
|
||||||
|
@ -537,7 +538,7 @@ protected function get_message($attribute, $rule)
|
||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function format_message($message, $attribute, $rule, $parameters)
|
protected function format($message, $attribute, $rule, $parameters)
|
||||||
{
|
{
|
||||||
// First we will get the language line for the attribute being validated.
|
// First we will get the language line for the attribute being validated.
|
||||||
// Storing attribute names in a validation file allows the easily replacement
|
// Storing attribute names in a validation file allows the easily replacement
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use Laravel\Lang;
|
||||||
use Laravel\Validation\Validator;
|
use Laravel\Validation\Validator;
|
||||||
|
|
||||||
class ValidatorTest extends PHPUnit_Framework_TestCase {
|
class ValidatorTest extends PHPUnit_Framework_TestCase {
|
||||||
|
@ -10,6 +11,7 @@ public function test_simple_group_of_validations()
|
||||||
'email' => 'required|email',
|
'email' => 'required|email',
|
||||||
'password' => 'required|confirmed|min:6',
|
'password' => 'required|confirmed|min:6',
|
||||||
'name' => 'required|alpha',
|
'name' => 'required|alpha',
|
||||||
|
'age' => 'required',
|
||||||
);
|
);
|
||||||
|
|
||||||
$attributes = array(
|
$attributes = array(
|
||||||
|
@ -24,7 +26,13 @@ public function test_simple_group_of_validations()
|
||||||
$validator = Validator::make($attributes, $rules, $messages);
|
$validator = Validator::make($attributes, $rules, $messages);
|
||||||
|
|
||||||
$this->assertFalse($validator->valid());
|
$this->assertFalse($validator->valid());
|
||||||
|
$this->assertTrue($validator->errors->has('name'));
|
||||||
|
$this->assertTrue($validator->errors->has('email'));
|
||||||
$this->assertFalse($validator->errors->has('password'));
|
$this->assertFalse($validator->errors->has('password'));
|
||||||
|
$this->assertEquals(count($validator->errors->get('name')), 1);
|
||||||
|
$this->assertEquals($validator->errors->first('name'), 'The name must be alphabetic!');
|
||||||
|
$this->assertEquals($validator->errors->first('email'), Lang::line('validation.email', array('attribute' => 'email'))->get());
|
||||||
|
$this->assertEquals($validator->errors->first('age'), Lang::line('validation.required', array('attribute' => 'age'))->get());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue