webin2015
(Webin2015)
June 27, 2015, 11:59am
1
hi community,
I have stuck in my project.
i have unique validation of email and username.
also have profile pic to upload on user creation.
usermodel:
[['name', 'username','email','pass','profilepic'], 'required'],
[['email','username'], 'unique', 'targetClass' => '\backend\models\User', 'message' => 'This email or username has already been taken.'],
usercontroller:
to validate unique filed i need to use $model->validate();
if ($model->load(Yii::$app->request->post()) && $model->validate())
{
}
my issue is if im using $model->validate() profilepic is get null after submitting form.
if im not using validate() unique validation is not working.
can someone please help me in this.
regards.
nguyendh
(Duynguyen0511)
June 27, 2015, 11:28pm
2
hi community,
I have stuck in my project.
i have unique validation of email and username.
also have profile pic to upload on user creation.
usermodel:
[['name', 'username','email','pass','profilepic'], 'required'],
[['email','username'], 'unique', 'targetClass' => '\backend\models\User', 'message' => 'This email or username has already been taken.'],
usercontroller:
to validate unique filed i need to use $model->validate();
if ($model->load(Yii::$app->request->post()) && $model->validate())
{
}
my issue is if im using $model->validate() profilepic is get null after submitting form.
if im not using validate() unique validation is not working.
can someone please help me in this.
regards.
if you post controller, model and view code. Then people may be able to help. cant do much with the tiny snippet
webin2015
(Webin2015)
June 29, 2015, 5:02am
3
#sitecontroller: #
public function actionUpload()
{
$model = new User();
$model->scenario = 'upload';
if ($model->load(Yii::$app->request->post()) ) {
if($model->validate())
{
$file = UploadedFile::getInstance($model, 'profilepic');
if($file){
$file->saveAs(\Yii::$app->basePath.'/web/uploads/'.$file->name);
$model->profilepic = $file->name;
$model->save(false);
Yii::$app->getSession()->setFlash('success', 'Successfully registered.');
return $this->render('upload', [
'model' => $model,
]);
}
}
else
{
echo "<pre>";
print_r($model->geterrors());exit;
Yii::$app->getSession()->setFlash('error', 'Error in registration.');
return $this->render('upload', [
'model' => $model,
]);
}
}
#site /upload#
<?php $form = ActiveForm::begin(['id' => 'form-signup',
'type' => ActiveForm::TYPE_HORIZONTAL,
'formConfig' => ['showErrors' => true,'showLabels' => true],
'options' => ['enctype' => 'multipart/form-data']
]);
?>
<div class="form-group kv-fieldset-inline">
<?= $form->field($model, 'profilepic')->widget(FileInput::classname(),[ 'pluginOptions' => ['showUpload' => false]]); ?>
</div>
<div class="form-group">
<?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
</div>
<?php ActiveForm::end(); ?>
#User Model#
public function rules()
{
return [
['profilepic', 'required'],
[['profilepic'], 'required', 'on' => 'upload'],
['profilepic', 'file', 'extensions'=>'jpg, jpeg, png'],
];
}
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios['upload'] = ['profilepic'];//Scenario Values Only Accepted
return $scenarios;
}
webin2015
(Webin2015)
June 29, 2015, 5:09am
4
validate() method is not working with file upload.
uwxc
(Dmitriy Krista)
June 30, 2015, 11:00am
5
webin
I think you should call validate() (or save()) method before you call getInstance(). Look at this code, it may be helpful:
if (Yii::$app->request->isPost) {
$model->load(Yii::$app->request->post());
$model->profilepic = UploadedFile::getInstance($model, 'profilepic');
if ($model->validate()) {
// do your things...
}
}
webin2015
(Webin2015)
June 30, 2015, 12:02pm
6
uwxc:
webin
I think you should call validate() (or save()) method before you call getInstance(). Look at this code, it may be helpful:
if (Yii::$app->request->isPost) {
$model->load(Yii::$app->request->post());
$model->profilepic = UploadedFile::getInstance($model, 'profilepic');
if ($model->validate()) {
// do your things...
}
}
still facing the same issue
can nyone please guide me to solve this?