I created a form with ActiveForm which include one multiple file input. I enabled AJAX validation on submit but when I select "n" items and click the submit button, yii send data in background but the file validator return the next error message: "Please upload a files". Ok I understand that why do it but I don't know that what the best practise if I want to check max file numbers and sizes and required at least one file and I want to use ajax validation too. Perhaps the best practise if I don't use ajax validation and I change the skipOnEmpty from false to true...?
Example (not the real full code):
Model:
class Document extends Model {
public $name;
public $files;
public function rules()
{
return [
['name', 'required'],
['files', 'file',
'maxSize' => 1024 * 1024,
'maxFiles' => 5,
'skipOnEmpty' => false
]
];
}
public function create() { // other code }
}
Controller:
class DocumentController extends Model
{
public function create()
{
$model = new Document();
$model->load(Yii::$app->request->post());
$model->files = UploadedFile::getInstances($model, 'files');
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if ($model->validate() && $model->create()) {
// other code...
}
}
}
Thx guys!
Controller
extendsModel
? - Nebulosar