I have a form field whose required validation work when submit is clicked without attaching file. But when I attach the file after clicking submit, the required validation doesn't return valid which stays the file field as required itself
form.php
$form
->field($model, 'document_name')
->fileInput(['accept'=>'image/jpeg, application/pdf, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document']);
model.php
[['document_name'], 'required'],
[['document_name'], 'file',
'maxSize' => 1024 * 1024, //1MB
'tooBig' => Yii::t('app', 'File size cannot exceed 1MB'),
'extensions' => 'jpg, pdf, doc, docx',
'wrongExtension' => Yii::t('app', 'Only files with these extensions are allowed: jpg, pdf, doc, docx') ],
// Tried custom validator
// [['document_name'], 'validate_document_name', 'skipOnEmpty'=> false],
// function validate_document_name($attribute, $param) {
// if($this->attachment_required) {
// $file = UploadedFile::getInstance($this, $attribute);
// if (!$file) {
// $this->addError($attribute, Yii::t('app', 'Attachment required'));
// }
// }
// }
| Q | A
| ---------------- | ---
| Yii version | 2.0.11.2.
| PHP version | 7.0.23
| Operating system | Windows
Can you please help me what am missing?
Thank you for your question.
In order for this issue tracker to be effective, it should only contain bug reports and feature requests.
We advise you to use our community driven resources:
If you are confident that there is a bug in the framework, feel free to provide information on how to reproduce it. This issue will be closed for now.
_This is an automated comment, triggered by adding the label question._
@samdark Isn't this a bug.? or do we have a solution for it? If so, I was expecting an answer for this issue.
It's not. I feel there's something in how you handle submit in controller.
Am using the form in a modal view. Below is my code in controller.
public function actionRegistration($id, $urlFrom)
{
$model = new EventRegistration();
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()))
{
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if ( $model->load( Yii::$app->request->post() ))
{
.....
$uploadedFile_pdf = UploadedFile::getInstance($model, 'document_name');
if($uploadedFile_pdf) {
$model->document_name = 'article-' . date('Ymdhis') . '.' . $uploadedFile_pdf->extension;
}
if($model->save()){
if(!empty($uploadedFile_pdf)) {
$article_path = $this->getFilePath();
if (!file_exists($article_path)) {
FileHelper::createDirectory($article_path, 0777, true);
}
$uploadedFile_pdf->saveAs($article_path . $model->document_name);
}
}
Yii::$app->session->setFlash('success', Yii::t('app', 'successfull'));
if (isset($urlFrom)) {
return $this->redirect([$urlFrom]);
} else {
return $this->redirect(['/']);
}
}
else if (Yii::$app->request->isAjax)
{
return $this->renderAjax( 'create_form', [
'model' => $model,
'event_id' => $id,
]);
}
return $this->redirect(['/']);
}
Hello @sadiq-mc . I think there is some flaws in your action logic. I suggest you to do something like that, adapting on your needs:
.....
if ($model->load(Yii::$app->request->post())){
$model->document_name = UploadedFile::getInstance($model, 'document_name');
if ($model->validate()) {
// Save file
$fileName = 'article-' . date('Ymdhis') . '.' . $model->document_name->extension;
$model->document_name->saveAs($this->getFilePath() . '/' . $fileName); // You can move the folder creation logic to method getFilePath()
$model->document_name = $fileName;
// Save model
if ($model->save(false)) {
Yii::$app->session->setFlash('success', Yii::t('app', 'successfull'));
return $this->redirect(isset($urlFrom) ? [$urlFrom] : ['/']);
}
}
}
....
(note: code not tested)
Tips:
'skipOnEmpty' => false in your validation rule to force user to submit a fileMore info:
http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html
@berosoboy thanks for checking it.
Most helpful comment
Hello @sadiq-mc . I think there is some flaws in your action logic. I suggest you to do something like that, adapting on your needs:
(note: code not tested)
Tips:
'skipOnEmpty' => falsein your validation rule to force user to submit a fileMore info:
http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html