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']
Related
I'm using form helper to input date select as follows
echo $this->Form->date('date_from', [
'empty' => [
'year' => 'Choose Year',
'month' => 'Choose Month',
'day' => 'Choose Date'
],
'label' => 'Date From'
]);
But this is only showing select field and not the label Date From
It looks like CakePHP3 form helper with date doesn't support label as parameter.
But this will generate exactly the same label as you want:
<?php
echo $this->Form->label('Date From');
echo $this->Form->date('date_from', [
'empty' => [
'year' => 'Choose Year',
'month' => 'Choose Month',
'day' => 'Choose Date'
],
]);
?>
See here: Creating label in CakePHP3 form helper.
You can add the label in your HTML code:
<div class="input date">
<label>My label</label>
<?php echo $this->Form->date('from_date'); ?>
</div>
The difference between the Date Form Control and Form Control is that the last one outputs the div wrapper and the label (among others).
I have this problem, I am using cake 2.8.x, my cakephp application is not saving the selected value of the date field. It kept saving "1970-01-01" even if you selected "2016-02-02" . What could be wrong?
This is my code in the controller
$this->request->data['Jobapp']['dayofbirth']= date('Y-m-d H:i:s', strtotime($this->request->data['Jobapp']['dayofbirth']));
$this->request->data['Jobapp']['dateofissue']= date('Y-m-d H:i:s', strtotime($this->request->data['Jobapp']['dateofissue']));
$this->request->data['Jobapp']['expirydate']= date('Y-m-d H:i:s', strtotime($this->request->data['Jobapp']['expirydate']));
and this is one of the field in the view file
<td><?php echo $this->Form->input('dayofbirth',array('label'=>'Date of Birth','type'=>'date','minYear'=>date('Y') - 90))?>
What could be the issue?
'dayofbirth' => array(
'day' => '12',
'month' => '08',
'year' => '1993'
),
'maritalstatus' => 'emtpy',
'nationality' => 'empty',
'complexion' => '',
'passportnumber' => '',
'dateofissue' => array(
'month' => '05',
'day' => '25',
'year' => '2016'
),
'placeofissue' => '',
'expirydate' => array(
'month' => '05',
'day' => '25',
'year' => '2016'
),
Your date fields are arrays. Try:
$this->request->data['Jobapp']['dayofbirth']= date('Y-m-d H:i:s', strtotime(implode('-',$this->request->data['Jobapp']['dayofbirth'])));
the same with other fields
You don't need to modify date field, CakePHP takes care of it. So You can get rid of these lines:
$this->request->data['Jobapp']['dayofbirth']= date('Y-m-d H:i:s', strtotime($this->request->data['Jobapp']['dayofbirth']));
$this->request->data['Jobapp']['dateofissue']= date('Y-m-d H:i:s', strtotime($this->request->data['Jobapp']['dateofissue']));
$this->request->data['Jobapp']['expirydate']= date('Y-m-d H:i:s', strtotime($this->request->data['Jobapp']['expirydate']));
You can take help of jquery ui, with which you can avoid date field modification withing controller.
In your view do the following changes,
<!-- Load the jquery & jquery ui library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script>
$(function(){
$(".datepicker").datepicker({dateFormat:"yyyy-mm-dd"});
});
</script>
<?php echo $this->Form->input('dayofbirth',['class'=>'datepicker']); ?>
<?php echo $this->Form->input('dateofissue',['class'=>'datepicker']); ?>
<?php echo $this->Form->input('expirydate',['class'=>'datepicker']); ?>
Please remove your controller code that processes input data. You should be then able to insert date in correct format.
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 have a cakephp 1.3 app which get's a person's consent to participate in a study. I'm trying to link inclusion criteria and exclusion criteria for the study on the Add study page. The HABTM relationship is setup and working(If there's only one way to select them) What I'm trying to do now is display the current Inclusion Criteria as a list of checkboxes the person adding the study can select, but I also want a field where new criteria can be entered as a comma seperated list. Right now it will save the checkboxes, but I want it to also add the text entered in the field as well. Screen showing the Add study as it sits now is below. I don't really know how I would go about doing that. Is there a way to do this or will I have to take the data entered, sort through it and then add it to $this->data[inclusion field data]? An example would be nice. :) I've added the code I have so far below the image.
Controller:
/**
* Add a study.
*/
function add() {
if (!empty($this->data)) {
//get the inclusions from the text data
//Sort through the comma seperated list and add it to $this->data['Study']['Inclusions']???
$this->Study->create();
if ($this->Study->save($this->data)) {
$this->Session->setFlash(__('The study has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The study could not be saved. Please, try again.', true));
}
}
$this->set('inclusions', $this->Study->Inclusion->find('list', array(
'fields' => array('id', 'name'),
'order' => 'Inclusion.name',
'recursive' => 0,
)));
$this->set('exclusions', $this->Study->Exclusion->find('list', array(
'fields' => array('id', 'name'),
'order' => 'Exclusion.name',
'recursive' => 0,
)));
$this->set('forms', $this->Study->Form->find('list', array('order' => 'Form.name','recursive' => 0,)));
}
View:
<div class="studies form">
<?php echo $this->Form->create('Study', array('type' => 'file'));?>
<fieldset>
<legend><?php __('Add Study'); ?></legend>
<?php
echo $this->Form->input('studyname', array('label'=>'Study Name','required' => true));
echo $this->Form->input('studynumber', array('label'=>'Study Number','required' => true));
echo $this->Form->input('file', array('label'=>'Select Electronic Consent','type' => 'file'));
echo $this->Form->input('consent_form_date');
?>
<fieldset class="inclusion">
<legend><?php __('Inclusions'); ?></legend>
<div class="IncExc">
<?php
echo $this->Form->input('Inclusion',array(
'label' => false,
'type' => 'select',
'multiple' => 'checkbox',
'options' => $inclusions,
'selected' => $html->value('Inclusion.Inclusion'),
));
?>
</div>
<?php
echo $form->input('Inclusion.inclusions',array(
'type' => 'text',
'label' => __('Add New Inclusions',true),
'after' => __('Seperate each new Inclusion with a comma. Eg: family, sports, icecream',true)
));
?>
</fieldset>
<fieldset>
<legend><?php __('Exclusions'); ?></legend>
<div class="IncExc">
<?php
echo $this->Form->input('Exclusion',array(
'label' => false,
'type' => 'select',
'multiple' => 'checkbox',
'options' => $exclusions,
'selected' => $html->value('Exclusion.Exclusion'),
));
?>
</div>
</fieldset>
<fieldset style="width: 700px;">
<legend><?php //__('Forms'); ?></legend>
<?php /*
echo $this->Form->input('Form',array(
'label' => false,
'type' => 'select',
'multiple' => 'checkbox',
'options' => $forms,
'selected' => $html->value('Form.Form'),
));
*/ ?>
</fieldset>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
You are trying to do pretty much the same as he wanted to do: Saving tags into a database table in CakePHP
What you have to do is:
Check if the manual inclusion field is empty or not. If the field is empty ignore it and go to step 5
If the field is not empty, split it at comma or whatever sign you want to use as a seperator. You can now validate it if you want to.
Use all the inclusions of step 3 and save them in your custom or real inclusion table. You should keep track of those custom entries in some way, so you don't mess them up with those you provided. Save all the ids generated while saving within an array.
Merge the collected ids of step 3 with those you got from the form which were given by you.
Save all collected and given data to database
The answere in the post given above including the code there can be used to do that. You should get along with it pretty well if you read the whole post + code comments ;)
If you have any further questions, just ask ;)
Greetings
func0der
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.