Yii 2文件输入呈现隐藏文件输入标记

时间:2016-01-04 14:10:07

标签: php yii2 yii2-advanced-app yii2-validation

我正在使用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>

3 个答案:

答案 0 :(得分:1)

我认为您应该使用不同的方案,避免在不必要时隐藏输入的验证。see this doc for a brief guide

这是场景使用的一个示例

定义规则和方案

<?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']);
        ..............
    }
}

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

答案 1 :(得分:1)

hiddenOptions中需要设置值属性

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

答案 2 :(得分:-1)

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

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