Although there are many separate file uploads and CForm (formbuilder) documents, there is no combination of the two examples.
Although there are many separate file uploads and CForm (form builder) documents, there is no combination of the two examples.
Model)
First, you need a file Upload model:FileUpload. php
true, 'types' => 'jpg, jpeg, gif, png'), ); } }
Note:> for more information about verification rules, see Reference: Model rules validation.
Array used for CForm
Create an array for CForm:UploadForm. php
'Upload your image', 'attributes' => array( 'enctype' => 'multipart/form-data', ), 'elements' => array( 'image' => array( 'type' => 'file', ), ), 'buttons' => array( 'reset' => array( 'type' => 'reset', 'label' => 'Reset', ), 'submit' => array( 'type' => 'submit', 'label' => 'Upload', ), ), );
View File)
Create a view file:Upload. php
user->hasFlash('success')): ?>
user->getFlash('success'); ?>
Image Upload
Controller)
Add controller and action ):
submitted('submit') && $form->validate()) { $form->model->image = CUploadedFile::getInstance($form->model, 'image'); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //do something with your image here //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Yii::app()->user->setFlash('success', 'File Uploaded'); $this->redirect(array('upload')); } $this->render('upload', array('form' => $form)); } }
The best method here is to perform some operations on the image in your FileUpload model. Note: You do not needFileUpload: imageAssign values, but you can provide a method to access it to achieve a strong model (model) to simplify the controller ).
This article translated from foreign language website, view the original, please click: http://www.yiiframework.com/wiki/265/file-uploads-in-cform-form-builder/