I am new to cake php.I have to enter multiple city id in a database with comma separator like 10,11,12 where 10,11,12 is city id
When I use this code
**<?php
echo $form->input('city_id', array('options' => $city, 'class'=>'input_box2',
'empty' => 'Please select a city'), null, array('id' => 'city_id', 'label' => 'City'
));**
I am able to insert one city id at a time,but i need to insert multiple city id with comma separator .
I am useing the following code but it is not working.please help
**<?php
echo $form->input('city_id', array('options' => $city, 'type'=>'select','multiple'
=> 'true', 'empty' => 'Please select city'), null, array('id' => 'city_id', 'label' => 'City'));**
You need to modify POST data in your controller:
in CakePHP 2.x
$city_ids = Set::extract('/YourModel/city_id', $this->request->data);
$this->request->data['YourModel']['city_id'] = implode(",", $city_ids);
in CakePHP 1.x
$city_ids = Set::extract('/YourModel/city_id', $this->data);
$this->data['YourModel']['city_id'] = implode(",", $city_ids);
Related
I want top populate dropdown date field in cakephp 2.x.
my date format is 1987-07-05 but when i using cakephp date.
echo $this->form->input('birthday', array(
'type' => 'date',
'id' => 'birthday',
'required' => false,
'empty' => array(
'month' => 'Month', 'day' => 'Day', 'year' => 'Year'
),
'id' => 'birthday',
'minYear' => date('Y') - 16,
'maxYear' => date('Y') - 60,
'label' => FALSE,
'style'=>'width:100px',
'value'=>$getProfile['Performer_detail']['birthday']
));
With the code above, I am getting 3 dropdown fields: Month, Day, and Year: I am also getting 1987-07-05 in $getProfile['Performer_detail']['birthday'] but these 3 fields are not selected.
Please help me.
Use this code -
<?php echo $this->form->dateTime(
'birthday', 'MDY','', array(
'id'=>'birthday','value'=>$getProfile['Performer_detail']['birthday'], 'empty' => array('month' => 'Month', 'day' => 'Day', 'year' => 'Year'),'label'=>'','minYear'=>date('Y')-16,'maxYear'=>date('Y')-60
)
);?>
I am pasting the demo to understand the Date input field in cakephp.
table: users
id int
name varchar
birthday date
add.ctp
<?php
echo $this->Form->create();
echo $this->Form->input('name');
echo $this->Form->input('birthday',array('dateFormat' => 'YDM'));
echo $this->Form->end();
?>
Form will look like this,Notice the values.It will display the current date by default.
I have added one record with name Kevin and Date 2019-5-September.Now this is when i edit the same record
edit.ctp
<?php
echo $this->Form->create();
echo $this->Form->input('name');
echo $this->Form->input('birthday',array('dateFormat' => 'YDM'));
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->end('submit');
?>
As you can see that in both file add.ctp and edit.ctp file i haven't included extra value for the birthday.Cakephp will autofill the field data in edit form if you have correctly followed the cakephp conventions.So here no need to add the $getProfile['Performer_detail']['birthday']
The database is like this. Owner has many cats.
Owner ownerID ownerName
Cat catID catType ownerID
I am trying to add the new cat and on the field of ownerID, I want to show the droplist of all the ownerName. How can I do that?
I'm assuming that you already have owner model, for dropdown list of owner
In Conrtoller
$Owner = $this->Owner->find('list', array('fields' => array('Owner.ownerID', 'Owner.ownerName')));
$this->set('owners', $owner);
In View
<?php echo $form->input('owner', array(
'label' => false,
'id' => 'owner_id',
'options' => $owners
)); ?>
Write in your contoller:
$all_security_question = $this->Security_question->find('list',array('fields'=>array('id','question')));
$this->set("all_security_question",$all_security_question);
Write in your Model:<?php echo $this->Form->input('security_question_id', array(
'label' => false,
'id' => 'id',
'options' =>$all_security_question
)); ?>
Inspect the 'Qty' form input with Firebug
<input id "Item0Quantity">
I am having difficult validating this element produced by this code:
<?php echo $form->input("Item.$id.quantity.", array('label' => 'Qty', 'class' => 'txt'));?>
I am working with a form input element that is prefixed with the database table name (Item) and a unique id, the reason for this is to add some JavaScript functionality. Though it still needs CakePHP validation on the Item model.
Here is the input form element that needs to be validated:
<?php echo $form->input("Item.$id.quantity.", array('label' => 'Qty', 'class' => 'txt'));?>
So how will the will be written in the items model? My attempt:
'Item.$id.quantity' => array(
'rule' => 'numeric',
'allowEmpty' => true,
'message' => 'quanitity mut be numeric'
),
The validation will look the same, still by just the field name:
'quantity' => array(
'rule' => 'numeric',
'allowEmpty' => true,
'message' => 'quanitity mut be numeric'
),
When you use saveAll Cake will iterate through each Item and validate it accordingly.
Title seems a bit odd as I am trying to find way's to explain my dilema in layman's terms.
What I am trying to achieve is is from what I can gather, fairly simple but.. I just can't seem to place my finger on it.
I have a drop down selection menu which users can select a country of residence which resides in a helper - example below:
class CountryListHelper extends FormHelper {
var $helpers = array('Form');
function select($fieldname) {
$list = $this->Form->input($fieldname , array(
'type' => 'select', 'label' => 'Country of Residence', 'options' => array(
'' => 'Please select a country',
'AF' => 'Afganistan',
'AL' => 'Albania',
'DZ' => 'Algeria',
.................
),
'error' => 'Please select a country'));
return $this->output($list);
}
}
in the add.ctp:
<?php echo $this->CountryList->select('country');?>
Pretty simple stuff - on save it writes the acronym to the country field.
My issue is.. When pulling the data to view.ctp, how would I go about displaying the full country name as apposed to the acronym saved in the database without having to write the entire list down in view.ctp and matching the acronym to Country name there..
<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Country of Residence'); ?></dt>
<dd<?php if ($i++ % 2 == 0) echo $class;?>>
<?php echo $user['User']['country']; ?>
</dd>
Any and all help is very much appreciated!
Add a new function to the helper that returns the full name of the country.
class CountryListHelper extends FormHelper {
var $helpers = array('Form');
var $countryList = array(
'AF' => 'Afganistan',
'AL' => 'Albania',
'DZ' => 'Algeria',
.................
);
function select($fieldname) {
$list = $this->Form->input($fieldname , array(
'type' => 'select', 'label' => 'Country of Residence',
'options' => $this->countryList,
'empty' => 'Please select a country',
'error' => 'Please select a country'));
return $this->output($list);
}
function fullName( $abbr ) {
return $this->countryList[ $abbr ];
// + error checking
}
}
I have two models, Category and Point. The associations are defined as:
Category hasMany Point
Point belongsTo Category
I would like, when adding Points to my database, to be able to select the category it belongs to from a <select> box, along with the rest of the form data.
Where would I need to set the category list and how could I do it? And how would I produce the select box?
I assume it could be done with
$form->input('categorieslist',array('type'=>'select')); //categorieslist needs
//setting somewhere.
Also to generalize a bit:
In a View with access to the Form helper
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'key1' => 'val1',
'key2' => 'val2',
),
));
?>
The above will render a select input with two options. You can also place an empty option as the first item. Passing a value of true will simply append an empty option with a blank value to the beginning of the options rendered in the HTML.
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'key1' => 'val1',
'key2' => 'val2',
),
'empty' => true,
));
?>
You can pass a string to the 'empty' key to have it display custom text as the key field for the empty option.
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'California' => 'CA',
'Oregon' => 'OR',
),
'empty' => 'choose a state',
));
?>
One last example, you can also pre-select an option with the selected key. The value should match the value of one of the select options, not the key.
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'California' => 'CA',
'Oregon' => 'OR',
),
'empty' => 'choose a state',
'selected' => 'California',
));
?>
From the Model
Model->find( 'list', array( ... )); will always return an array formatted for use with select box options. If you pass data to your view stored in a variable with a lowercase plural model name, that is, ( $this->set( 'categories', $categories );, then you will automagically generate drop downs for related models by using the form helper in the view and passing it a data index of the same model name in singular form suffixed with "_id".
Aziz's answer at #2 is the example of that automagic kicking in.
CakePHP 1.3 Form Helper
CakePHP1.2 Form Helper
In the controller:
$categories = $this->Point->Category->find('list');
$this->set(compact('categories'));
In the view:
$form->input('category_id',array('type'=>'select'));