I'm creating a Yii2 activeForm for creating and editing a product, and want the page to manage images as well. I want to be able to upload (add) images for the product, and then have a sortable preview region where the images show up and can be sorted to the right order. My plan is to upload files via ajax/pjax to a temporary folder where the form will pull from to display them in the sortable region, and upon saving the model, the images will be moved/named accordingly.
Initially I'm using Kartik's FileInput widget (which may be unnecessary) and Kartik's Sortable Input for re-ordering the images.
I believe I can handle the file uploading and moving/renaming, my question is how do I upload the images separate from the rest of the form, and then have the ActiveForm pull them into the sortable input area? Additionally, I'll want to be able to delete images from it as well (with a little x in the corner of the thumbnail). I'm familiar with using pjax to wrap grid-views and even using pjax to submit a form to a pjax method (for use inside a modal), but not sure how to use it inside another form and make the other form dynamically load the images.
To clarify, I have a Product that extends ActiveRecord, and a ImageForm that extends Model. I then have ProductController with the following Create action:
public function actionCreate()
{
$model = new Product();
$images = new ImageForm();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
'images' => $images,
]);
}
My goal is to be able to upload images (through ImageForm) to a temporary location that is displayed on the Product's create view. However currently, using the upload function in the widget merely reloads the page and the images are gone.
formModelthat extendsyii\base\Modelto upload the file using thekartik\FileInputI don't see any problem in doing that your normal form will have a separate model which is extended from\yii\db\ActiveRecord, you can use the events provided byFileInputwidget to refresh thedivin which you want to show the uploaded files or make a separateactionin yourcontrollerthat is called by thiseventusingajax- Muhammad Omer Aslamkartik\FileInputyou dont have to write the javascript to post the picture it has the option to trigger upload as soon you select the file and as it usesActiveFormandmodelso yes it is ayiiway of doing the upload it will even show you the validation too from model if it fails. like if size limit increases - Muhammad Omer Aslam$form->field()->widgetscenario, would I have to embed another activeForm inside the first to make this work? And once the upload is complete how do I trigger the refresh? Is that custom javascript or is there a way to trigger it otherwise? I'm going to work on it a bit and will update my question as appropriate. - sharfFileInputyou are using should use the sameActiveFormbut a different model object that would be of yourUploadFormclass i.e$form->field('imagefileName',$uploadModel)->widget()- Muhammad Omer Aslam