Uploading file with jshelper in CakePHP 2.2.1 - ajax

I am trying to made upload files with jshelper.
In my view:
<?php echo $this->Form->create('Form', array(
'inputDefaults' => array(
'format' => array('before', 'error', 'label', 'between', 'input', 'after'),),
'enctype' => 'multipart/form-data'
));
echo $this->Form->input('Form.file', array('type' => 'file', 'id'=>'file'));
$this->Js->get('#file')->event(
'update',
$this->Js->request(
array('controller' => 'ControllerForm','action' => 'actionForm'),
array(
'async' => true,
'method' => 'post',
'dataExpression'=>true,
'data'=> $this->Js->serializeForm(array(
'isForm' => false,
'inline' => true,
))
)
));
echo $this->Js->writeBuffer();
echo $this->Form->end();
?>
In my controller:
public function actionForm(){
debugger::dump($this->data);
}
But debugger::dump output empty array.

Man as I know you cannot send file using ajax (include xhrRequestObject2 in Chrome). Firstly in server side you check post data instanted debug($_FILES), but that as I wrote is empty. Use old good iframe to send data to server.

Related

Fetch and display dropdown selected value in codeigniter

I wanted to fetch the selected value from the database and display it in codeigniter form_dropdown() function but it displays wrong.
Controller:
$type = array(
'options' => array(
'section' => 'Section',
'transaction' => 'Transaction',
'document' => 'Document'
),
'attributes' => array(
'class' => 'form-control'
)
);
View:
<?php echo form_dropdown('type', $type['options'],'', $type['attributes']) ?>
The Screenshot
Try the below code:
Controller:
$this->data['type'] = array(
'name' => 'type_value',
'attributes' => 'class="form-control"',
'value' => (isset($database_type_value) && trim($database_type_value)) ? $database_type_value: $this->input->post('type_value',TRUE), //$database_type_value - value from database
'options_list' => array(
'section' => 'Section',
'transaction' => 'Transaction',
'document' => 'Document'
),
);
View:
<?php echo form_dropdown($type['name'],$type['options_list'],$type['value'],$type['attributes']);?>

CakePHP - How to dynamically change the fields in my form after selecting from drop-down list

I am having a trouble on getting the value of the selected drop-down list(package_id) to populate the next fields particularly the Departure Time and Price which is in the Package Model.
Here is my code in View/Reservations/package.ctp:
<?php echo $this->Form->create('Reservation'); ?>
<table cellspacing="10">
<?php
echo $this->Html->tableCells(array(
array(
'Date: ',
$this->Form->input('date', array('label' => false, 'type' => 'text', 'class' => 'datepicker'))
),
array(
'Package Name:',
$this->Form->input('package_id', array('label' => false, 'options' => $name, 'id' => 'PackageID', 'empty' => '-- Select Package --'))
),
array(
'Departure Time:',
$this->Form->input('departure_time', array('label' => false, 'type' => 'label', 'id' => 'test'))
),
array(
'Price:',
$this->Form->input('price', array('label' => false, 'id' => 'test'))
),
array(
'Number of Person:',
$this->Form->input('number_of_people', array('label' => false))
),
array(
'Total Price:',
$this->Form->input('price', array('label' => false))
),
array(
'',
$this->Form->Submit('Book Now', array('class' => 'button'))
),
));
?>
</table>
and Here is my code in public function package():
$options = $this->Packages->find('list'); //or whatever conditions you want
$this->set('name', $options);
I was trying to use JS helper but I can't get it right here is my code:
$this->Js->get('#PackageID');
$this->Js->event('change',
$this->Js->request(array(
'controller'=>'Reservation',
'action'=>'getPackage'
), array(
'update'=> '#test',
'async' => true,
'method' => 'post',
'dataExpression' => true,
'data'=> $this->Js->serializeForm(array(
'isForm' => true,
'inline' => true))
)
)
);
Feel free to ask questions for clarification. Thank You in advance :)
Try this :
in the view .ctp , add this js function :
<script language="JavaScript">
jQuery(document).ready(function() {
$("#PackageID").chosen().bind('change', function() {
$.post('/project_name/controller_name/listDeparetementByPackage/'+$(this).val(), function(data) {
$("#test").empty().append(data);
$("#test").trigger("liszt:updated");
}, 'html');
});
});
</script>
and in your controller , here is the function to get the departements by pacakge id :
function listDeparetementByPackage($package = null ) {
$this->layout = 'ajax';
$this->beforeRender();
$this->autoRender = false;
$data = $this->Package->Departement->find('list', array('fields' => array('Departement.id', 'Departement.libelle'),
'conditions' => array('Departement.package_id' => $package),
'recursive' => 0 ));
if(count($data)>0){
foreach($data as $key => $val) {
echo "<option value=$key>$val</option>";
}
}else{
echo "<option></option>"; // if the result is empty , show a select empty
}
}
Hope it helps .

cakephp pass select value ajax

I have two select buttons and want to filter records, when user select any option I have to fire a event and pass the values of buttons to the controller's action. my code is as under
view
<?php echo $this->Form->create('Employee'); ?>
<fieldset>
<div class="pure-control-group">
<?php echo $this->Form->input('designation', array("label" => "Designation",'type' => 'select', 'id'=>'DesignationType','options' => $settings,'empty' => 'select'));?>
<?php echo $this->Form->input('district', array("label" => "District",'type' => 'select', 'id'=>'districtType','options' => $district,'empty' => 'select'));?>
</div>
</fieldset>
<?php echo $this->Form->end();
$this->Js->get('#DesignationType')->event('change',
$this->Js->request(array(
'controller'=>'employees',
'action'=>'getByCategory'
), array(
'update'=>'#success',
'async' => true,
'method' => 'post',
'dataExpression'=>true,
'data'=> $this->Js->serializeForm(array(
'isForm' => true,
'inline' => true
))
))
);
?>
<div id="success"></div>
controller
public function getByCategory(){
$design_id =$this->request->data['Employee']['designation'];
$district_id =$this->request->data['Employee']['district'];
$this->layout = 'ajax';
}
when i click designation then i m able to get the value of designation but not able to get the value of district .And following error occur
Undefined index: district [APP\Controller\EmployeesController.php, line 18]
so, how can i get the value of district;
Try to send the form id in data variable. that is
Try this :
$data = $this->Js->get('#YourFormId')->serializeForm(array('isForm' => true, 'inline' => true));
$this->Js->get('#DesignationType')->event('change',
$this->Js->request(array(
'controller'=>'employees',
'action'=>'getByCategory'
), array(
'update'=>'#success',
'async' => true,
'method' => 'post',
'dataExpression'=>true,
'data'=> $data
))
);

Yii CGridview Filter Datepicker

I have already read the forum about make custom cgridview.. I want to make filter use datetimepicker.
Datetimepicker:
I can include already the datetimepicker, but when I choose the date, it became error. :(..
My date datatype on database is 'timestamp'
This is the code
<?php
$this->breadcrumbs=array(
'Events'=>array('index'),
'Manage',
);
$this->menu=array(
array('label'=>'List Event','url'=>array('index')),
array('label'=>'Create Event','url'=>array('create')),
);
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$.fn.yiiGridView.update('event-grid', {
data: $(this).serialize()
});
return false;
});
");
?>
<h1>Manage Events</h1>
<p>
You may optionally enter a comparison operator (<b><</b>, <b><=</b>, <b>></b>, <b>>=</b>, <b><></b>
or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
</p>
<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button btn')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
'model'=>$model,
)); ?>
</div><!-- search-form -->
<?php $this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'event-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'afterAjaxUpdate'=>"function(){jQuery('#event_date_search').datepicker({'dateFormat': 'yy-mm-dd'})}",
'columns'=>array(
array(
'type' => 'raw',
'name'=> 'event_image',
'value' => 'CHtml::image("'.Yii::app()->request->baseUrl.'/uploads/event/$data->event_image", "event_image" ,array("width"=>100))',
'filter'=> false,
),
'event_name',
array(
'name' => 'event_date',
'type' => 'raw',
'filter'=>$this->widget('zii.widgets.jui.CJuiDatepicker', array(
'model'=>$model,
'attribute'=>'event_date',
'htmlOptions' => array(
'id' => 'event_date_search'
),
'options' => array(
'dateFormat' => 'yy-mm-dd'
)
), true)
),
array(
'name'=>'published',
'filter'=>CHtml::dropDownList('Event[published]', '',
array(
''=>'',
'1'=>'Published',
'0'=>'Not Published',
)
),
'value' =>'($data->published==1)?"Published":"Not Published"',
),
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
),
),
));
?>
Thank you very much...
You can change search() function of model class. It can be like this:
public function search() {
....
$criteria->addCondition('DATE_FORMAT(time_field, \'%Y-%m-%d\') = ' . $this->time_field);
....
}
but it can be need to translate input value of $this->time_field.
i had the exact same problem yesterday and for some unknown reason by moving the
'dateFormat' => 'yy-mm-dd'
from option to htmloptions fixed my problem. to be honest i have it in both htmloptions and options and don't have any problems so far
'htmlOptions' => array(
'id' => 'event_date_search',
'dateFormat' => 'yy-mm-dd'
),
'options' => array(
'dateFormat' => 'yy-mm-dd'
),
hope this helps
Could you replace your code
'options' => array(
'dateFormat' => 'yy-mm-dd'
)
with
'defaultOptions' => array(
'showOn' => 'focus',
'dateFormat' => 'yy-mm-dd',
'showOtherMonths' => true,
'selectOtherMonths' => true,
'changeMonth' => true,
'changeYear' => true,
'showButtonPanel' => true,
)
Hope this help
For detail check this link http://www.yiiframework.com/wiki/318/using-cjuidatepicker-for-cgridview-filter/

CJuiDatePicker validation messages isn't working

Hi.
I have a problem with form-validation in Yii framework.
Here is my VIEW code:
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'search-form',
'enableAjaxValidation' => true,
'enableClientValidation' => true,
'focus' => array($model, 'ccc'),
'clientOptions' => array(
'validateOnSubmit' => true,
),
));
?>
<?php
echo $form->errorSummary($model);
?>
<div class="row">
<?php echo $form->labelEx($model, 'input'); ?>
<?php echo $form->textField($model, 'input', array('class' => 'input-medium', 'maxlength' => 11,)); ?>
<?php echo $form->error($model, 'input'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'date'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'attribute' => 'date',
'name' => 'date',
'model' => $model,
'language' => 'ru',
'options' => array(
'dateFormat' => 'dd/mm/y',
'showAnim' => 'slideDown',
'changeMonth' => true,
'changeYear' => true,
'showOn' => 'button',
'constrainInput' => 'true',
),
'htmlOptions' => array(
'style' => 'height:15px; width:6em'
),
));
?>
<?php echo $form->error($model, 'date'); ?>
</div>
<?php $this->endWidget(); ?>
Nothing special. But validation messages working only for textField (Ajax requests are sending only with onChange textField).
How to enable CJuiDatePicker validation messages?
You just have to give the right id to your CJuidatepicker object, use CHtml::getIdByName to create the id value, try to the name of the html element there, it must be something like
'id' => CHtml::getIdByName(get_class($model) . '[' . $attribute . ']')
it would become something like this:
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'id' => CHtml::getIdByName(get_class($model) . '[date]'),
'attribute' => 'date',
'name' => 'date',
'model' => $model,
'language' => 'ru',
'options' => array(
'dateFormat' => 'dd/mm/y',
'showAnim' => 'slideDown',
'changeMonth' => true,
'changeYear' => true,
'showOn' => 'button',
'constrainInput' => 'true',
),
'htmlOptions' => array(
'style' => 'height:15px; width:6em'
),
));
wonde's answer "you should include a value" didn't work for me...
This is what worked:
Yii CActiveForm date validation
views/site/login.php originally had:
$form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
Things worked when this was changed to:
$form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableAjaxValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => true,
),
See:
http://sky-walker.net/temp/test/yii/testdate/index.php?r=site/login
This is what i did and it works, i think you should include a value
<?php echo $form->labelEx($model,'reportDate'); ?>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',
array( 'model'=>$model,
'attribute'=>'reportDate',
**'value'=>$model->reportDate,**
'options'=>array(
'showButtonPanel'=>true,
'changeYear'=>true,
'changeMonth'=>true,
'autoSize'=>true,
'dateFormat'=>'yy-mm-dd',
'defaultDate'=>$model->reportDate,
),
));
?>
<?php echo $form->error($model,'reportDate'); ?>
this problem can be shoved simply by adding $form->error($model,'end_date') after CJuiDatePicker.
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'id' => CHtml::getIdByName(get_class($model) . '[date]'),
'attribute' => 'date',
'name' => 'date',
'model' => $model,
'language' => 'ru',
'options' => array(
'dateFormat' => 'dd/mm/y',
'showAnim' => 'slideDown',
'changeMonth' => true,
'changeYear' => true,
'showOn' => 'button',
'constrainInput' => 'true',
),
'htmlOptions' => array(
'style' => 'height:15px; width:6em'
),
));
echo $form->error($model,'dateTo');// added to enaple clint validation

Resources