phpmodelyiirulesyii1.x

Access to `types` of `rules` of `photos` attribute of model


I use Yii 1. (I am ashamed!)

Model:

<?php
class GalleryForm extends CFormModel
{
    public $photos;

    public function rules()
    {
        return array(
            array('photos', 'file', 'types'=>'jpg, gif, png', 'allowEmpty'=>true,),
        );
    }
}

View:

<?php
$model = new GalleryForm();
$this->widget('CMultiFileUpload', array(
    'model'=>$model,
    'attribute'=>'photos',
    'accept'=>'jpg|gif|png',
    'max'=>10,
));

How can I access to types of rules of photos attribute of model?
I need use jpg, gif, png string in the view.


Solution

  • I find solution:

    $types = null;
    $validators = $model->getValidators('photos');
    foreach($validators as $validator) {
        if ($validator instanceof CFileValidator) {
            $types = $validator->types;
            break;
        }
    }
    echo $types; //jpg, gif, png
    

    And extra codes:

    $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)