Yii 2 文件输入呈现隐藏文件输入标签
Yii 2 file input renders hidden file input tag
我正在使用 yii2 构建一个具有注册功能的简单应用程序。问题是当我使用活动表单呈现文件输入标签时,它呈现文件输入字段和隐藏字段。验证器然后选择隐藏的那个,并且总是说需要配置文件图像,尽管它将它保存在我的上传目录中,并将路径添加到数据库,但仍然 returns 并出现此错误。谢谢你的帮助。
代码如下:
查看:
<?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
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>
我认为你应该使用不同的场景,避免在不必要时隐藏输入验证.. 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']);
..............
}
}
登录场景只需要姓名和密码,注册场景需要邮箱和密码
好的,伙计们,我发现问题出在我使用的验证器上。使用图像验证器而不是文件验证器解决了我的问题。这是用于验证的更新代码。
['profile_path', 'image', 'extensions' => 'png, jpg',
'minWidth' => 100, 'maxWidth' => 2000,
'minHeight' => 100, 'maxHeight' => 2000,
],
需要在 hiddenOptions 中设置 value 属性
<?= $form->field($model, 'profile_path')->widget(FileInput::class, ['options' => ['accept' => 'image/*', 'hiddenOptions' => ['value' => $model->profile_path]]]) ?>
我正在使用 yii2 构建一个具有注册功能的简单应用程序。问题是当我使用活动表单呈现文件输入标签时,它呈现文件输入字段和隐藏字段。验证器然后选择隐藏的那个,并且总是说需要配置文件图像,尽管它将它保存在我的上传目录中,并将路径添加到数据库,但仍然 returns 并出现此错误。谢谢你的帮助。
代码如下: 查看:
<?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
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>
我认为你应该使用不同的场景,避免在不必要时隐藏输入验证.. 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']);
..............
}
}
登录场景只需要姓名和密码,注册场景需要邮箱和密码
好的,伙计们,我发现问题出在我使用的验证器上。使用图像验证器而不是文件验证器解决了我的问题。这是用于验证的更新代码。
['profile_path', 'image', 'extensions' => 'png, jpg',
'minWidth' => 100, 'maxWidth' => 2000,
'minHeight' => 100, 'maxHeight' => 2000,
],
需要在 hiddenOptions 中设置 value 属性
<?= $form->field($model, 'profile_path')->widget(FileInput::class, ['options' => ['accept' => 'image/*', 'hiddenOptions' => ['value' => $model->profile_path]]]) ?>