0

My Model code,

public function rules()
{
    return [            
        [['image'], 'safe'],
        ['image', 'file', 'types'=>'jpg, gif, png'],
    ];
}

Use Apartfrom types,validation message not display document save in database

1 Answer 1

3

To validate file type you should use property $extensions of FileValidator.

public function rules()
    {
        return [

            [['image'], 'safe'],
            [['image'], 'file', 'extensions'=>'jpg, gif, png'],
        ];
    }

Update

Ok, I've got this. Please check one more time the source link, especially the controller section. To validate your model, you have to use validate() function.

Example code:

$model->file = UploadedFile::getInstance($model, 'file');

if ($model->file && $model->validate()) {                
    $model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension);
}

Source.

Sign up to request clarification or add additional context in comments.

3 Comments

Use extensions also.Validation message not display
$fileupload = UploadedFile::getInstance($model, 'image'); if(!empty($fileupload)) { $fileupload->saveAs('uploads/' . $fileupload->baseName . '.' . $fileupload->extension); $model->image = $fileupload->baseName . '.' . $fileupload->extension; $model->save(); } Yii::$app->getSession()->setFlash('succ', 'Category added successfully'); return Yii::$app->getResponse()->redirect('/admin/category');
@Dileep kurane, don't be shy to upvote the answer if it helped you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.