php - Yii 2 file input renders hidden file input tag -
i using yii2 build simple application signup feature. problem when render file input tag using active forms, render file input field , hidden field. validator picks 1 hidden , says profile image required though saves in upload directory , adds path database still returns error. help.
here code: view:
<?php $form = activeform::begin(['id' => 'form-signup' , 'options' => ['enctype' => 'multipart/form-data']]); ?> <?= $form->field($model, 'username') ?> <?= $form->field($model, 'email') ?> <?= $form->field($model, 'profile_path')->widget(fileinput::classname(), [ 'options' => ['accept' => 'image/*'], ]); ?> <?= $form->field($model, 'password')->passwordinput() ?> <div class="form-group"> <?= html::submitbutton('signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?> </div> <?php activeform::end(); ?> signupform // model class
class signupform extends model { public $username; public $email; public $password; public $profile_path; /** * @inheritdoc */ public function rules() { return [ ['username', 'filter', 'filter' => 'trim'], ['username', 'required'], ['username', 'unique', 'targetclass' => '\common\models\user', 'message' => 'this username has been taken.'], ['username', 'string', 'min' => 2, 'max' => 255], ['email', 'filter', 'filter' => 'trim'], ['email', 'required'], ['email', 'email'], ['email', 'string', 'max' => 255], ['email', 'unique', 'targetclass' => '\common\models\user', 'message' => 'this email address has been taken.'], ['password', 'required'], ['password', 'string', 'min' => 6], [['profile_path'], 'file', 'skiponempty' => false, 'extensions' => 'png, jpg'], ]; } /** * signs user up. * * @return user|null saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new user(); $user->username = $this->username; $user->email = $this->email; $user->setpassword($this->password); $user->generateauthkey(); $user->setprofilepicture($this->profile_path); if ($user->save(false)) { return $user; } } return null; } public function upload() { if ($this->validate()) { $this->profile_path->saveas('uploads/' . $this->profile_path->basename . date('y-m-d h:i:s') . '.' . $this->profile_path->extension); $this->profile_path = 'uploads/' . $this->profile_path->basename . '.' . $this->profile_path->extension; return true; } else { return false; } } }
output:
<label class="control-label" for="signupform-profile_path">profile path</label> <input type="hidden" value="" name="signupform[profile_path]"> <input id="signupform-profile_path" type="file" name="signupform[profile_path]"> <p class="help-block help-block-error">please upload file.</p>
i think should use different scenario avoiding validation of input hidden when not necessary.. see doc brief guide
and sample of scenario use
define rules , scenarios
<?php class user extends model { public $name; public $email; public $password; public function rules(){ return [ [['name','email','password'],'required'], ['email','email'], [['name', 'email', 'password'], 'required', 'on' => 'register'], ]; } public function scenarios() { $scenarios = parent::scenarios(); $scenarios['login'] = ['name','password'];//scenario values accepted return $scenarios; } } ?> apply scenario
<?php ... class usercontroller extends controller { .. // apply scenarios // scenario set property ............ public function actionlogin(){ $model = new user; $model->scenario = 'login'; ............. } // scenario set through configuration public function actionregister(){ $model = new user(['scenario' => 'register']); .............. } } in login scenario name , password required in register scenario name, email , password required
here good examples for hidden input fields in yii2 https://mirakmalsulton.github.io/notes/2019/08/07/yii2-hidden-input-value
ReplyDelete