Dropdown list size with CakePHP - cakephp

I am currently using CakePHP and I have a dropdown list that contains a lot of text. This is actually a list of predefined messages that the user can select.
I wonder if there is a way to tell CakePHP to keep the original size of the dropdown list to not allow text spans to the right, force a carriage return somehow.
I also use Foundation, can be a solution at this level too?
Thanks

You can add class or Id to that select box and apply css on that to control width of that box like:
$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('gender', $options, array('escape' => false));
Which will look like
<select name="data[User][gender]" id="UserGender">
<option value=""></option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
then add css like
#UserGender {width : 200px;}

Related

Cakephp 3 - select not submitted if no selection is made

I have a form ( generated using the form helper ) with this select input
<div class="input select">
<label for="pilot-ratings">Pilot Ratings</label>
<select name="pilot_ratings" class="listbox" size="5" id="pilot-ratings">
<option value="1">Habilitación de Vuelo Nocturno Local</option>
<option value="3">Habilitación Cat. II / Cat. III</option>
<option value="5">Habilitación de Remolque de Planeador</option>
</select>
</div>
Only when an option is selected, that option is added to $this->request->data['pilot_ratings'].
Is there any way to force submitting all of the options of the select input every time no matter an option is selected or not ?
Thanks.
Regards.
How are you moving options between the select boxes?
to automatically submit an option you need to add selected="selected" attribute to the <option> element. To submit many options in a select box in the form you need to add multiple to the <select> element
if you want to automatically submit all options in the list on page load it would need to look like this
<select name="pilot_ratings" class="listbox" size="5" id="pilot-ratings" multiple>
<option value="1" selected="selected">Habilitación de Vuelo Nocturno Local</option>
<option value="3" selected="selected">Habilitación Cat. II / Cat. III</option>
<option value="5" selected="selected">Habilitación de Remolque de Planeador</option>
</select>
this can be acheived using the cakephp Form helper like:
<?= $this->Form->input('pilot_ratings',[
'type' => 'select',
'class' => 'listbox',
'size' => 5,
'id' => 'pilot_ratings',
'multiple' => 'multiple',
'options' => [
['name' => 'Habilitación de Vuelo Nocturno Local', 'value' => '1', 'selected' => 'selected'],
['name' => 'Habilitación Cat. II / Cat. III', 'value' => '2', 'selected' => 'selected'],
['name' => 'Habilitación de Remolque de Planeador', 'value' => '5', 'selected' => 'selected']
]
]); ?>
Havent tested the syntax of the form helper code but it is the right idea.
One thingI just thought about is how you read the data after the form has been submitted. The data will still be submitted like:
pilot_ratings=1
pilot_ratings=3
pilot_ratings=5
By default I think (havnt actually tested this) $this->request->data['pilot_ratings']; will only hold one of these values, either the 1st one-or the last one as it keeps overwritting itself.
If this that is the case you may need to change the form method to get
and then extract all the values from the query string which you can get from:
$this->request->here();

CakePHP - pass an empty value into a radio button

How can I pass an empty value to a radio button in Cake?
I've looked into Cake's 2.0 helpers form documentation but I didn't seem to find much about this.
Maybe some of you can enlighten me?
Cheers
FormHelper::radio(string $fieldName, array $options, array $attributes)
Creates a set of radio button inputs.
$options = array('M' => 'Male');
$attributes = array('legend' => false);
echo $this->Form->radio('gender', $options, $attributes);
Will output:
<input name="data[User][gender]" id="UserGender_" value="" type="hidden" />
<input name="data[User][gender]" id="UserGenderM" value="M" type="radio" />
If for some reason you don’t want the hidden input, setting
$attributes['value'] to a selected value or boolean false will do just
that.
As you can see on the documentation of FormHelper::radio

Change selected <option> of a <select> using ng-click

I'm wondering how i can change the selected <option> of a <select> using ng-click.
<select ng-model="orderProp">
<option value="name">Alphabetical</option>
<option value="age">Newest</option>
</select>
order
Can this be done like in the example above?
thx,
The below code seems to be working. Please check if that is what is required. What I have added is the single quotes surrounding 'name' in anchor tag.
<select ng-model="orderProp">
<option value="name">Alphabetical</option>
<option value="age">Newest</option>
</select>
order
This can be done using two ways, either call a function on click of the anchor or just set the value 'name' as a string.
1) http://jsfiddle.net/HB7LU/10717/ here you can directly set the string 'name' on ng-click
2) Or this, http://jsfiddle.net/HB7LU/10718/
order
$scope.fun=function(){
$scope.orderProp= 'name'
}

How to create elements in Zend Form with array names

I am trying to create a very complex form with ZF2, and I can't find a solution to implement array based names in input elements.
This is a chunck of code from my actual form:
<input type=text name=table[name] value="Table Name">
<input type=text name=table[title] value="Table Title">
<input type=text name=table[columns][names][] value="Column Name 1">
<input type=text name=table[columns][names][] value="Column Name 2">
<input type=text name=table[columns][names][] value="Column Name 3">
<input type=text name=table[columns][labels][] value="Column Label 1">
<input type=text name=table[columns][labels][] value="Column Label 2">
<input type=text name=table[columns][labels][] value="Column Label 3">
I know how to work with Zend/Form, but this is an advanced usaged I can`t master.
Your code looks like a dynamic form rendered into table format. One possibility besides Zend Fieldsets is to generate your basic form structure dynamically wihtin OOP-Constructor, like #Sam said.
In your case, you should have a concept of which the form is structured. E.g. you have an array with data from a database, which gives you for example the number and names of your "columns" and so the structure how to build your dynamic form.
Following example uses Zend Form constructor to generate a dynamic form. Number of columns are dynamic and specified by given array $tableColumns
<?php
class DynamicForm extends Form {
/**
* This constructor builds a Zend Form dynamically
*
* #param array $tableColumns Dynamic data e.g. 'Column Name 1'
*/
public function __construct($tableColumns) {
// Add table name input
$this->add(array(
'type' => 'text',
'name' => 'name'
));
// Add table title input
$this->add(array(
'type' => 'text',
'name' => 'title'
));
// iterate each dynamic data
foreach($tableColumns as $column) {
$this->add(array(
'type' => 'text',
'name' => $column['name']
));
}
}
}
This is how you generate the Zend form structure. Rendering works similar, but within yout view code.
<?php
echo "<table>";
echo "<tr>";
echo "<td>". $this->formElement($form->get('name')) . "</td>";
echo "<td>". $this->formElement($form->get('title')) . "</td>";
foreach($tableColumns as $column) {
echo "<td>". $this->formElement($column) ."</td>";
}
echo "</tr>";
echo "</table>";
?>
In my opinion it is much more comfortable to render a form dynamically into a table format, than it's possible a Fieldset.
But nevertheless Zend Form Collection (e.g. Fieldsets) is a good choice, if you have to add or remove Form-elements dynamically with Javascript and handle your form data also highly dynamically.
More information about Zend From Collections are described at the ZF2 Reference Manual
You make use of Zend\Form\Element\Fieldset for this kind of scenario. It helps a great deal with proper OOP-Construction of Forms.

Country/State Dropdown in CakePHP

How do i deal with dependent combo boxes in the views with the form helper. For example:
Country Select Box (Choosing a country shall filter out the states of the selected country)
States Select Box
This should happen with the help of Javascript / Jquery etc. I came across an example for the same with Cake's core AJAX helper but it would be very nice if someone can help with a Javascript example.
Thanks
In views/ edit.ctp
<script type="text/javascript">
$(document).ready(function (){
$('#country').change(function() {
$('#state').load('/controller/getStates/'+$(this).val());
});
});
</script>
<select id="country" name="country">
<option value="1">Greece</option>
</select>
<span id="state">
<select name="state">
<option value=""></option>
</select>
</span>
and in controller.php
function getStates(int countryID){
$this->set('selectbox',
$this->State->find('list',array('conditions'=>'State.Country_id='.$countryID,
'fields;=>array('description')));
}
and views/getStates.ctp
<select name="state">
<option value=""></option>
<?php
foreach($selectbox as $option)
echo '<option value="'.$option['id'].'">'.$option['description'].'</option>'."\n";
?>
</select>
I hope I don't forget something
#gong's solution works well. Just remember to add:
$this->layout = 'ajax';
in the controller and ensure there is a clean ajax.ctp in the layouts folder... otherwise all the layout code will be returned in the ajax response as well as just the dropdown code!
$states = $this->State->find('list', array(
'conditions' => array('State.country_id' =>$codePassed),
'order'=>array('State.stateName ASC'),
'fields' =>array('id','stateName'),
'recursive' => -1
));
$a='';
$a.= "<select name=\"state\">";
$a.= "<option value=\"\">Select state</option>";
foreach($states as $key=>$value){
$a.="<option value=\"$key\">".$value."</option>";
}
$a.="</select>";

Resources