Yii 2 文件输入渲染隐藏文件输入标签 [英] Yii 2 file input renders hidden file input tag

查看:21
本文介绍了Yii 2 文件输入渲染隐藏文件输入标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 yii2 构建一个具有注册功能的简单应用程序.问题是当我使用活动表单呈现文件输入标签时,它呈现一个文件输入字段和一个隐藏字段.然后验证器选择隐藏的那个,并且总是说需要配置文件图像,尽管它将它保存在我的上传目录中,并将路径添加到数据库,但仍然返回此错误.谢谢你的帮助.

代码如下:查看:

 'form-signup' , 'options' => ['enctype' => 'multipart/form-data']]);?><?= $form->field($model, 'username') ?><?= $form->field($model, 'email') ?><?= $form->field($model, 'profile_path')->widget(FileInput::classname(), ['选项' =>['接受' =>'图片/*'],]);?><?= $form->field($model, 'password')->passwordInput() ?><div class="form-group"><?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>

<?php ActiveForm::end();?>

SignupForm//模型类

class SignupForm 扩展模型{公共 $ 用户名;公共 $email;公共 $password;公共 $profile_path;/*** @inheritdoc*/公共函数规则(){返回 [['用户名', '过滤器', '过滤器' =>'修剪'],['用户名','必填'],['用户名','唯一','targetClass' =>'\common\models\User', 'message' =>'此用户名已被占用.'],['用户名', '字符串', 'min' =>2,'最大'=>[255],['email', 'filter', 'filter' =>'修剪'],['电子邮件','必填'],['电子邮件','电子邮件'],['email', 'string', 'max' =>[255],['email', 'unique', 'targetClass' =>'\common\models\User', 'message' =>'此电子邮件地址已被占用.'],['需要密码'],['密码','字符串','分钟'=>6],[['profile_path'], 'file', 'skipOnEmpty' =>假,'扩展' =>'png, jpg'],];}/*** 注册用户.** @return User|null 保存的模型或 null 如果保存失败*/公共功能注册(){如果 ($this->validate()) {$user = 新用户();$user->username = $this->username;$user->email = $this->email;$user->setPassword($this->password);$user->generateAuthKey();$user->setProfilePicture($this->profile_path);如果 ($user->save(false)) {返回 $user;}}返回空;}公共函数上传(){如果 ($this->validate()) {$this->profile_path->saveAs('uploads/' . $this->profile_path->baseName .date('Ymd H:i:s') .'.' . $this->profile_path->扩展名);$this->profile_path = 'uploads/' .$this->profile_path->baseName .'.'.$this->profile_path->extension;返回真;} 别的 {返回假;}}

}

输出:

<label class="control-label" for="signupform-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">请上传文件.</p>

解决方案

好的,我发现问题出在我使用的验证器上.使用图像验证器而不是文件验证器解决了我的问题.这是用于验证的更新代码.

['profile_path', 'image', 'extensions' =>'png, jpg','minWidth' =>100, 'maxWidth' =>2000,'minHeight' =>100, 'maxHeight' =>2000,],

I am using yii2 to build a simple application with a signup feature. Problem is when I render a file input tag using active forms, it render a file input field and a hidden field. The validator then picks the one which is hidden and always says that profile image is required though it saves it in my upload directory and also adds the path to the database but still returns with this error. Thanks for any help.

Here is the 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 already 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 already been taken.'],

        ['password', 'required'],
        ['password', 'string', 'min' => 6],
        [['profile_path'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
    ];
}

/**
 * Signs user up.
 *
 * @return User|null the 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 a file.</p>

解决方案

Ok guys i figured out that the issue was with the validator i was using. Using image validator instead of file validator solved my problem. Here is the updated code for validation.

['profile_path', 'image', 'extensions' => 'png, jpg',
       'minWidth' => 100, 'maxWidth' => 2000,
       'minHeight' => 100, 'maxHeight' => 2000,
],

这篇关于Yii 2 文件输入渲染隐藏文件输入标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆