added error type to validation rule.
This commit is contained in:
@@ -18,6 +18,14 @@ abstract class Rule {
|
|||||||
*/
|
*/
|
||||||
public $message;
|
public $message;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The error type. This is used for rules that have more than
|
||||||
|
* one type of error such as Size_Of and Upload_Of.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $error;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new validation Rule instance.
|
* Create a new validation Rule instance.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -61,16 +61,19 @@ class Size_Of extends Rule {
|
|||||||
{
|
{
|
||||||
if ( ! is_null($this->length) and $attributes[$attribute] !== $this->length)
|
if ( ! is_null($this->length) and $attributes[$attribute] !== $this->length)
|
||||||
{
|
{
|
||||||
|
$this->error = 'number_wrong_size';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! is_null($this->maximum) and $attributes[$attribute] > $this->maximum)
|
if ( ! is_null($this->maximum) and $attributes[$attribute] > $this->maximum)
|
||||||
{
|
{
|
||||||
|
$this->error = 'number_too_big';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! is_null($this->minimum and $attributes[$attribute] < $this->minimum))
|
if ( ! is_null($this->minimum and $attributes[$attribute] < $this->minimum))
|
||||||
{
|
{
|
||||||
|
$this->error = 'number_too_small';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,16 +93,19 @@ class Size_Of extends Rule {
|
|||||||
|
|
||||||
if ( ! is_null($this->length) and Str::length($value) !== $this->length)
|
if ( ! is_null($this->length) and Str::length($value) !== $this->length)
|
||||||
{
|
{
|
||||||
|
$this->error = 'string_wrong_size';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! is_null($this->maximum) and Str::length($value) > $this->maximum)
|
if ( ! is_null($this->maximum) and Str::length($value) > $this->maximum)
|
||||||
{
|
{
|
||||||
|
$this->error = 'string_too_big';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! is_null($this->minimum) and Str::length($value) < $this->minimum)
|
if ( ! is_null($this->minimum) and Str::length($value) < $this->minimum)
|
||||||
{
|
{
|
||||||
|
$this->error = 'string_too_small';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,11 +7,11 @@ use System\Validation\Rule;
|
|||||||
class Upload_Of extends Rule {
|
class Upload_Of extends Rule {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The acceptable file extensions.
|
* The acceptable file types.
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $extensions;
|
public $types = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The maximum file size in bytes.
|
* The maximum file size in bytes.
|
||||||
@@ -38,25 +38,30 @@ class Upload_Of extends Rule {
|
|||||||
|
|
||||||
if ( ! is_null($this->maximum) and $file['size'] > $this->maximum)
|
if ( ! is_null($this->maximum) and $file['size'] > $this->maximum)
|
||||||
{
|
{
|
||||||
|
$this->error = 'file_too_big';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! is_null($this->extensions) and ! in_array(File::extension($file['name']), $this->extensions))
|
foreach ($this->types as $type)
|
||||||
{
|
{
|
||||||
return false;
|
if ( ! File::is($type, $file['tmp_name']))
|
||||||
|
{
|
||||||
|
$this->error = 'file_wrong_type';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the acceptable file extensions.
|
* Set the acceptable file types.
|
||||||
*
|
*
|
||||||
* @return Upload_Of
|
* @return Upload_Of
|
||||||
*/
|
*/
|
||||||
public function has_extension()
|
public function is()
|
||||||
{
|
{
|
||||||
$this->extensions = func_get_args();
|
$this->types = func_get_args();
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user