yii2 File and Image validators not working in DynamicModel

Created on 30 Sep 2018  路  6Comments  路  Source: yiisoft/yii2

$model = new yii\base\DynamicModel(['file']);
$model->AddRule('file','required');
$model->AddRule('file','file');
$model->load(yii::$app->request->post(),'');
$model->file = yii\web\UploadedFile::getInstance($model,'file');
var_dump($model->validate()); #return false
var_dump($model->file === null);  #return true

$nonDynamicModel = new TestCaseModel();
$nonDynamicModel->load(yii::$app->request->post(),'');
$nonDynamicModel->file = yii\web\UploadedFile::getInstance($nonDynamicModel,'file');
var_dump($nonDynamicModel->validate()); //return true
var_dump($nonDynamicModel->file === null);  #return false

file or image validator not working only in DynamicModel but working in non dynamic model.

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0.15.1

to be verified bug

Most helpful comment

thanks @bizley. you're right. DynamicModel not have any way for set formName.

this class can be help me

namespace app\classes;
use yii;

class DynamicModel extends yii\base\DynamicModel{

    public $labels = [];
    public $formName = '';

    public function attributeLabels(){
        return $this->labels;
    }

    public function formName(){
        return $this->formName;
    }
}

All 6 comments

Failed to reproduce, everything works fine for file and image validator in DynamicModel.

class TestController extends \yii\web\Controller
{
    public function actionTest()
    {
        $model = new \yii\base\DynamicModel(['file']);
        $model->addRule('file','required');
        $model->addRule('file','file');

        if ($model->load(\Yii::$app->request->post(),'')) {
            $model->file = \yii\web\UploadedFile::getInstance($model, 'file');

            var_dump($model->validate());
            var_dump($model->file);
            die;
        }

        return $this->render('test', ['model' => $model]);
    }
}

view:

$form = \yii\widgets\ActiveForm::begin();
echo $form->field($model, 'file')->fileInput();
echo \yii\helpers\Html::submitButton();
\yii\widgets\ActiveForm::end();

output:

boolean true
object(yii\web\UploadedFile)

@ziaratban any specific details we're missing?

Thanks for posting in our issue tracker.
In order to properly assist you, we need additional information:

  • When does the issue occur?
  • What do you see?
  • What was the expected result?
  • Can you supply us with a stacktrace? (optional)
  • Do you have exact code to reproduce it? Maybe a PHPUnit tests that fails? (optional)

Thanks!

_This is an automated comment, triggered by adding the label status:need more info._

@samdark and @bizley , this is my test.

_app\controllers\TestController.php_

<?php
namespace app\controllers;

use yii;

class TestCaseModel extends yii\base\Model{

    public $file;

    public function formName(){
        return '';
    }

    public function rules(){
        return [
            ['file','required'],
            ['file','file']
        ];
    }

}

class TestController extends yii\web\Controller{

    public function actionIndex(){

        $dmodel = $ndmodel = false;

        if(yii::$app->request->post('start')){
            $dmodel = new yii\base\DynamicModel(['file']);
            $dmodel->AddRule('file','required');
            $dmodel->AddRule('file','file');
            $dmodel->load(yii::$app->request->post(),'');
            $dmodel->file = yii\web\UploadedFile::getInstance($dmodel,'file');

            $ndmodel = new TestCaseModel();
            $ndmodel->load(yii::$app->request->post(),'');
            $ndmodel->file = yii\web\UploadedFile::getInstance($ndmodel,'file');
        }

        return $this->renderPartial('index',['dmodel' => $dmodel,'ndmodel' => $ndmodel]);
    }

}

_app\views\test\index.php_

<!DOCTYPE html>
<html>
    <head>
        <title>Test Model :)</title>
        <meta charset="utf8">
    </head>
    <body>
<?php
if($dmodel !==  false){
    var_dump($dmodel->validate());
    echo '<br>';
    var_dump($dmodel->file === null);
    echo '<br>';
    echo '<br>';
    var_dump($ndmodel->validate());
    echo '<br>';
    var_dump($ndmodel->file === null);
}
?>
        <form enctype="multipart/form-data" method="post">
            <input type="hidden" name="start" value="1">
            <input type="file" name="file">
            <input type="submit" value="send">
        </form>
    </body>
</html>

request screenshot
image

output :

bool(false)
bool(true)

bool(true)
bool(false) 

As I said - all is fine.

You have got your results because of few things:

  1. You have overwritten formName() in TestCaseModel and because of that yii\web\UploadedFile::getInstance($ndmodel,'file') returns proper file field name.
  2. You should use yii\web\UploadedFile::getInstanceByName('file') instead of yii\web\UploadedFile::getInstance($dmodel,'file') because it expects DynamicModel[file] as file field name otherwise and does not see the file.

thanks @bizley. you're right. DynamicModel not have any way for set formName.

this class can be help me

namespace app\classes;
use yii;

class DynamicModel extends yii\base\DynamicModel{

    public $labels = [];
    public $formName = '';

    public function attributeLabels(){
        return $this->labels;
    }

    public function formName(){
        return $this->formName;
    }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

indicalabs picture indicalabs  路  3Comments

psfpro picture psfpro  路  3Comments

Locustv2 picture Locustv2  路  3Comments

AstRonin picture AstRonin  路  3Comments

Kolyunya picture Kolyunya  路  3Comments