Set default radio button in Cakephp - php

The below code creates some radio buttons and html to go with them, it works well however I now want to set the first radio button as the default selected one and im not sure what needs to be added.
<?php
for ($i =0; $i < count($packages); $i++){
echo "<div class='package-outter'>";
echo "Name: ".$packages[$i]['Package']['name']."<br>";
echo "Number of Campaigns (per month): ".$packages[$i]['Package']['quantity']."<br>";
echo "Price: ".$packages[$i]['Package']['price']."<br>";
if ($i == 0){
echo $this->Form->input('package', array(
'type' => 'radio',
'options' => array($packages[$i]['Package']['id'] => $packages[$i]['Package']['name'],),
'class' => 'testClass',
));
}else{
echo $this->Form->input('package', array(
'type' => 'radio',
'options' => array($packages[$i]['Package']['id'] => $packages[$i]['Package']['name'],),
'class' => 'testClass',
'hiddenField' => false, // added for non-first elements
));
}
echo "</div>";
}
?>

I'm not really familiar with CakePHP but a quick Google Search gave me this
$options = array(
'standard' => 'Standard',
'pro' => 'Pro'
);
$attributes = array(
'legend' => false,
'value' => $foo
);
echo $this->Form->radio('type', $options, $attributes);
So you should add the attribute 'value' and set it as your default selected radio

If you try the associated value option it should work.
<?php
echo $this->Form->input('status', array(
'div' => true,
'label' => true,
'type' => 'radio',
'legend' => false,
'required' => false,
'hiddenField'=>false,
'options' => array(
1 => 'High',
2 => 'Medium',
3 => 'Low'
)
'value' => 'Medium'
) ); ?>

<?php
echo $this->Form->input('status', array(
'div' => true,
'label' => true,
'type' => 'radio',
'legend' => false,
'required' => false,
'hiddenField'=>false,
'options' => $status,
'value' => 1 => default value of input
));

Try this
$this->Form->control('company_id', [
'label' => 'Escolha a empresa',
'options' => $companies ,
'value'=> key($companies),
'type' => 'radio',
'required' => true
])

Related

How to add condition in array

I want to add an array condition in input field. if flag == 1 then it is a required field otherwise it is not required field
<?php echo $this->Form->input('course_workbook_answer_file', array(
'type' => 'file',
'label' => false,
'id' => 'course_workbook_answer_file',
'class' => 'form-control',
'name' => 'data[CourseWorkbook][0][course_workbook_answer_file]',
$port_flag == 0 ? 'required' => 'required' : Null
)); ?>
You should change your condition to value instead of complete index.
<?php echo $this->Form->input('course_workbook_answer_file', array(
'type' => 'file',
'label' => false,
'id' => 'course_workbook_answer_file',
'class' => 'form-control',
'name' => 'data[CourseWorkbook][0][course_workbook_answer_file]',
'required' => ($port_flag==0)?'required':Null,
)); ?>

Ajax Request is Not Working After CGridView Update

I Want to open a CJuiModel by Clicking on CGridView CButtonColumn view Button as my CGridView and CJuiModel is as:-
<?php
$dataProvider = new CArrayDataProvider($model->exhibitorLocation, array(
'keys' => array('productId'),
));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'id' => 'exhibitor-location-grid',
'summaryText' => false,
'pager' => array(
'class' => 'CLinkPager',
'header' => false,
),
'columns' => array(
array(
'header' => 'S No.',
'value' => '++$row',
),
array(
'header' => 'Hall No',
'value' => '$data->location->hallNo',
),
array(
'header' => 'Stand No',
'value' => '$data->standNo',
),
array(
'class' => 'CButtonColumn',
'header' => 'Manage',
'template' => '{view}{delete}',
'buttons' => array(
'view' => array(
'imageUrl' => $this->module->assetsUrl . "/images/info_icon.png",
'options' => array('class' => 'exb-location'),
'url' => 'Yii::app()->createUrl("admin/venuemap/viewExbLocation", array("exbLocId"=>$data->primaryKey))',
),
'delete' => array(
'imageUrl' => $this->module->assetsUrl . "/images/delete_icon.png",
'url' => 'Yii::app()->createUrl("admin/venuemap/deleteExbLocation", array("exbLocId"=>$data->primaryKey))',
),
),
'htmlOptions' => array(
'style' => 'width:100px;float:center'
)
),
),
'rowCssClassExpression' => '($row%2 ? "visit2" : "visit1")',
'itemsCssClass' => 'visitor_list',
'htmlOptions' => array(
)
));
?>
<div class="name_field">Hall No<span>*</span></div>
<?php echo CHtml::dropDownList('hall-no', '', CHtml::listData(Venuemap::model()->findAll(), 'locationId', 'hallNo'), array('class' => 'name_input2', 'id' => 'hall-no')); ?>
<div class="name_field">Stand No</div>
<?php echo CHtml::textField('Stand No', '', array('class' => 'name_input', 'id' => 'stand-no')); ?>
<?php
echo CHtml::ajaxLink("Add Location", Yii::app()->createUrl('admin/venuemap/addExbLocation'), array(
'type' => 'post',
'data' => array(
'exhibitorId' => $model->exhibitorId,
'standNo' => 'js: $("#stand-no").val()',
'locationId' => 'js: $("#hall-no").val()'
),
'success' => 'function(html){ $.fn.yiiGridView.update("exhibitor-location-grid"); }'
), array('class' => 'search_btn'));
?>
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id' => 'location-dialog',
'options' => array(
'title' => 'View Location',
'autoOpen' => false,
'modal' => true,
'width' => 'auto',
'height' => 'auto',
'resizable' => false
),
));
$this->endWidget();
?>
<?php
Yii::app()->clientScript->registerScript('view-location-ajax', "
$('.exb-location').click(function(){
url=$(this).attr('href');
$.ajax({
type: 'post',
url: url,
success: function(result){
$('#location-dialog').html(result).dialog('open');
$('#draggable').draggable();
return false;
}
});
return false;
});
");
?>
View Button is working fine and open a CJuiModel on click but when I add a new Location in CGridView .
On Clicking on view button my CJuiModel is not opening and page is redirecting to Url.Why my jquery is getting failed on updating CGridView. Help..
Change ajax call using JQuery 'on' instead direct event binding:
This:
...
$('.exb-location').click(function(){
...
Change for:
...
$('.exb-location').on('click', '.exb-location', function(){
...
Html Elements are destroyed and regenerated after CGridview refresh. Direct JQuery events only work with existing elements before DOM load. When they are removed, or new elements are added later, direct events don't work.

How to configure ObjectMultiCheckBox to persist an empty element?

I have the following element in my form, and I tried all possible options found in the web to allow empty value for element:
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'directPractice',
'options' => array(
'label' => 'A. Check all direct practice field education assignments',
'label_attributes' => array(
'class' => 'label-multicheckbox-group'
),
'required' => false,
'allow_empty' => true,
'continue_if_empty' => false,
'object_manager' => $this->getObjectManager(),
'target_class' => 'OnlineFieldEvaluation\Entity\FieldEducationAssignments', //'YOUR ENTITY NAMESPACE'
'property' => 'directPractice', //'your db collumn name'
'disable_inarray_validator' => true,
'value_options' => array(
'1' => 'Adults',
'2' => 'Individuals',
'3' => 'Information and Referral',
'4' => 'Families',
array(
'value' => 'Other',
'label' => 'Other (specify)',
'label_attributes' => array(
'class' => 'bindto',
'data-bindit_id' => 'otherDirectPracticeTxt_ID'
),
'attributes' => array(
'id' => 'otherDirectPractice_ID',
),
)
),
),
'attributes' => array(
'value' => '1', //set checked to '1'
'multiple' => true,
)
));
And I am always getting the same error message when it is empty:
Validation failure 'directPractice':Array
(
[isEmpty] => Value is required and can't be empty
)
Ok, this is a best what I could come up after researching on it. Solution is inspired by these question.
Form: hidden field and ObjectMultiCheckBox
public function init()
{
$this->setAttribute('method', 'post');
$this->setAttribute('novalidate', 'novalidate');
//hidden field to return empty value. If checkbox selected, checkbox values will be stored with empty value in Json notation
$this->add(array(
'type' => 'Hidden',
'name' => 'otherLearningExperiences[]', // imitates checkbox name
'attributes' => array(
'value' => null
)
));
$this->add(array(
// 'type' => 'Zend\Form\Element\MultiCheckbox',
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'otherLearningExperiences',
'options' => array(
'label' => 'C. Check other learning experiences',
'object_manager' => $this->getObjectManager(),
'target_class' => 'OnlineFieldEvaluation\Entity\FieldEducationAssignments',
'property' => 'otherLearningExperiences',
'label_attributes' => array(
'id' => 'macro_practice_label',
'class' => 'control-label label-multicheckbox-group'
),
'value_options' => array(
array(
'value' => 'seminars',
'label' => 'Seminars, In-Service Training/Conferences',
'label_attributes' => array(
'class' => 'bindto',
'data-bindit_id' => 'seminarsTxt_ID'
),
'attributes' => array(
'id' => 'seminars_ID',
),
),
array(
'value' => 'other',
'label' => 'Other (specify)',
'label_attributes' => array(
'class' => 'bindto',
'data-bindit_id' => 'otherLeaningExperiencesTxt_ID'
),
'attributes' => array(
'id' => 'otherLeaningExperiences_ID',
),
)
),
),
'attributes' => array(
//'value' => '1', //set checked to '1'
'multiple' => true,
'empty_option' => '',
'required' =>false,
'allow_empty' => true,
'continue_if_empty' => false,
)
));
Validator
$inputFilter->add($factory->createInput(array(
'name' => 'otherLearningExperiences',
'required' => false,
'allow_empty' => true,
'continue_if_empty' => true,
)));
}
View
//hidden field to be posted for empty value in multicheckbox
echo $this->formHidden($form->get('otherLearningExperiences[]'));
$element = $form->get('otherLearningExperiences');
echo $this->formLabel($element);
echo $this->formMultiCheckbox($element, 'prepend');

How to add value dynamically to multiselect attribute in magento?

I have used the following code for to add multiselect attribute in mine custom module Form
$fieldset->addField('multiselect2', 'multiselect', array(
'label' => Mage::helper('checkout')->__('Select Type2'),
'class' => 'required-entry',
'required' => true,
'name' => 'title',
'onclick' => "return false;",
'onchange' => "return false;",
'value' => '4',
'values' => array(
'-1'=> array( 'label' => 'Please Select..', 'value' => '-1'),
'1' => array(
'value'=> array(array('value'=>'2' , 'label' => 'Option2') , array('value'=>'3' , 'label' =>'Option3') ),
'label' => 'Size'
),
'2' => array(
'value'=> array(array('value'=>'4' , 'label' => 'Option4') , array('value'=>'5' , 'label' =>'Option5') ),
'label' => 'Color'
),
),
'disabled' => false,
'readonly' => false,
'after_element_html' => '<small>Comments</small>',
'tabindex' => 1
));
Its working Good. But i want to set the values as dynamically. That means I want to display all category list under this multiselect attribute. I have searched lot of things. But i cant get. Can we use foreach loop for adding value to that attribute? or have any other way to do this? Please help me guys!
You can prepare values array with any data before and then use it in your form. For categories it's smth like this:
$categories = Mage::getModel('catalog/category');
$values = array();
foreach ($categories as $category) {
$values[] = array('label' => $category->getName(), 'value' => $category->getId());
}
Hope it's clear and works for you.

PHP attachment in write form

I need to edit the following form so it will have attach file function:
function _getWriteForm()
{
$aForm = array(
'form_attrs' => array(
'name' => 'WallPostText',
'action' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'post/',
'method' => 'post',
'enctype' => 'multipart/form-data',
'target' => 'WallPostIframe',
'onsubmit' => 'javascript:return ' . $this->_sJsPostObject . '.postSubmit(this);'
),
'inputs' => array(
'content' => array(
'type' => 'textarea',
'name' => 'content',
'caption' => '',
'colspan' => true
),
'submit' => array(
'type' => 'submit',
'name' => 'submit',
'value' => _t('_wall_post'),
'colspan' => true
)
),
);
I wanted to edit HTML file first, but the only HTML code for this is:
<!-- Post Text -->
<div class="wall-ptype-cnt wall_text">__post_wall_text__</div>
Can someone please help me with editing array to allow file attachment?
Thank you.
You should try something like this:
function _getWriteForm()
{
$aForm = array(
'form_attrs' => array(
'name' => 'WallPostText',
'action' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'post/',
'method' => 'post',
'enctype' => 'multipart/form-data',
'target' => 'WallPostIframe',
'onsubmit' => 'javascript:return ' . $this->_sJsPostObject . '.postSubmit(this);'
),
'inputs' => array(
'content' => array(
'type' => 'textarea',
'name' => 'content',
'caption' => '',
'colspan' => true
),
'file' => array(
'type' => 'file',
'name' => 'upload_file',
'caption' => 'Upload',
'colspan' => true
),
'file_two' => array( //I added a second input file
'type' => 'file',
'name' => 'upload_file_two',
'caption' => 'Upload',
'colspan' => true
),
'submit' => array(
'type' => 'submit',
'name' => 'submit',
'value' => _t('_wall_post'),
'colspan' => true
)
),
);
I hope it helps.
Saludos ;)

Categories