Usually, I always used CakePHP form helper to list down select box with options.
I would like to know,if I can create it manually(I mean without CakePHP form helper) ?
Example:
Using cakephp form helper to create select box
<?php echo $this->Form->input('product_name',array('options'=>$myoptions));?>
But I want to do something like below
<select name="data[Product][product_name]" id="product">
<options value="1">One</options>
<options value="2">Two</options>
<options value="3">Three</options>
</select>
Any ideas?
Thanks
CakePHP 2.5.7
--EDIT--
I want to clone multiple select box that will follow CakePHP form structure.
Okay the problem start here.
By default I'd use CakePHP form helper to draw the select box and their options.
I wrote this kind of code:
<?php echo $this->Form->input('Redemption.0.type_id',array('class'=>'form-control','id'=>'selectProduct','label'=>false,'empty'=>'Choose Type','options'=>$types));?>
It will return in HTML structure like below:
<select name="data[Redemption][0][type_id]" class="form-control" id="selectProduct">
<option value="">Choose Type</option>
<option value="1">Toner</option>
<option value="2">Ink Catridge</option>
</select>
I want to clone the existing select box above to new select box but with different name value like this:
<select name="data[Redemption][1][type_id]" class="form-control" id="selectProduct">
<option value="">Choose Type</option>
<option value="1">Toner</option>
<option value="2">Ink Catridge</option>
</select>
Notice that integer value on name="data[Redemption][1][type_id]" has increased.That is what I wanted.
But now,I've no idea to do cloning with data changing as stated.
I have try several jquery method such as .clone() .append() but it's not work as the options data inside the select box become redundant.
Can you please enlighten me how to duplicate/clone select box in CakePHP?
Thanks.
CakePHP 2.5.7
<?php
echo $this->Form->input( 'product_name', array(
'type' => 'select',
'options' => array(
'key1' => 'val1',
'key2' => 'val2',
),
'empty' => true,
));
?>
You can add other conditional values to your select options just by adding values to the variable array like this:
$myOptions = array(
'1' => 'One',
'2' => 'Two',
'3' => 'Three'
);
// If user make some action
if($someAjaxAction == true) {
$myOptions[4] = 'Four';
}
echo $this->Form->input('product_name', array('options' => $myOptions));
Related
i'm currently a newbie when it comes to the yii framework / php. I would like some assistance creating this Chtml::DropDownList.
http://www.yiiframework.com/doc/api/1.1/CHtml#dropDownList-detail
Chtml::dropDownList($name, $select, $data)
I understand that $data is the array of data I will be loading in from my Database.
But can someone explain to me how $name, and $select truly works. I have a hard time finding documentation that explains this at a extremely dumbdown level.
I manage to get this piece of code working, but I would prefer using Chtml::dropdownlist.
<div class="row">
<?php
echo $form->dropDownList($model, 'id',
Chtml::listData(UsersTeam::model()->findAllByAttributes(array('coachId'=>$model->id)), 'id', 'teamName'),
array('empty'=>'Select Team'))
?>
</div>
I would like to be able to display all teamName for the current user that he is enlisted in.
I'm currently displaying this in the model view of a User, but the information I need is from UserTeam which holds the teams for the users.
'memberOfTeams' => array(self::MANY_MANY, 'UsersTeam', '{{teamMembers}}(userId, teamId)'),
'coachOfTeams' => array(self::HAS_MANY, 'UsersTeam', 'coachId'),
$name is the name="mySelect" form value it will have (the one that will be passed if sent as a form i.e. $_POST['mySelect']).
$select is the preselected ID. Say you have an array...
$options = array('12' => 'Twelve', '10' => 'Ten');
And your dropdown looks like this...
echo CHtml::dropDownList('mySelect', '12', $options);
Then 'Twelve' will be the preselected item in the dropdown and $_POST['mySelect'] will be the value passed when the form is sent.
You can add additional html options to each <option> tag, using the fourth parameter CHtml::dropDownList accepts, like so:
$htmlOptions = array(
// adds to the select element
'style' => 'cursor: pointer;',
// adds to the actual options
'options' => array(
'12' => array('title' => '12')
)
);
And updating the call to:
echo CHtml::dropDownList('mySelect', '12', $options, $htmlOptions);
The finished list would look like this:
<select name="mySelect" style="cursor: pointer;">
<option value="12" selected="selected" title="12">Twelve</option>
<option value="10">Ten</option>
</select>
You can easily do the same with CHtml::activeDropDownList.
So your code will look like:
<div class="row">
<?php
echo CHtml::activeDropDownList($model, 'id',
Chtml::listData(UsersTeam::model()->findAllByAttributes(array('coachId'=>$model->id)), 'id', 'teamName'),
array('empty'=>'Select Team'))
?>
</div>
Problem
Show lengthy options on mouse hover in select box with a fixed width.
Names are hidden here:
Rendered HTML in browser
<select id="prim" size="5" multiple="multiple" scroabble="1" name="prim[]">
<option value="Applied Research Associates Inc.">Applied Research Associates Inc.</option>
</select>
Required Output
Show span or title as below
Expected HTML
<select id="prim" size="5" multiple="multiple" scroabble="1" name="prim[]">
<option value="Applied Research Associates Inc." title="Applied Research Associates Inc.">Applied Research Associates Inc.</option>
</select>
My CakePHP Code
echo $form->input('prim',
array('options'=>$refined_list,
'type'=>'select',
'scrollable'=>true,
'multiple'=>true,
'name'=>'prim',
'label'=>false,
'size'=>'5'));
What attribute should I add to make desired changes?
EDIT/UPDATE: One way might be to update my $refined_list array with title field as suggested by #ammu. I must wait for a better solution.
There is no such way to add attribute to select option in cakephp documentation.Have a look at this, it might help you for adding attributes to select option.
http://www.dereuromark.de/2012/03/01/some-new-crazy-cakephp-tricks/
Well use it like this and you'll get what you want:
$options = array(
...
array('name' => 'United states', 'value' => 'USA', 'title' => 'the title that you want'),
array('name' => 'USA', 'value' => 'USA', 'title' => 'the other title that you want'),
);
echo $this->Form->input('test', array('type'=>'select', 'options'=>$options));
you can also add any attribute that you want in the form of array for each option (class, placeholder, etc).
I have table types and i want to build selectbox with all values from this table
In my controller i wrote this code
$allRegistrationTypes = RegistrationType::model()->findAll();
$this->render('index', array('allRegistrationTypes' => $allRegistrationTypes))
How build selectbox in view file ?
Well then its pretty simple all you need to do is first create List Data like
CHtml::ListData(allRegistrationTypes,'value you want to pass when item is selected','value you have to display');
for ex
typeList = CHtml::ListData(allRegistrationTypes,'id','type');
now remember both id and type are fields in table
now all you have to do is if you are using form then
<?php echo $form->dropDownList($model, 'type_id', $typeList, array('empty'=>'Select a tyoe')); ?>
and if you need multiple you can pass multiple => multiple in the array as htmlOptions
You would use CHtml::dropDownList, or activeDropDownList if there is a "parent" model and you want to leverage its validation rules.
If you want to make the <select> element multiple-selection-capable, pass in 'multiple' => 'multiple' and 'size' => X as part of the $htmlOptions parameter.
Simplest Method to get "Select Box" in YII Framework:
<div class="row">
<?php
echo $form->labelEx($model,'county');
$data = CHtml::listData(County::model()->findAll(), 'id', 'county');
$htmlOptions = array('size' => '1', 'prompt'=>'-- select county --', );
echo $form->listBox($model,'county', $data, $htmlOptions);
echo $form->error($model,'county');
?>
</div>
Good Luck..
I'm using CakePHP 2.1.1, and upgraded to 2.2.0 and the issue exists in both. I am using the FormHelper to generate a Select dropdown with the options defined in an array. When it generates the options, it repeats some of them. Which ones and how many repeat changes depending on which record I am editing.
Using the following code in my view:
debug($advisors);
echo $this->Form->input('advisor',array('options'=>$advisors));
I see:
/app/View/Students/edit.ctp (line 38)
array(
'K-1' => 'K-1',
'K-2' => 'K-2',
'2-3' => '2-3',
'3-5n' => '3-5n',
'3-5s' => '3-5s',
'4-5' => '4-5',
'6-8' => '6-8'
)
and then a dropdown with the following options:
<option selected="selected" value="K-1">K-1</option>
<option value="K-2">K-2</option>
<option value="2-3">2-3</option>
<option value="3-5n">3-5n</option>
<option value="3-5s">3-5s</option>
<option value="4-5">4-5</option>
<option value="6-8">6-8</option>
<option value="K-1">K-1</option>
<option value="K-2">K-2</option>
<option value="2-3">2-3</option>
What else should I be checking?
Ok, never mind. It was a javascript function I wrote a while back that was messing with the generated options.
I am using Zend Frameworks ViewHelpers.
I am trying to pass something to set disabled attribute in SELECT. For example if
$countries = array(1=>'Select Option', 2=>'us', 3=>'uk')
and
formSelect('country','us',null,$this->countries)
I need to diable first option i.e. 'Select Option'
Do you have any idea ?
Thanks in addvance
I don't think you can disable one element? If you disable it then why have it at all?
You can only disable the whole <select> input.
Suggest you write validation to not accept the first element.
Edit after OP's comment about being able to do this
Here is another answer
// Get the countries element (do this after adding your options), then set the
// attribute disable for option '1'
$form->getElement("countries")->setAttrib("disable", array(1));
This is suggested here
There IS a way of doing it through Zend_Form (at least on my current ve 1.11):
$this->addElement
(
"select","selectName",
array("multiOptions"=>array("one","two","three"), "disable"=>array(0,1))
);
That one will disable first two options.
Credit goes to jakenoble.
Just reformatted the code to use the formSelect-viewhelper instead of a form-element.
<?php
$countries = array(1 => 'Select Option', 2 => 'us', 3 =>'uk');
echo $this->formSelect('country', 2, array('disable' => array(1)), $countries)
This will result in:
<select name="country" id="country">
<option value="1" label="Select Option" disabled="disabled">Select Option</option>
<option value="2" label="us" selected="selected">us</option>
<option value="3" label="uk">uk</option>
</select>