mirror of
https://github.com/yiisoft/yii.git
synced 2026-03-10 10:06:58 +01:00
Added CFileValidator and CUploadedFile.
This commit is contained in:
48
framework/validators/CFileValidator.php
Normal file
48
framework/validators/CFileValidator.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* CFileValidator class file.
|
||||
*
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright © 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
/**
|
||||
* CFileValidator verifies if the attribute is receiving a valid uploaded file.
|
||||
*
|
||||
*
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @version $Id$
|
||||
* @package system.validators
|
||||
* @since 1.0
|
||||
*/
|
||||
class CFileValidator extends CValidator
|
||||
{
|
||||
/**
|
||||
* @var boolean whether the attribute value can be null or empty. Defaults to true,
|
||||
* meaning that if the attribute is empty, it is considered valid.
|
||||
*/
|
||||
public $allowEmpty=true;
|
||||
|
||||
/**
|
||||
* Validates the attribute of the object.
|
||||
* If there is any error, the error message is added to the object.
|
||||
* @param CModel the object being validated
|
||||
* @param string the attribute being validated
|
||||
*/
|
||||
protected function validateAttribute($object,$attribute)
|
||||
{
|
||||
$value=$object->$attribute;
|
||||
if($this->allowEmpty && ($value===null || $value===''))
|
||||
return;
|
||||
|
||||
$file=CUploadedFile::getInstance(get_class($object).'['.$attribute.']');
|
||||
|
||||
if(!$valid)
|
||||
{
|
||||
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be {type}.',array('{type}'=>$this->type));
|
||||
$this->addError($object,$attribute,$message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user