How can you call a CJuiDialog in beforesave - yii

I will appreciate if somebody could help me to find how to solve out one problem, I have a checkbox in my create form. If i pushed the create button I want to have a popup window if the checkbox is checked and do nothing if the checkbox is unchecked.
my codes in _form
<?php echo $form->checkBoxRow($model, 'default'); ?>
<div class="form-actions">
<?php $this->widget('bootstrap.widgets.TbButton', array(
'buttonType'=>'submit',
'type'=>'primary',
'label'=>$model->isNewRecord ? 'Create' : 'Save',
)); ?>
</div>
in my create controller
public function actionCreate()
{
$model=new EmpSched;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['EmpSched']))
{
$model->attributes=$_POST['EmpSched'];
if($model->save())
$this->redirect(array('view','id'=>$model->id_empsched));
}
$this->render('create',array(
'model'=>$model,
'emp'=> new CActiveDataProvider('schedule'),
));
}
in my create.php
<?php
$this->breadcrumbs=array(
'Emp Scheds'=>array('index'),
'Create',
);
$this->menu=array(
array('label'=>'List EmpSched','url'=>array('index')),
array('label'=>'Manage EmpSched','url'=>array('admin')),
);
?>
<h1>Create EmpSched</h1>
<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>
<?php $this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'schedule-grid',
'dataProvider'=>$emp,
'columns'=>array(
'id_sched',
'sched_name',
array(
'name'=>'mon',
'value'=>'$data->fkidday->monday->name'
),
array(
'name'=>'tues',
'value'=>'$data->fkidday->tuesday->name'
),
array(
'name'=>'wed',
'value'=>'$data->fkidday->wednesday->name'
),
array(
'name'=>'thurs',
'value'=>'$data->fkidday->thursday->name'
),
array(
'name'=>'fri',
'value'=>'$data->fkidday->friday->name'
),
/*'sat',
'sun',
*/
),
));
?>
<script>
$(function() {
// when row is clicked
$('#schedule-grid tbody:first').on('click', 'tr', function() {
// get the ID
var id = $(this).find('td:first').text();
// select the same option in dropdown list with same value
$('#EmpSched_fk_id_sched')
.find("option[value="+ id +"]")
.prop("selected", "selected");
});
});
</script>

Add custom javascript (supposing the create button will submit the form)
$('#form-id').submit(function() {
if $(this).find('#check-box-id').is(':checked') {
// open popup here
}
return false;
});

Related

yii dependent dropdown is not working

This is my view page
<?php echo $form->dropDownListRow($order,'area_id',Area::model()->getActiveAreaList(),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('currentController/getdeliveryforarea'),
'update'=>'#pickup_time_slot', //selector to update
'data' => array('area_id' => 'js:this.value',),
)));
?>
<?php echo $form->dropDownListRow($order,'pickup_time_slot',array(),array('prompt'=>'Select time')); ?>
and in my controll the getdeliveryforarea is like
public function actionGetdeliveryforarea()
{
$data=Areatimeslot::model()->findAll('area_id=:area_id',
array(':area_id'=>(int) $_POST['id']));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
here my dependent dropdown is not working ,from getActiveAreaList i will get the area list in first dropdown and once i choose an area it must show corrosponding time list in second dropdown,i hope someone will help me out,thanks in advance
Use Juqery migrate plugin or try something like,
jQuery.browser = {};
(function () {
jQuery.browser.msie = false;
jQuery.browser.version = 0;
if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
jQuery.browser.msie = true;
jQuery.browser.version = RegExp.$1;
}
})();

Unable to post data to survey controller in yii

index.php
<?php
Yii::app()->clientScript->registerScript('items_update',
"$('#dropit').change(function(){
$.ajax({
url: '".Yii::app()->createUrl('survey/questions')."',
data:
{
_designation : test,
},
type: 'POST',
success:function()
{
console.log('success');
},
error:function()
{
console.log('failure');
}
});
return false;
});
");
?>
<div id="surveyupdate">
<?php
echo CHtml::dropDownList('dropit', '',
array(
'Reception'=>'Service Reception',
'SA'=>'ServiceAdvisor','Billing'=>'Billing Staff',
'Parts'=>'Parts Counter','Cashier'=>'Cashier','Cwd'=>'Customer Welfare Desk'),
array('prompt'=>'- select -'));
?>
</div>
<?php
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'id'=>'ajaxListView',
));
?>
controller 'survey.php'
public function actionQuestions()
{
//debugging purposes
echo "<pre>";
print_r($_POST);
exit;
}
To Do's
What I'm trying to do is to update the CListView by getting the selected data from dropdownlist via POST that to be evaluated by the controller, get the data from database and update the CListview.
Please help me. I'm just newly learning Yii. Thanks!
You need to add Id for dropdownList, like below
echo CHtml::dropDownList('dropit', '',
array(
'Reception'=>'Service Reception',
'SA'=>'ServiceAdvisor','Billing'=>'Billing Staff',
'Parts'=>'Parts
Counter','Cashier'=>'Cashier','Cwd'=>'Customer Welfare
Desk'),
array('prompt'=>'- select -', 'id'=>'dropit')); // added I'd here

Yii update cgridview via ajax button not working.

I have in my controller:
public function actionFilterClients {
if (Yii::app()->request->isAjaxRequest) {
if (isset($_POST['category_id'])) {
$criteria = new CDbCriteria;
$criteria->condition = "user_id=:user_id";
$criteria->params = array(':user_id' => Yii::app()->user->id);
$criteria->compare('category_id',$_POST['category_id'],true);
$dataProvider = new CActiveDataProvider('Client', array(
'criteria'=>$criteria,
));
$this->renderPartial('transfer_step_3' , array('dataProvider'=>$dataProvider)) ;
}
}
}
In my view among other things I have:
<?php $filter=$this->beginWidget('CActiveForm', array(
'id'=>'client-filter-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('class'=>'form-horizontal'),
)); ?>
<label for="category_id">View clients in category:</label>
<?php echo CHtml::dropDownList('category_id','',Client::clientCategories(), array('options' => array('2'=>array('selected'=>true)))); ?>
<?php
echo CHtml::ajaxButton(
'Filter Clients',
'filterclients',
array(
'type'=>'POST',
'update' => 'client-grid' ,
'success' =>"function(data) {
\$.fn.yiiGridView.update('client-grid');}",
)
);
?>
<?php $this->endWidget(); ?>
and
<?php $this->widget('bootstrap.widgets.TbGridView',array(
'type'=>'bordered striped condensed',
'id'=>'client-grid',
'ajaxUpdate' => true ,
'rowCssClassExpression'=>'($data->duplicate==2)?"yellow":($data->duplicate==1?"blue":"")',
'dataProvider'=>(isset($dataProvider)?$dataProvider:$clients->moveclients()),
'template'=>"{items}\n{pager}",
'columns'=>array(
array(
'class'=>'CCheckBoxColumn',
'selectableRows'=>2,
'id'=>'clients',
),
'name',
'surname',
'telephone',
'email',
array(
'header'=>'Category',
'name' => 'category_title',
'type' => 'raw',
'value' => '$data->category->title',
),
),
)); ?>
Because this is a multi-step form, the cgridview dataprovider defaults to listing all clients ($clients->moveclients() lists all clients).
The ajax button posts the category_id to the client/filterclients url correctly.
I can see with firebug that actionFilterClients returns the rendered html correctly (with the correct clients) but the gridview is not updated...
Any ideas on why not?
In the end I added another view that had only a gridview in it and modified my code thus:
Controller:
$this->renderPartial('_ajax_transfer_step_3' , array('dataProvider'=>$dataProvider)) ;
Original view:
<?php $filter=$this->beginWidget('CActiveForm', array(
'id'=>'customer-filter-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('class'=>'form-horizontal'),
)); ?>
<label for="category_id">View customers in category:</label>
<?php
echo CHtml::dropDownList('category_id', '', Customer::customerCategories(),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('filtercustomers'),
'data'=>'js:jQuery(this).serialize()',
'success'=>'function(response) {
jQuery("#customer-grid").html(response)
}',
)
)
);
?>
<?php $this->endWidget(); ?>
Now it just replaces a portion of the page.
I still haven't figured out why my original code didn't update the gridview though.

Custom validation rule is not working for CFormModel

My front end is Php Yii. I am trying to create a custom validation rule which checks to see if the username already exists in the database.
I don't have direct access to the database. I have to use the RestClient to communicate with the Database. My issue is that custom validation rule is not working with my CFormModel.
Here is my code:
public function rules()
{
return array(
array('name', 'length', 'max' => 255),
array('nickname','match','pattern'=> '/^([a-zA-Z0-9_-])+$/' )
array('nickname','alreadyexists'),
);
}
public function alreadyexists($attribute, $params)
{
$result = ProviderUtil::CheckProviderByNickName($this->nickname);
if($result==-1)
{
$this->addError($attribute,
'This Provider handler already exists. Please try with a different one.');
}
This doesn't seem to work at all, I also tried this:
public function alreadyexists($attribute, $params)
{
$this->addError($attribute,
'This Provider handler already exists. Please try with a different one.');
}
Even then, it doesn't seem to work. What am I doing wrong here?
The problem with your code is that it doesn't return true or false.
Here is one of my rules to help you:
<?php
....
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('title, link', 'required'),
array('title, link', 'length', 'max' => 45),
array('description', 'length', 'max' => 200),
array('sections','atleast_three'),
);
}
public function atleast_three()
{
if(count($this->sections) < 3)
{
$this->addError('sections','chose 3 at least.');
return false;
}
return true;
}
...
?>
I met the same issue and finally got it solved. Hopefully, the solution could be useful for resolving your problem.
The reasons why the customised validation function is not called are:
this is a server side rather than client side validation
when you click the "submit" button, the controller function takes over the process first
the customised function won't be involved if you didn't call "$model->validate()"
Therefore, the solution is actually simple:
Add "$model->validate()" in the controller function. Here is my code:
"valid.php":
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'alloc-form',
'enableClientValidation'=>true,
'clientOptions'=>array('validateOnSubmit'=>true,),
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'valid_field'); ?>
<?php echo $form->textField($model,'valid_field'); ?>
<?php echo $form->error($model,'valid_field'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
"ValidForm.php":
class ValidForm extends CFormModel
{
public $valid_field;
public function rules()
{
return array(
array('valid_field', 'customValidation'),
);
}
public function customValidation($attribute,$params)
{
$this->addError($attribute,'bla');
}
}
"SiteController.php"
public function actionValid()
{
$model = new ValidForm;
if(isset($_POST['AllocationForm']))
{
// "customValidation" function won't be called unless this part is added
if($model->validate())
{
// do something
}
// do something
}
}

yii update clistview by dropdownlist

I'm a very newbie in Yii framework. I would like to create a page. Which is when the dropdown list change, the listview/gridview will be change by dropdown value.
this is my view
<div class="row">
<?php
$records = Company::model()->findAll();
$company_list = CHtml::listData($records, 'id', 'name');
echo CHtml::dropDownList('company_id','', $company_list,
array(
'onchange'=>"$.fn.yiiListView.update('ajaxListView', {url: '".Yii::app()->createUrl('department/dynamicsectionlist')."?company_id='+$('#company_id option:selected').val()})",
'prompt'=>'Please select a company',
)); ?>
</div>
<?php
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view_section',
'id'=>'ajaxListView',
));
?>
This is model
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('p_id',$this->p_id);
$criteria->compare('created',$this->created,true);
$criteria->compare('updated',$this->updated,true);
$criteria->compare('company_id',$this->company_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
This is Controller
public function actionDynamicsectionlist()
{
$company_id = $_POST['company_id'];
$criteria=new CDbCriteria();
$criteria->condition .= 't.id IN (SELECT t2.id, t2.name FROM department t2 WHERE t2.company_id = :company_id)';
$criteria->params[':company_id'] = $company_id;
$dataProvider = new CActiveDataProvider( 'Department', array( 'criteria' => $criteria, ) );
$this->render( 'sectionlist', array( 'dataProvider' => $dataProvider ) );
}
But it is not working. Please help me.
Regads
Tharsoe
I solved it.
This is controller
// Initial view (department/depatmentlist)
public function actionDepartmentlist()
{
$model=new Department('search');
$model->unsetAttributes(); // clear any default values
$model->p_id = 0;
// $dataProvider->getData() will return a list of Post objects
// $dataProvider=new CActiveDataProvider('Department');
$this->render('list_department',array(
'model'=>$model,
));
}
// when the user selected the company from dropdown list
public function actionDynamicsectionlist()
{
$model=new Department('dsearch');
$model->unsetAttributes(); // clear any default values
$model->p_id = 0;
if(isset($_GET['company_id']))
$model->company_id = $_GET['company_id'];
$this->render('sectionlist',array(
'model'=>$model,
));
}
This is model (nothing change)
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('p_id',$this->p_id);
$criteria->compare('created',$this->created,true);
$criteria->compare('updated',$this->updated,true);
$criteria->compare('company_id',$this->company_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
This is view (list_department.php)
<h1>Departments List</h1>
<div class="row">
Company<br />
<?php
$records = Company::model()->findAll();
$company_list = CHtml::listData($records, 'id', 'name');
echo CHtml::dropDownList('company_id','', $company_list,
array('prompt'=>'Please select a company',)); ?>
</div>
<?php
/*
for ListView
$this->widget('zii.widgets.CListView', array(
//'dataProvider'=>$dataProvider,
'dataProvider'=>$model->search(),
'itemView'=>'_view_section',
'id'=>'ajaxListView',
));
*/
?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'department-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
//'p_id',
'created',
'updated',
//'company_id',
),
));
?>
<?php
Yii::app()->clientScript->registerScript('search',
"$('#company_id').change(function(){
var companyId = $('#company_id option:selected').val();
$.fn.yiiGridView.update(
'department-grid',
{ type: 'GET',
url: 'http://localhost/mmaig_ceo/ceo-control-system/index.php?r=department/dynamicdepartmentlist&ajax=department-grid&company_id=' + companyId
}
);
});
")
?>