yii ajax xupload form submit is not working - php

Q1 : form submitting is not working.
Q2 : how to limit upload files (e.g 1 - 5 files only)
status : create a form with ajax upload xupload
My model (fadepreciation.php)
public function afterSave( ) {
$this->addImages( );
parent::afterSave( );
}
public function addImages( ) {
//If we have pending images
if( Yii::app( )->user->hasState( 'images' ) ) {
$userImages = Yii::app( )->user->getState( 'images' );
//Resolve the final path for our images
$path = Yii::app( )->getBasePath( )."/../images/uploads/{$this->id}/";
//Create the folder and give permissions if it doesnt exists
if( !is_dir( $path ) ) {
mkdir( $path );
chmod( $path, 0777 );
}
//Now lets create the corresponding models and move the files
foreach( $userImages as $image ) {
if( is_file( $image["path"] ) ) {
if( rename( $image["path"], $path.$image["filename"] ) ) {
chmod( $path.$image["filename"], 0777 );
$img = new Image( );
$img->size = $image["size"];
$img->mime = $image["mime"];
$img->name = $image["name"];
$img->source = "/images/uploads/{$this->id}/".$image["filename"];
$img->somemodel_id = $this->id;
if( !$img->save( ) ) {
//Its always good to log something
Yii::log( "Could not save Image:\n".CVarDumper::dumpAsString(
$img->getErrors( ) ), CLogger::LEVEL_ERROR );
//this exception will rollback the transaction
throw new Exception( 'Could not save Image');
}
}
} else {
//You can also throw an execption here to rollback the transaction
Yii::log( $image["path"]." is not a file", CLogger::LEVEL_WARNING );
}
}
//Clear the user's session
Yii::app( )->user->setState( 'images', null );
}
}
My view (_form.php)
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'fa-depreciation-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<!-- Other Fields... -->
<div class="row">
<?php echo $form->labelEx($model,'photos'); ?>
<?php
$this->widget( 'xupload.XUpload', array(
'url' => Yii::app( )->createUrl( "/fadepreciation/upload"),
//our XUploadForm
'model' => $photos,
//We set this for the widget to be able to target our own form
'htmlOptions' => array('id'=>'fa-depreciation-form'),
'attribute' => 'file',
'multiple' => true,
//Note that we are using a custom view for our widget
//Thats becase the default widget includes the 'form'
//which we don't want here
//'formView' => 'application.views.faDepreciation._form',
)
);
?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
My controller (fadepreciation.php)
public function actionCreate()
{
$model=new FaDepreciation;
Yii::import( "xupload.models.XUploadForm" );
$photos = new XUploadForm;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['FaDepreciation']))
{
//Assign our safe attributes
$model->attributes=$_POST['FaDepreciation'];
//Start a transaction in case something goes wrong
$transaction = Yii::app( )->db->beginTransaction( );
try {
//Save the model to the database
if($model->save()){
$transaction->commit();
$this->redirect(array('view','id'=>$model->id));
}
} catch(Exception $e) {
$transaction->rollback( );
Yii::app( )->handleException( $e );
}
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
Yii::import( "xupload.models.XUploadForm" );
$photos = new XUploadForm;
$this->render('create',array(
'model'=>$model,
'photos'=>$photos,
));
}
public function actionUpload( ) // From xupload nothing change

What you need to do is to create a custom form.
Copy the content from xupload _form and paste it removing the begin form - end form.
Add to your widget 'formView' the reference at the custom form.

what is the issue about submission form?
yes file limit can be done. Please make sure you follow these http://www.yiiframework.com/wiki/348/xupload-workflow/

Q1: form submition is not working, because the XUpload widget generates its own form tag. so your generated HTML has a form embebed in another form, you should use formView option of the widget to point to a view that has no form tags, as described in the xupload workflow wiki
Q2: You should use maxNumberOfFiles option in the widget config
It all should look like this:
<?php
$this->widget( 'xupload.XUpload', array(
'url' => Yii::app( )->createUrl( "/fadepreciation/upload"),
//our XUploadForm
'model' => $photos,
//We set this for the widget to be able to target our own form
'htmlOptions' => array('id'=>'fa-depreciation-form'),
'attribute' => 'file',
'multiple' => true,
//Note that we are using a custom view for our widget
//Thats becase the default widget includes the 'form'
//which we don't want here
'formView' => 'application.views.faDepreciation._form',
'options' => array('maxNumberOfFiles' => 5)
)
);
?>

Just use 'showForm' parameter as follow:
<?php
$this->widget( 'xupload.XUpload', array(
...
'showForm' => false,
...
));
?>
Maybe, this option been added in next versions of xupload.

I know that it's an old post but maybe this answer will help someone to solve this issue.
I found out that it's caused by the last line in the file /xupload/views/form.php (with default settings). It looks like the if statement is somehow working opposite... in mining that for false value it's rendering the code. For example:
<?php
echo $this->showForm;
if($this->showForm) echo CHtml::endForm();
echo $this->showForm;
?>
returns:
Maybe I'm missing something but it looks weird... isn't it?

Related

Yii PHP file uploader overwriting on update

I have a basic form in PHP framework Yii, the action to create works fine, however, when i update the record (for example if i'm not changing the file upload, but another field), it overwrites the file upload and blanks it, can anyone assist me? I've tried all the validation I can think of around the controller items, but no matter what i add it still blanks it on update.
Here's the view code
<?php $form=$this->beginWidget('booster.widgets.TbActiveForm', array(
'id'=>'company-form',
'enableAjaxValidation'=>false,
'method' => 'post',
'type' => 'horizontal',
'htmlOptions' => array(
'enctype' => 'multipart/form-data'
)
));
echo $form->textFieldGroup($model,'name',array('class'=>'col-md-5','maxlength'=>75));
echo $form->fileFieldGroup($model, 'logo',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-md-9',
),
'hint' => 'You can only upload jpg, png, gif\'s – max upload filesize is 1.5mb. Square images are advised.<br/>In certain browsers, you can also drag \' drop files into the dropzone.',
)
);
if($model->isNewRecord!='1'){ ?>
<div class="row">
<label class="col-md-3 control-label" style="padding-top: 25px;">Company image</label>
<div class="col-md-6">
<?php echo CHtml::image(Yii::app()->request->baseUrl.'/images/portraits/company/'.$model->logo,"logo",array("class"=>"img-polaroid placeholder")); ?>
</div>
</div>
<?php } ?>
And this is my actionUpdate function in Controller
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(isset($_POST['Company']))
{
$model->attributes=$_POST['Company'];
$uploadedFile=CUploadedFile::getInstance($model,'logo');
if (is_object($uploadedFile) && get_class($uploadedFile)==='CUploadedFile'){
if(!$uploadedFile == null){
$rnd = rand(0,9999);
$filename_preg1 = preg_replace("/[^a-zA-Z0-9.]/", '', "{$uploadedFile}");
$fileName = "{$rnd}-{$filename_preg1}";
$company = $model->name;
$model->logo = $fileName;
}
if($model->save()){
if(!empty($uploadedFile)){
$fullPath = Yii::app()->basePath . '/../images/portraits/company/' . $fileName;
$uploadedFile->saveAs($fullPath);
}
$this->redirect(array('view','id'=>$model->company_id));
}
}
if($model->save()){
$this->redirect(array('view','id'=>$model->company_id));
}
}
$this->render('update',array(
'model'=>$model,
));
}
Can anyone see where i'm going wrong?
UPDATE
Thanks SiZE, the code you gave me worked in one form and not in the other, the one it didn't work in has validation in the model rules
array('logo', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'),
This only works with the allowEmpty param here as it's not a required field, however, with the rule in place in the model, it still blanks the file field regardless, anyone have any more thoughts?
CActiveForm calls CHtml::activeFileField method wich generates empty hidden field to correctly work with model's rules.
You can try this:
$model=$this->loadModel($id);
$original_logo = $model->logo;
if(isset($_POST['Company'])) {
$model->attributes = $_POST['Company'];
$logo = CUploadedFile::getInstance($model, 'logo');
$model->logo = $logo !== null ? $logo->getName() : $original_logo;
if ($model->save()) {
if ($logo !== null) {
$logo->saveAs(/* specify path with file name here */);
}
$this->redirect(array('view','id'=>$model->company_id));
}
}

Upload multiple files in array with yii

I've created a form with field in array following this link http://www.yiiframework.com/doc/guide/1.1/en/form.table
and created a file field in array like this
echo $form->fileField($m, "[$i]myfile");
But now i've no idea what i'm going to do in controller to save the file paths and filename etc. I am able to save other information but not the upload file. I've tried with this but no luck.
$imageUpload = CUploadedFile::getInstance($models[$i],'name');
Try this way, but first ensure your form type is
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'htmlOptions'=>array(
'enctype' => 'multipart/form-data'
),
)); ?>
//Avatar Upload on controller start
$uploaded_file = CUploadedFile::getInstance($model,'avatar');
$main_image=null;
if($uploaded_file and $model->validate())
{
$main_image =time()."-".$model->username.".".$uploaded_file->getExtensionName();
$model->avatar=$uploaded_file;#initialize model attribute with file name
$model->avatar->saveAs(Yii::app()->basePath."/../images/profile-images/".$main_image);//this will upload selected image file
}
//Avatar Upload on controller end
This method need the browser support HTML5.
In view file:
$form=$this->beginWidget('CActiveForm', array(
'id'=>'ca-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
));
...
echo $form->fileField($m, "myfile[]", array('multiple'=>true));
In Controller:
$imageUploads = CUploadedFile::getInstances($models,'myfile');
foreach ($imageUploads as $imageUpload) {
...// Here you can use $imageUpload->name.
}
Notice the 's' of getInstances

Yii CMultiFileUpload select multiple files

Got The Answer
To upload multiple file in to the database for registration
have tried so many ways to make multiple file upload workable using CMultiFileUpload widget. Already, I have checked and followed below links-
http://www.yiiframework.com/forum/index.php/topic/47665-multiple-file-upload/
Yii multiple file upload
BUT still no luck!!
Error: storing the data but the files are not getting uploaded
please help
Here is my code:
In Form
<?php $this->widget('CMultiFileUpload',
array(
'model'=>$model,
'attribute' => 'documents',
'accept'=>'jpg|gif|png|doc|docx|pdf',
'denied'=>'Only doc,docx,pdf and txt are allowed',
'max'=>4,
'remove'=>'[x]',
'duplicate'=>'Already Selected',
)
);?>
Controller Code
public function actionRegistration()
{
$model=new PatientRegistration;
$this->performAjaxValidation($model);
if(isset($_POST['PatientRegistration']))
{
$model->attributes=$_POST['PatientRegistration'];
if($model->validate())
{
if(isset($_POST['PatientRegistration']))
{
if($filez=$this->uploadMultifile($model,'documents','/Images/'))
{
$model->documents=implode(",", $filez);
}
$model->attributes=$_POST['PatientRegistration'];
if($model->save())
{
// $this->render('registration',array('model'=>$model));
$this->redirect(array('/patientregistration/patientview','id'=>$model->register_id));
}
}
}
}
$this->render('registration',array('model'=>$model));
}
public function uploadMultifile($model,$attr,$path)
{
/*
* path when uploads folder is on site root.
* $path='/uploads/doc/'
*/
if($sfile=CUploadedFile::getInstances($model, $attr)){
foreach ($sfile as $i=>$file){
// $formatName=time().$i.'.'.$file->getExtensionName();
$fileName = "{$sfile[$i]}";
$formatName=time().$i.'_'.$fileName;
$file->saveAs(Yii::app()->basePath.$path.$formatName);
$ffile[$i]=$formatName;
}
return ($ffile);
}
}
Add in CActiveForm widget
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
),
Hence u can use this code to upload multiple files in yiiframework
Here is the simplest Code for Multiple File Upload in Yii Framework
code
In Controller
public function actionCreate()
{
$model = new Upload;
echo Yii::app()->basePath.'/Images/';
if(isset($_POST['Upload']))
{
if($filez=$this->uploadMultifile($model,'Document','/Images/'))
{
$model->Document=implode(",", $filez);
}
$model->attributes=$_POST['Upload'];
if ($model->save())
{
$this->redirect(array('view', 'id' => $model->idUpload));
}
}
$this->render('create', array(
'model' => $model,
));
}
//Function for uploading and saving Multiple files
public function uploadMultifile ($model,$attr,$path)
{
/*
* path when uploads folder is on site root.
* $path='/uploads/doc/'
*/
if($sfile=CUploadedFile::getInstances($model, $attr)){
foreach ($sfile as $i=>$file){
// $formatName=time().$i.'.'.$file->getExtensionName();
$fileName = "{$sfile[$i]}";
$formatName=time().$i.'_'.$fileName;
$file->saveAs(Yii::app()->basePath.$path.$formatName);
$ffile[$i]=$formatName;
}
return ($ffile);
}
}
In Form
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'upload-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
),
)); ?>
<?php $this->widget('CMultiFileUpload',
array(
'model'=>$model,
'attribute' => 'Document',
'accept'=>'jpg|gif|png|doc|docx|pdf',
'denied'=>'Only doc,docx,pdf and txt are allowed',
'max'=>4,
'remove'=>'[x]',
'duplicate'=>'Already Selected',
)
);?>
That was all up is for multiple upload - that's nice, but as for multiple select you can try this yii extension
I hope that this link will help someone, because I was struggling with multiple select files for multiple upload then. Spent lot of time in Google search. Cheers

Yiiframework Range Slider input

I have a user settings page where users can change the settings of the application. The users can input a number between 0 and 10. See screenshot: http://oi43.tinypic.com/2uerazp.jpg
Now I would like to transform the input fields into a range slider using the CJuiSliderInput widget of the yiiframework: http://www.yiiframework.com/doc/api/1.1/CJuiSliderInput#maxAttribute-detail
I cannot seem to get the code working, this is the current code in view:
<?php foreach($settings as $i=>$value): ?>
<?php $setting=UserSetting::model()->findByPk($value->setting_id); ?>
<h4><?php echo CHtml::encode($setting->label); ?></h3>
<?php echo CHtml::activeTextField($value,"[$i]value"); ?>
<?php endforeach; ?>
I would like to replace the activeTextField with
$this->widget('zii.widgets.jui.CJuiSliderInput',array(
'model'=>$model,
'attribute'=>'[$i]value',
'maxAttribute'=>'timeMax',
// additional javascript options for the slider plugin
'options'=>array(
'range'=>true,
'min'=>0,
'max'=>10,
),
));
What values do I need to fill in to the widget to get it to work? Each textField input is from a different model btw.
The controller looks something like this(don't know if you need it):
$settingsvalues = UserSettingValue::model()->findAll('client_id=:id', array(
':id' => $id,
));
if(isset($_POST['UserSettingValue'])){
$valid = true;
foreach($settingsvalues as $i=>$value){
if(isset($_POST['UserSettingValue'][$i]))
$value->attributes = $_POST['UserSettingValue'][$i];
$value->save();
$valid = $value->validate() && $valid;
}
if($valid)
$value->save();
}
$this->render('settings',array(
'model' => $model,
'settings' => $settingsvalues,
));
Thanks a lot!
I fixed the problem by putting a javascript function in the slider. It is not exactly as I intended in the beginning, but it'll do. The slider now changes the value in the input fields.
<?Php
$this->widget('zii.widgets.jui.CJuiSliderInput',array(
'name' => $i,
'model'=>$value,
'attribute'=>"value",
'event'=>'change',
//'value'=>'$value',
'options'=>array(
'min'=>0,
'max'=>10,
'animate' => true,
'slide'=>'js:function(event,ui){$("#UserSettingValue_'.$i.'_value").val(ui.value);}',
),
)); ?>
This was a little big to be a comment. But please try the following
$this->widget('zii.widgets.jui.CJuiSliderInput',array(
'name'=>'$setting->label',
'attribute'=>'[$i]value',
'value'=>$value,
'options'=>array(
'min'=>0,
'max'=>10,
),
));
Also please try this
$this->widget('zii.widgets.jui.CJuiSliderInput',array(
'name'=>'$setting->label',
'attribute'=>'[$i]value',
'value'=>$value,
'options'=>array(
'min'=>0,
'max'=>10,
),
'htmlOptions'=>array(
'style'=>'height:20px;',
),
));

yii : how to ajax update the cgridview

Q : How to ajax update the CgridVeiw?
Status : I've done with cookie. but there are a problem. it is when the page upload, the date are always show in from-date and to-date text box. and When reload the page, it doesn't clear out.
This is my view
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'page-form',
'enableAjaxValidation'=>true,
)); ?>
<div style="margin-top:30px;">
<b>From :</b>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name'=>'from_date', // name of post parameter
//'value'=>Yii::app()->request->cookies['from_date']->value, // value comes from cookie after submittion
'options'=>array(
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
),
'htmlOptions'=>array(
'style'=>'height:20px;'
),
));
?>
<span> </span>
<b>To :</b>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name'=>'to_date',
//'value'=>Yii::app()->request->cookies['to_date']->value,
'options'=>array(
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
),
'htmlOptions'=>array(
'style'=>'height:20px;'
),
));
?>
<span> </span>
<?php echo CHtml::submitButton('Go'); ?>
<?php
echo CHtml::ajaxSubmitButton('Search', CHtml::normalizeUrl(array('index')),
array(
'success'=>'js:'
.'function(){'
.'$.fn.yiiGridView.update("acc-payment-recei-grid", {url:"index"});'
.'}',
),
array('id'=>'go', 'name'=>'go'));
?>
<?php $this->endWidget(); ?>
<p style="float:right;">
New Payment Receive
</p>
</div>
<style>
.items table tr:last-child td:first-child {
-moz-border-radius-bottomleft:10px;
-webkit-border-bottom-left-radius:10px;
border-bottom-left-radius:10px
}
.items table tr:last-child td:last-child {
-moz-border-radius-bottomright:10px;
-webkit-border-bottom-right-radius:10px;
border-bottom-right-radius:10px
}
</style>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'acc-payment-recei-grid',
'dataProvider'=>$accpaymentrecei->search(),
//'filter'=>$accpaymentrecei,
'columns'=>array(
//'id',
array('name' => 'acc_category_id',
'value'=>'(isset($data->acccategories->name)) ? CHtml::encode($data->acccategories->name) :""',
),
array('name' => 'acc_recei_id',
'header'=> 'Account Received',
//'value'=>'(isset($data->method)) ? CHtml::encode($data->method) :""',
),
array(
'name' => 'date',
'value'=>'($data->date= 0) ? "" : date("d M yy",strtotime($data->date))',
),
array('name' => 'method',
'value'=>'(isset($data->method)) ? CHtml::encode($data->method) :""',
),
array('name' => 'description',
//'value'=>'(isset($data->method)) ? CHtml::encode($data->method) :""',
),
/*
'created_date',
'updated_date',
'file_name',
*/
array(
'class'=>'CButtonColumn',
),
),
));
This is controller
public function actionIndex()
{
$accpaymentrecei=new AccPaymentRecei('search');
$accpaymentrecei->unsetAttributes(); // clear any default values
if(isset($_GET['AccPaymentRecei']))
$accpaymentrecei->attributes=$_GET['AccPaymentRecei'];
$acccategory = AccCategory::model()->findAll();
$arr_method = array('Cash'=>'Cash', 'Cheque'=>'Cheque', 'Credit Card'=>'Credit Card', 'Bank Transfer'=>'Bank Transfer');
$this->from_to_date($accpaymentrecei);
//exit();
$this->render('index',array(
'accpaymentrecei'=>$accpaymentrecei,
'acccategory'=>$acccategory,
'arr_method'=>$arr_method,
));
}
protected function from_to_date($model)
{
unset(Yii::app()->request->cookies['from_date']); // first unset cookie for dates
unset(Yii::app()->request->cookies['to_date']);
//$model=new XyzModel('search'); // your model
$model->unsetAttributes(); // clear any default values
if(!empty($_POST))
{
Yii::app()->request->cookies['from_date'] = new CHttpCookie('from_date', $_POST['from_date']); // define cookie for from_date
Yii::app()->request->cookies['to_date'] = new CHttpCookie('to_date', $_POST['to_date']);
$model->from_date = $_POST['from_date'];
$model->to_date = $_POST['to_date'];
}else{
Yii::app()->request->cookies['from_date'] = new CHttpCookie('from_date', date("Y/m/d"));
Yii::app()->request->cookies['to_date'] = new CHttpCookie('to_date', date("Y/m/d"));
}
}
Code below - it's only example, main principles of solution. First principle: we don't need refresh page (by the submit), we need only refresh CGridView after request.
The solution has a few parts:
We need form with some parameters. In your case - fromDate and toDate. You have this one already. But we don't need any submit element. At all! We will use AJAX.
We need some function for initiate AJAX-request. For example, let's event for this request will be change of parameters *from_date* or *to_date*. This function also must update CGridView after request.
We need action in controller for processing AJAX-request.
Let's begin from part 2. It may be some like this:
Yii::app()->clientScript->registerScript('scriptId',
"var ajaxUpdateTimeout;
var ajaxRequest;
$('**selector_for_parameters_fields**').change(function(){
ajaxRequest = $(this).serialize();
clearTimeout(ajaxUpdateTimeout);
ajaxUpdateTimeout = setTimeout(function () {
$.fn.yiiGridView.update(
// this is the id of the CGridView
'acc-payment-recei-grid',
{data: ajaxRequest}
)
},
// this is the delay
300);
});"
);
Place this code into view. Notes:
$(this).serialize() - key moment. Look for more info
selector_for_parameters_fields - give them ( fromDate and toDate ) some unique
selector
We use some delay (300 ms) for decreasing loading from unnecessary AJAX-requests
Now - about action in Controller
public function actionIndex($fromDate='', $toDate='')
{
$criteria = new CDbCriteria();
if(!empty($fromDate))
$criteria->addSearchCondition('your search condition about fromDate');
if(!empty($toDate))
$criteria->addSearchCondition('your search condition about toDate');
$dataProvider=new CActiveDataProvider('YourModel',
array('criteria' => $criteria,));
$this->render('YourView',array('dataProvider'=>$dataProvider,));
}
It must works, but requires some tuning for concrete conditions.

Categories