I have the following code in my view:
$this->Form->input('born');
Which is a date field and I'm look to see if it is possible to have different empty text for each select box like: [Month |v][Day |v][Year |v].
Has anyone come across doing this? Much help is appreciated.
You can do something like this:
echo $this->Form->input('born', array( 'label' => 'Date of birth',
'dateFormat' => 'DMY',
'minYear' => date('Y') - 70,
'maxYear' => date('Y') - 18 ));
They will be dropdowns not empty text fields. You can read more about the form helper and automagic forms here:
http://book.cakephp.org/#!/view/1390/Automagic-Form-Elements
I have resorted to using jquery to update the cake empty date values
Cake:
echo $this->Form->input('born', array( 'label' => 'Date of birth',
'dateFormat' => 'DMY',
'minYear' => date('Y') - 70,
'maxYear' => date('Y') - 18,
'empty' => true
));
jQuery:
$(function() {
$('.input.date select[name$="[day]"] option[value=""]').html('-- Day --');
$('.input.date select[name$="[month]"] option[value=""]').html('-- Month --');
$('.input.date select[name$="[year]"] option[value=""]').html('-- Year --');
});
echo $this->Form->input('born', array( 'label' => 'Date of birth',
'type'=>'date',
'dateFormat'=> 'DMY',
'minYear' => date('Y') - 70,
'maxYear' => date('Y') - 18 ));
this works form me (CakePHP 2x)
echo $this->Form->input('born', array( 'label' => 'Date of birth',
'dateFormat' => 'DMY',
'minYear' => date('Y') - 70,
'maxYear' => date('Y') - 18,
'empty' => array(
'day' => '-- Day --', 'month' => '-- Month --', 'year' => '-- Year --',
)
));
If you can leave the input blank, try this:
echo $this->Form->input('born', array('empty' => true));
If not, check this answer: https://stackoverflow.com/a/11610483/1001673
It's a bit hacky but it'll do wha you want.
Related
I have Form input with multiple select options. I am unable to set default values. This is my code:
<?= $this->Form->input('PaymentMethods', array(
'type' => 'select',
'multiple' => true,
'label' => false,
'options' => array(
'cash'=>'cash',
'invoice'=>'invoice',
'ax'=>'ax',
'ca'=>'ca',
'vi'=>'vi',
'tp'=>'tp',
'dc'=>'dc'
),
'default'=>'ax'
)); ?>
How do I set default values for this input with PHP only?
This is working on my system. You can also set it from controller like this :
$this->request->data[$this->modelClass]['PaymentMethods'] = 'ax';
Please check these url also
CakePHP select default value in SELECT input
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
example :
$sizes = array('s' => 'Small', 'm' => 'Medium', 'l' => 'Large');
echo $this->Form->input(
'size',
array('options' => $sizes, 'default' => 'm')
);
Since this is multi-choice select, value given must be array. And the key shouldn't be default, I should've used value instead.
<?= $this->Form->input('PaymentMethods', array(
'type' => 'select',
'multiple' => true,
'label' => false,
'options' => $options,
'value'=> $array_of_data_fetched_from_database
)); ?>
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']
I'm looking for a way, to handle the Pagination-options in a dropdown menu.
I'd like my users to select the sorting order in the same form with my filtering options, so when they hit "Send", pagination-order is already set.orm
e.g. like:
$this->Form->input('paginationorder',array(
'label' => 'order',
'options' => array(
'sort:id/direction:desc' => 'New Items, desc',
'sort:id/direction:asc' => 'New Items,asc',
),
'div' => false
)
);
$(function() {
$('#sort-properties').change(function() { // replace the ID_OF_YOUR_SELECT_BOX with the id to your select box given by Cake
var price = $(this).val();
window.location = baseUrl + 'properties/property_view/'+price;
});
});
<?php
echo $this->Form->input('orderby', array(
'id' => 'sort-properties',
'options' => array(
'sort:sale_rent_price/direction:asc' => 'Sort by Price Low to High',
'sort:sale_rent_price/direction:desc' => 'Sort by Price High to Low',
'sort:created/direction:asc' => 'Sort by Date Old to New',
'sort:created/direction:desc' => 'Sort by Date New to Old'
),
'label' => false,
'empty' => 'Default Order'
));
?>
I would change the values of the options to e.g. fieldname.desc, like :
$this->Form->input('paginationorder',array(
'label' => 'order',
'options' => array(
'id.desc' => 'New Items, desc',
'id.asc' => 'New Items,asc',
),
'div' => false
)
);
Afterwards in the controller put the values to an array via the explode method:
$order = explode(".", $this->request->data['Model']['field']);
then you can use the values in your find condition, like:
$result = $this->Model->find('all', array(
'order' => array( $order[0] => $order[1] )
));
There is propably a more elegant way to make this, but this should work.
Cake 2.2.4
I have a database sport_year field set to the YEAR data type.
I've tried two approaches to creating a year menu for my add/edit views, but both have their own problems.
Using $this->Form->input:
echo $this->Form->input('sport_year', array(
'type' => 'date',
'dateFormat' => 'Y',
'name' => 'data[Sport][sport_year]',
'minYear' => date('Y') - 2,
'maxYear' => date('Y') + 1,
'label' => 'Year',
'empty' => '- select -'
));
The code works fine to add or edit data (as does the validation), but when arriving at the edit page existing year data is not properly selected in the form. The data array does show [sport_year] => 2012, but 2012 is not selected in the menu.
echo $this->Form->year(
'sport_year',
date('Y') - 2,
date('Y') + 1,
array(
'name' => 'data[Sport][sport_year]',
'label' => 'Year',
'empty' => '- select -'
)
);
The year helper seems to correctly retrieve and select the existing data, but the label does not work, and it doesn't correctly render the field as required even though my Model has validation set. It does still require the data, but it ignores my Model custom message and is falling back to a save error message in my controller.
$this->Form->year() will never show a label. Only $this->Form->input() adds labels as it is a wrapper method and year() is for creating a year field.
The code in cake is something like:
public function input() {
echo '<div>';
echo $this->label(...);
echo $this->year(...);
echo '</div>';
}
Taking this code, you do not need name as cake will be generating that already. If you are not in the Sport model you can use input('Sport.sport_year', ...)
If you read the docs you should see there is a default option for inputs. you can use that or make sure that $this->request->data['Sport']['sport_year'] is set to the year you want.
You could also set the value option.
echo $this->Form->input('sport_year', array(
'type' => 'date',
'dateFormat' => 'Y',
'minYear' => date('Y') - 2,
'maxYear' => date('Y') + 1,
'label' => 'Year',
'empty' => '- select -',
'default' => date('Y')
));
or
echo $this->Form->input('sport_year', array(
'type' => 'date',
'dateFormat' => 'Y',
'minYear' => date('Y') - 2,
'maxYear' => date('Y') + 1,
'label' => 'Year',
'empty' => '- select -',
'value' => date('Y')
));
For option 1 do this:
echo $this->Form->input('sport_year', array(
...
'selected' => 'data[Sport][sport_year]'
));
I'm struggling to find the answer to this. I have only been using CakePHP for a month and I've hit a problem. It's something I can fix by manually inserting the values but I expected my data to pre-populate. Here is what is happening:
The Model is Product which hasMany 'Dynamicprice'
I'm testing a product with the id of 7 (/products/edit/7).
the first part of my edit function is:
public function edit($id = null) {
$this->Product->id = $id;
if (!$this->Product->exists()) {
throw new NotFoundException('Invalid Product');
}
if ($this->request->is('get')){
$this->request->data = $this->Product->read();
}
debug($this->request->data);
//other stuff setting vars for drop-down lists
}
the
debug($this->request->data);
gives me the following:
array(
'Product' => array(
'id' => '7',
'category_id' => '70',
'name' => 'Full Test',
'description' => 'This is to test all features',
'price' => '0.00',
'aesthetic' => true,
'image' => '',
'price_structure' => '2',
'suggest_for' => '',
'created' => '2012-06-28 12:49:06',
'modified' => '2012-06-28 12:49:06'
),
'Dynamicprice' => array(
(int) 0 => array(
'id' => '15',
'product_id' => '7',
'drop' => '600',
'prices' => '6000:9.99, 12000:18.99 '
),
(int) 1 => array(
'id' => '16',
'product_id' => '7',
'drop' => '1200',
'prices' => '6000:19.99, 12000:28.99 '
),
(int) 2 => array(
'id' => '17',
'product_id' => '7',
'drop' => '2400',
'prices' => '6000:29.99, 12000:38.99 '
)
)
)
However, whilst everything in ['Product'] pre-populates the ['Dynamicprice'] array does not pre-populate the following:
<?php
echo $this->Form->input('Dynamicprices.0.id');
echo $this->Form->input('Dynamicprices.0.drop', array('label' => 'Drop 1 (mm). Enter "0" for "Any Drop"'));
echo $this->Form->input('Dynamicprices.0.prices', array('label' => 'Prices 1', 'type' => 'textarea', 'rel' => 'dynamic'));
?><hr><?php
echo $this->Form->input('Dynamicprices.1.id');
echo $this->Form->input('Dynamicprices.1.drop', array('label' => 'Drop 2 (mm).'));
echo $this->Form->input('Dynamicprices.1.prices', array('label' => 'Prices 2', 'type' => 'textarea', 'rel' => 'dynamic'));
?><hr><?php
echo $this->Form->input('Dynamicprices.1.id');
echo $this->Form->input('Dynamicprices.2.drop', array('label' => 'Drop 3 (mm).'));
echo $this->Form->input('Dynamicprices.2.prices', array('label' => 'Prices 3', 'type' => 'textarea', 'rel' => 'dynamic'));
?>
Am I right to expect them to populate automatically and if so what have I done wrong?
I have created /Model/Dynamicprice.php with the following just to make sure:
class Dynamicprice extends AppModel {
public $name = 'Dynamicprice';
public $belongsTo = 'Product';
But as I expected it didn't change anything.
This is the second time I've done this this week; ask a stupid question. Yes I was right to expect them to pre-populate. The problem is I got my naming conventions mixed up. "Dynamicprices.1.drop" shouldn't have the 's' at the end. Silly me!