CakePHP - input select not taking select options from a variable - forms

I am developing an application with CakePHP 2.3.2 and I am having some trouble with an input select on a form. I am creating an array, in my Controller, which contains a list of states. In my View I find that when I use this variable in the 'options' field of the input I do not get any select options. If I do a print_r on the variable, in the view, I see exactly what I think I should be seeing for the 'options' field. I have even tried copying this print_r output and putting it in the 'options' field and then the input select works fine.
Here is what I have
In Controller
$options = 'array(1 => \'NSW\',2 => \'ACT\',3 => \'NT\');
$this->set('all_states, $options);
In View
<?php
$options = $all_states;
echo $this->Form->create('Refine', array('url => '/ServiceDirectoryResults/view/refine'));
echo $this->Form->input('field' ,array(
'type' => 'select',
'label' => false,
'options' => $options
));
echo $this->Form->end('Refine Search');
?>
When I run this I see a select with no select options
If I add print_r($options) after the echo $this->Form->end('Refine Search'); I see
array(1 => 'NSW',2 => 'ACT,3 => 'NT')
Which is what I would expect as it is the content of the $options variable which was the $all_states variable passed from the controller. If I take this output from the print_r and replace the $option with it in the input select the select drop down works fine and I see the three options. For some reason I can't work out the select is working fine if I hard code the select options but it will not work if I pass a variable containing the array to the input select.
I would really appreciate if if someone could give me a clue what I am doing wrong here.
Kind Regards
Richard

you might try it like below:
echo $this->Form->input('field', array('type'=>'select','label' => false,
'options' => $options,'default'=>'2'));
to the following HTML being generated:
<option value="2" selected="selected">ACT</option>
option two is shown instead any other one.

Likely issue:
Arrays should not be made as strings like you have:
$options = 'array(1 => \'NSW\',2 => \'ACT\',3 => \'NT\');
Instead, just make an array:
$options = array(1 => 'NSW', 2 => 'ACT', 3 => 'NT');
Other notes:
Why are you setting $options to $all_states only to set it back?
Missing quotes all over - make sure if you start quotes, that you also end them
not good practice to hard-code your URLs (like in your Form->create)

Related

display radio buttons using FormHelper

I am using CakePHP3
I am unable to decipher the documentation on using radio buttons to create for a list of results.
There is create radio button.
Which I tried this way:
foreach ($userGroups as $group_id => $group) {
echo $this->Form->radio("UserGroup.$group_id.id", ['value' => $group_id, 'label' => $group]);
}
There is using select pickers.
Which says all you need to do is set the type to radio. But I checked the api and it looks like it will unset the type.
Anyway, I tried
echo $this->Form->select("user_circle_id", $userGroups, ['type' => 'radio']);
Nothing works.
Please advise.
UPDATE:
$userGroups = [1 => 'Group 1', 2 => 'Group 2']; // basically primary key is keys and the display fields are the values.
Use following syntax :
$selectedStatus = 1;
$attributes = array('legend'=>false,'value'=>$selectedStatus,'class'=>'sortByStatusCourse','name'=>'sortByStatusCourse');
$options = array("1"=>"Active","0"=>"Inactive");
echo $this->Form->radio('is_active', $options,$attributes);
Code explanation :
We are passing parameters to cakePHP core so that it will output Radio button in HTML.
echo $this->Form->radio :
Here 1st param is Name on field in Database. 2nd param is What are the options for radio buttons , its value and text .3rd param is what are its attributes like what should be its default value here I explicitely set it to 1 so it will already tick radio with Activetext.
According to your code : echo $this->Form->radio("UserGroup.$group_id.id", ['value' => $group_id, 'label' => $group]);
I think order of param is wrong as you pass VALUE in 2nd param which should be 3rd param.Check my code.

Magento admin form time field

I am trying to add a time field to an admin form but it does not work:
$fieldset->addField('sched_time', 'time', array(
'label' => Mage::helper('servicemanager')->__('Scheduled Time'),
'style' => 'width:45px',
'class' => 'required-entry',
'required' => true,
'name' => 'sched_time',
'value' => '09,00,00',
));
The problems are as follows:
The data isn't getting stored in the database ('sched_time' is a mysql time field)
'value' doesn't do anything. I need the default value to be set to 09:00:00 (9am)
Reference: http://www.excellencemagentoblog.com/magento-admin-form-field
You need to write some code manually in your controller's saveAction() method which might look like:
public function saveAction(){
$id = $this->getRequest()->getParam('id');
$model = Mage::getModel('module/model')->load($id);
$time = $this->getRequest()->getParam('sched_time');
$sched_time = $time[0] . ':' . $time[1] . ':' . $time[2]; //HH:MM:SS
$model->setData('sched_time', $sched_time);
$model->save();
}
Hope this will help future visitors!
You need to make an observer, after hours trying to find this myself I came across this link magento weight attribute without decimal points
Hope this helps as it helped me

Custom field doesn't appear in sugarCRM

I have created a custom field sugarfield_ast_rec_link_c.php in custom/Extension/modules/Calls/Ext/Vardefs with such content:
`
<?php
$dictionary['Calls']['fields']['ast_rec_link_c'] = array
('name' => 'ast_rec_link_c',
'vname' => 'LBL_AST_REC_LINK_C',
'type' => 'varchar',
'len' => '255',
'source' => 'non-db',
'function' => array('name'=>'getRecordLink',
'returns'=>'html',
'include'=>'custom/modules/Calls/CustomLogic.php')
);
?>
`
Also aded language file in custom/Extension/modules/Calls/Ext/Language. After quick repair my custom field doesn't appear in Studio -> Calls -> Fields. So I can't put it on views. Can anyone help?
You should change
<?php
$dictionary['Calls']['fields']['ast_rec_link_c'] = array(...);
to
<?php
$dictionary['Call']['fields']['ast_rec_link_c'] = array(...);
Remember you should always use bean name (not module one!) as a $dictionary array key while defining new custom fields.
In my opinion best way to check if everything is OK with your custom vardefs is to compare your own ones with existing in cache/modules/<module_name>/BEAN_NAMEvardefs.php

Form->end without a div in CakePHP

I'd like to create a submit button that does NOT have a <div> around it. I assumed using the inputDefaults would make this happen like it does for all of the forms inputs, but - no luck.
Obviously I could just create a submit button via HTML, without CakePHP, but - I was hoping there'd be a cake answer. Here's what I've tried:
$this->Form->create(false, array('inputDefaults' => array('div'=>false)));
$this->Form->end('Submit');
echo $this->Form->submit('Submit', array('div'=>false));
Should do what you are after. The other example may have been Cake 1.2 or something; not sure.
It also appears you can just do this instead:
<?php
$options = array(
'label' => 'Update',
'value' => 'Update!',
'div' => false
)
);
echo $this->Form->end($options);
That looks more cakey.

Render view from Zend_form

I try it via my Zend_form:
$output .= $this->_view->render('admin/form.phtml'
, array('id' => $this->getName()
, 'action' => $this->getAction()
, 'method' => $this->getMethod()
, 'enctype' => $this->getEnctype()
, 'data' => array('code' => $code
, 'name' => $name
, 'description' => $description)));
but when i <?php echo $this->enctype; ?> in admin/form.phtml i got nothing.
admin/form.phtml is rendered correctly
Choosing render just displays the output, Zend does not pass your data to the view. But Using view partials, you could achieve that.
From the documentation,
The Partial view helper is used to render a specified template within
its own variable scope. The primary use is for reusable template
fragments with which you do not need to worry about variable name
clashes. Additionally, they allow you to specify partial view scripts
from specific modules.