My DB column name subscribed tinyint(1),
I want if checkBox is checked then value send 1 if not checked then value send 0.
This is my code-
<?php echo $form->checkBox($model,'subscribed'); ?>
Please help.
Try this:
<?php echo $form->checkBox($model,'subscribed',array('value' => '1', 'uncheckValue'=>'0')); ?>
Here, value for checked checkbox is 1 and unchecked checkbox is 0
In your view file:
<?php echo $form->checkBox($model,'attribute'); ?> //in your case 'attribute' is 'subscribed'
in your controller
<?php
//if checked, return 1;
//if unchecked, return 0;
.........
$checkbox_value=$model->attribute; //in your case 'attribute' is 'subscribed'
echo $checkbox_value;
.......
?>
Related
I have a Module table with these column
ID_MODULE, NAMA_MODULE, ESTIMASI_HARGA, FLAG
I want to display NAMA_MODULE and ESTIMASI_HARGA on RadioButton. But I can only manage to display NAMA_MODULE with this code
<?php $items=ArrayHelper::map(Module::find()->where(['FLAG'=>1])->all(),'ID_MODULE',['NAMA_MODULE']) ?>
<?php echo $form->field($pMod, 'ID_MODULE', ['options' => ['style' => 'display:none', 'id' => 'pModule']])->inline()->checkboxList($items,$items)->label(false); ?>
Is it possible to give radio button a label with 2 column from DB with Yii?
$items = ArrayHelper::map(Module::find()->where(['FLAG'=>1])->all(),
'ID_MODULE',
function($model) {
return $model->NAMA_MODULE.'-'.$model->ESTIMASI_HARGA;
}
);
<div class="row">
<?php echo $form->labelEx($model,'ClassId'); ?>
<?php
$School=Yii::app()->session['Schoolid'];
echo CHtml::activeDropDownList($model,'ClassId',CHtml::listData(Classdetails::model()->findAll(array("condition"=>"classid >0 and School_Id='$School' and Status=1","order"=>"classid")),'classid','classname'),
array(
'empty'=>'--Select a Class--',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('Studentmarks/Dynamiccities'), //url to call.
'data'=>array('Classid'=>'js: $(this).val()'),
'update'=>'#'.CHtml::activeId($model,'pid'),
)));
echo $form->error($model,'ClassId');
echo $form->labelEx($model,'pid');
echo CHtml::activeDropDownList($model,'pid', array(),array('prompt'=>'-- Select a Student --'));
echo $form->error($model,'pid');
?>
</div>
As per the code specified, you are explicitly setting the second dropdown empty which is why it's not working in update view.
In update mode, you need to simply populate the dropdown with possible values with the saved value selected.
You can probably do something like this.
if($model->isNewRecord)
echo CHtml::activeDropDownList($model,'pid', array(),array('prompt'=>'-- Select a Student --')); // create
else
echo CHtml::activeDropDownList($model,'pid', $model->populateStudentsDate($model->ClassId)); // update
In populateStudentsDate($model->ClassId) method you need to filter and show the list of students as per class selection.
i need custom id eg, 'id'=>table_name in below code. When i remove id from htmloptions array, ajax validation works fine, but it does no when i use id.
<div class="form">
<?php
$form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'data-fixer-form',
'enableAjaxValidation'=>true,
'htmlOptions'=>array(
'onsubmit'=>"return false;",/* Disable normal form submit */
'onkeypress'=>" if(event.keyCode == 13){ ajaxReplaceValue(event); } " /* Do ajax call when user presses enter key */
),
));
?>
<?php echo $form->dropDownListControlGroup($model,'table_name',$tableNames,array('id'=>'table_name','onchange'=>'getColumn(event)','class'=>'span4')); ?>
<?php echo $form->dropDownListControlGroup($model,'column_name',$columnNames,array('id'=>'column_name','class'=>'span4')); ?>
<?php echo $form->textFieldControlGroup($model,'search_for',array('id'=>'search_for','placeholder'=>tTools('dataFixer','search_for'),'class'=>'span4')); ?>
<?php echo $form->textFieldControlGroup($model,'replace_with',array('id'=>'replace_with','placeholder'=>tTools('dataFixer','replace_with'),'class'=>'span4')); ?>
<div class="form-actions">
<?php
echo CHtml::submitButton(t('common','submit'), array (
'id'=>'dataFixerSubmitBtn',
'class'=>'btn btn-primary',
'identity'=>'',
'dataFetched'=>'0',
'onclick'=>'ajaxReplaceValue(event);'
));
?>
</div>
<?php $this->endWidget(); ?>
finally i got the solution. We need to prepend Model Name befor id as below
'id'=>'DataFixer_table_name'
where DataFixer is model name
I have a checkboxlist using CHtml::checkBoxList and I need a checkbox with 'Select all' to select all checkboxes when it is checked. But I don't know how to do it.
You could do
<?php echo CHtml::checkBox('select_all',false, array('onclick' => "js:if($(this).is(':checked')) {$('.example').attr('checked','checked');}else{$('.example').removeAttr('checked');}")) ?>
<?php echo CHtml::activeCheckBoxList($model, 'items',array('example1','example2','example3'), array('class'=>'example')) ?>
My Goal:
I am trying to filter a model, based on a GET parameter, and populate a form based on this.
What I require is that when users select the update icon on the Gridview
I will
grab the 'telephone number' they want to edit,
populate a form with the telephonenumber's data
allow the user to edit this data for the telephonenumber and submit
I then run my own custom sql update query based on the new data.
My Problem
My gridview can successfully capture the telephonenumber for a selected row.
It can successfully send this to the Controller Update method (see below)
I cannot however filter a model based on this telephonenumber and then populate a form with this model.
My Error
get_class() expects parameter 1 to be object, array given
/framework/web/helpers/CHtml.php(2220)
/framework/web/helpers/CHtml.php(2220): get_class(array())
/framework/web/helpers/CHtml.php(1236): CHtml::resolveName(array(), "TelephoneNumbers_TelephoneNumber")
/framework/web/widgets/CActiveForm.php(562): CHtml::activeLabelEx(array(), "TelephoneNumbers_TelephoneNumber", array())
/views/dateAudiid/editupdateform.php(18): CActiveForm->labelEx(array(), "TelephoneNumbers_TelephoneNumber")
/framework/web/CBaseController.php(126): require("/var/www/OMReport/protected/views/dateAudiid/editupdateform.php")
Here's my Gridview.
$this->widget('bootstrap.widgets.TbGridView', array(
'id'=>'dateaudiidcondensed-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
'template'=>'{update}',
'buttons'=>array
(
'update' => array
(
'url'=>'Yii::app()->createUrl("dateAudiid/Update/",array("TelephoneNumbers_TelephoneNumber"=>$data->TelephoneNumbers_TelephoneNumber))',
),
),
),
'TelephoneNumbers_TelephoneNumber',
'FormId_Formid',
'Date',
'AudibeneID_Audibene_ID'
),
));
Here's my Controller
public function actionUpdate($TelephoneNumbers_TelephoneNumber)
{
$criteria=new CDbCriteria;
$criteria->compare('TelephoneNumbers_TelephoneNumber',$_GET['TelephoneNumbers_TelephoneNumber'],true);
$criteria->limit = 1;
$criteria->offset = 1;
$model = DateAudiidCondensedByAudibeneId::model()->findAll($criteria);
$this->render('editupdate',array('model'=>$model));
}
Here's my View
<?php
/* #var $this DateAudiidController */
/* #var $model DateAudiidCondensedByAudibeneId */
?>
<h1>Update Assignments </h1>
<?php echo $this->renderPartial('editupdateform', array('model'=>$model)); ?>
Here's my Form
<div class="form">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'date-audiid-condensed-by-audibene-id-customupdate-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php
echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'TelephoneNumbers_TelephoneNumber'); ?>
<?php echo $form->textField($model,'TelephoneNumbers_TelephoneNumber'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'FormId_Formid'); ?>
<?php echo $form->textField($model,'FormId_Formid'); ?>
<?php echo $form->error($model,'FormId_Formid'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Date'); ?>
<?php echo $form->textField($model,'Date'); ?>
<?php echo $form->error($model,'Date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'AudibeneID_Audibene_ID'); ?>
<?php echo $form->textField($model,'AudibeneID_Audibene_ID'); ?>
<?php echo $form->error($model,'AudibeneID_Audibene_ID'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
My Thoughts / What I've tried so far
I am returning the model as an array...but my form wants it as an object.
I need to change something in the way I filter my model in the Controller Update action, however I cannot see how to do this.
All my other methods use similar model filtering code.
here is what you can do to fix this :
your form is trying to use models labeling but you have given an array, ->findAll() will return an array of models, here you need a single object of model
the error your having is because $model in filter of your grid needs to be an object of model which here will try to validate using this model, so won't work when you are giving it an array of your models,
if you want to filter the results based on something, you need to do this where the dataprovider of your grid is being populated, so in this case, is in $model->search()
get the parameter and append it to that criteria
so your grid could look like this:
$this->widget('bootstrap.widgets.TbGridView', array(
'id'=>'dateaudiidcondensed-grid',
'dataProvider' => $model->search(), //create a new model with search scenario
'filter' => $model, // here use that model to validate fields
'columns'=>array(
.
.
.
),
));
and in your models search method:
public function search() {
$criteria = new CDbCriteria;
// grab the sent data and use it here
$tel = Yii::app()->request->getParam('TelephoneNumbers_TelephoneNumber' , null);
if(!empty($tel))
$criteria->compare('TelephoneNumbers_TelephoneNumber' , $tel , true);
.
.
.
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
Here's what eventually worked for me
The usage of 'findByAttributes' to filter and return a model to the Form
public function actionUpdate($TelephoneNumbers_TelephoneNumber)
{
$model = DateAudiidCondensedByAudibeneId::model()->findByAttributes(array("TelephoneNumbers_TelephoneNumber" => $_GET['TelephoneNumbers_TelephoneNumber']));
$this->render('editupdate',array('model'=>$model));
}