Yii 2文件输入渲染隐藏的文件输入标记


Yii 2 file input renders hidden file input tag

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

这是代码:视图:

<?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//型号类

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;
    }
}

}

输出:

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

我认为您应该使用不同的场景,避免在不必要的时候隐藏输入的验证。。有关的简要指南,请参阅此文档

这是使用的场景示例

定义规则和场景

<?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 Only Accepted
        return $scenarios;
    }
}
?>

应用场景

<?php
...
class UserController extends Controller
{
    ..
    // APPLY SCENARIOS
    // scenario is set as a property
    ............
    public function  actionLogin(){
        $model = new User;
        $model->scenario = 'login';
        .............
    }
    // scenario is set through configuration
    public function  actionRegister(){
        $model = new User(['scenario' => 'register']);
        ..............
    }
}

在登录场景中,注册场景名称中只需要名称和密码,需要电子邮件和密码

需要在hiddenOptions 中设置值属性

<?= $form->field($model, 'profile_path')->widget(FileInput::class, ['options' => ['accept' => 'image/*', 'hiddenOptions' => ['value' => $model->profile_path]]]) ?>

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

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