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