我用
Yii 1
。 (我很羞耻!)
型号:
<?php
class GalleryForm extends CFormModel
{
public $photos;
public function rules()
{
return array(
array('photos', 'file', 'types'=>'jpg, gif, png', 'allowEmpty'=>true,),
);
}
}
查看:
<?php
$model = new GalleryForm();
$this->widget('CMultiFileUpload', array(
'model'=>$model,
'attribute'=>'photos',
'accept'=>'jpg|gif|png',
'max'=>10,
));
如何访问模型的
types
属性的rules
或photos
?jpg, gif, png
字符串。
我找到解决方案:
$types = null;
$validators = $model->getValidators('photos');
foreach($validators as $validator) {
if ($validator instanceof CFileValidator) {
$types = $validator->types;
break;
}
}
echo $types; //jpg, gif, png
还有额外的代码:
$types = array_map('strtolower', array_map('trim', explode(',', $types)));
var_dump($types);
//array
// 0 => string 'jpg' (length=3)
// 1 => string 'gif' (length=3)
// 2 => string 'png' (length=3)