Hello,
i tried to use yii2 dynamic form (https://github.com/wbraganca/yii2-dynamicform )
but i allways end with this error :
Attribute name must contain word characters only.
looks the problem come from here ($i) inside form
<?= $form->field($modelVmproductvariation, "[{$i}]vmp_regtarif")->textInput(['maxlength' => true]) ?>
or maybe on the model line ? in the widget ?
'model' => $modelsVmproductvariation[0],
If someone has an idea… 2 days i’m stuck with this, looks like i don’t look where i shoud…
InsaneSkull
(Insane Skull)
September 18, 2019, 11:01am
2
How do you load and validate multiple models in controller?
Thank’s for your interest!
so in my controller :
i have those :
use Yii;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use common\models\Vmproducts;
use common\models\Vmproductvariation;
use common\models\VmproductsSearch;
use common\models\files;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\UploadedFile;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
and for now, without work on the saving part , my actionCreate is :
public function actionCreate()
{
$model = new Vmproducts();
$modelsVmproductvariation = [new Vmproductvariation];
$upload = new Files();
$model->date_creation = date("Y-m-d H:i:s");
$model->vmproduct_state = 'todo';
if ($model->load(Yii::$app->request->post()) ) {
$upload->file_url = UploadedFile::getInstances($upload, 'file_url');
$title = str_replace(' ', '_', $model->vmproduct_titre);
if( $model->validate() && $model->save()){
if($upload->file_url){
$upload->saveMyFile($model,'vmproducts',$upload->file_url,$title);
}
}
Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Produit enregistrée'));
return $this->redirect(['index']);
}
else{
return $this->render('create', [
'model' => $model,
'upload'=> $upload,
'modelsVmproductvariation' => (empty($modelsVmproductvariation)) ? [new Vmproductvariation] : $modelsVmproductvariation
]);
}
}
Problem resolved… DON’T let any space in the formFIeld array, and everything works
'formFields' => [
'vmp_regtarif',
'vmp_promotarif',
'vmp_stock',
'vmp_poids',
'vmp_profondeur',
'vmp_largeur',
'vmp_hauteur',
'vmp_materiau',
'vmp_taille',
'vmp_type'
],