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).
Related
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));
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>
The Zend Form is proving to be a bit tricky for me, even as much as I am working with it lately...
I have this form and I am attempting to dynamically create the several checkboxes for it. It is working out alright, except I cannot seem to get the 'value' attribute to change.
In my Zend form class I have this snippet...
// psychotic symptoms
$this->addElement('checkbox', 'psychoticsymptom', array(
'label' => 'psychoticsymptom',
'name' => 'psychoticsymptom',
));
In my view (phtml) I am calling it like this...
<div class="element">
<?php // Psychotic Symptoms
$Criteria = new Criteria();
$Criteria->add( DictionaryPeer::CATEGORY, 'MAR: Psychotic Symptoms' );
$Criteria->addAscendingOrderByColumn( 'Ordinal' );
$this->PsychoticSymptomsList = DictionaryPeer::doSelect( $Criteria );
foreach( $this->PsychoticSymptomsList as $Symptom ) {
$form->psychoticsymptom->setValue($Symptom->getDictionaryId());
$form->psychoticsymptom->setAttrib('name', $Symptom->getWord());
echo $Symptom->getDictionaryId(); // prove my id is coming through... (it is)
$form->psychoticsymptom->getDecorator('label')->setTag(null);
echo $form->psychoticsymptom->renderViewHelper();
$form->psychoticsymptom->setLabel($Symptom->getWord());
echo $form->psychoticsymptom->renderLabel();
echo '<br />';
}
?>
</div>
Everything seems to be working fine, except the value attribute on each checkbox is rendering a value of '1'. I have tried moving the 'setValue' line to several different positions, as to set the value before the form element renders but I am having no luck getting this to work. It's worth any effort to me because I need to do the same type of operation in many areas of my application. I would have done this a bit different too, but I am re-factoring another application and am trying to keep some things unchanged (like the database for instance).
Any help is much appriciated
Thanks
you can try to overwrite the "checkedValue" and "uncheckedValue". check this reference
$this->addElement('checkbox', 'psychoticsymptom', array(
'label' => 'psychoticsymptom',
'name' => 'psychoticsymptom',
'checkedValue' => 'checked Value',
'uncheckedValue' => 'unchecked Value'
));
You seem to only have one psychoticsymptom element "checkbox" which your adding (changing) the value too for each $this->PsychoticSymptomsList.
Maybe you would be better off using a multicheckbox element instead.
I have a mobile website form that I want to add type attributes to the inputs so that there correct keyboard format will pop up.
However in cakephp setting the type as number a textarea is created instead of the input and the type is not set.
Setting type as text does work.
How do I overide this and have cakephp just keep it as a text input with type=number?
<?php echo $form->input('phone',array('type' => 'number')); ?>
Result:
<textarea id="UserCardExpires" rows="6" cols="30" name="data[User][card_expires]"class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset"></textarea>
This is ok:
<?php echo $form->input('postcode' ,array('type' => 'text')); ?>
Result
<input type="text" id="UserPostcode" name="data[User][postcode]" class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset">
On older versions of Cake, the Form helper won't automagically interpret $options['type'] as the HTML5 input-element type attribute. You have to force it by using "type" as an option on an explicit text element.
Use the following:
$form->text( 'phone', array( 'type' => 'number' ) );
I think phone numbers might be:
echo $form->text( 'phone', array( 'type' => 'tel' ) );
EDIT:
Sorry I'm an idiot, thats HTML5.
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>