How to set place holder copy in date select menu in CakePHP - forms

I am using CakePHP 1.3 to create a select menu for date of birth (as below). I can set the default starting values as either blank or a selected date, but ideally I would like to have DD-MM-YYYY as the starting displayed values:
echo $form->input('dob',
array(
'before' => '',
'between' => '',
'after' => '',
'label' => false,
'divider' => false,
'monthNames' => false,
'selected' => false,
'empty' => true,
'dateFormat' => 'DMY',
'minYear' => date('Y') - 70,
'maxYear' => date('Y') - 16,
'error' => array('wrap' => 'div', 'class' => 'error-copy')
));
What I get:
What I would like:
Thank you

I believe if you want to do this you will have to make your own date fields and not use the FormHelper since you can only set a default date to it, what you want involves adding an element to the fields.
You could also try JQuery's datepicker out, it's what I use in my project, and since it's a text field you can just set whatever you want as a placeholder.

http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
echo $this->Form->dateTime('Contact.date', 'DMY', '12',
array(
'empty' => array(
'day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
)
)
);

I believe you can only set a default date on the date fields.
echo $this->Form->input('close_time', array('type' => 'time', 'selected' => '13:30:00'));
Cookbook entry

For the benefit of anyone Googling to find the answer to this question 1+ years after it was asked, we can now simply do the following to set default placeholder (temporary) values in CakePHP forms that are easily replaced when clicked:
<?php echo $this->Form->input('Email', array(
'placeholder'=>'Enter Your Email Here'
)); ?>
I like to include 'label' => false as well to make the forms really minimalist.

Related

Create a SelectBox using Form Helper in CakePHP3

I am trying to make a combo box for an edit page.
echo $this->Form->select('status',
['empty' => 'Select Status'],
['class' => 'form-control', 'required']
);
Here I want to add 2 things :
$options = array('0' => 'Inactive',
'1' => 'Active',
);
and selected value. suppose that is $status;
I tried with different options but sometime it do not add classes and sometime it shows options in tag
It will be great if somebody give clue.
Thanks
<?= $this->Form->input('status', [
'type' => 'select',
'options' => ['0' => __('Inactive') , '1' => __('Active')],
'empty' => __('Select Status'),
'class' => 'form-control',
'required' => true, 'label' => __('Type')
])
?>

zf2 Form Validator DateTime dateInvalidDate

Trying to use a DateTime Form element in ZF2 and cannot valid the form.
$inputFilter->add(array(
'name' => 'event_datetime',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 0,
'max' => 20,
),
),
),
));
Using this on the .phtml file.
<?php $formElement = $form->get('event_datetime');?>
<dt><?php echo $this->formLabel($formElement);?></dt>
<dd><?php echo $this->formDateTimeLocal($formElement);?>
<?php echo $this->formElementErrors($formElement);?>
NOTE: using formDateTimeLocal instead of formDateTime as the latter does not show the HTML5 elements.
Using Chrome the HTML5 DateTimeLocal field appears with a calendar and Time section.
When running $form->isValid() I receive: (var_dump($form->getMessages()))
array (size=1) 'event_datetime' => array (size=1) 'dateInvalidDate' => string 'The input does not appear to be a valid date' (length=44)
The getRequest->getPost() = public 'event_datetime' => string '2015-08-10T03:00' (length=16)
I've tried to split this field into 2: a Date and a Time field as separate variables. This works correctly for the Date BUT not for the Time element.
Reading around I've noticed this: ZF2 validating date and time format PT_BR always The input does not appear to be a valid date which does not help as I need the time component. (obviously I have looked at more than just 1 link but my rep on SO allows only 1 url in post.)
I've also read that Chrome and Opera cut off the 'seconds' part of the time field....
How to I validate either a \Zend\Form\Element\DateTime field or just the \Zend\Form\Element\Time for field...
I've tried to manually glue these together, add the :00 seconds part of the string to Time but to no effect.
If I set the input filter to 'required' => false I still receive the dateInvalidDate validator for for attempts: DateTime and Time...
So, the question is:
How do I validate a DateTime or Time field using Zf2 form elements and inputFilters. Following the Docs and example don't seem to work for me and manually creating the Time string also has the same issue.
Try this:
$inputFilter->add(array(
'type' => 'Zend\Form\Element\DateTimeLocal',
'name' => 'event_datetime',
'required' => true,
'options' => array(
'label' => 'Appointment Date',
'format' => 'Y-m-d\TH:i'
),
'filters' => array(
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 0,
'max' => 20,
),
),
),
));
You get the error, because the datetime string/format you pass is different than the expected datetime format by default. Try playing with 'format' => 'Y-m-d\TH:i' to get the result.
Taken directly from Zend documentation. It's all the same, but with a different element.
use Zend\Form\Element;
use Zend\Form\Form;
$time = new Element\Time('time');
$time
->setLabel('Time')
->setAttributes(array(
'min' => '00:00:00',
'max' => '23:59:59',
'step' => '60', // seconds; default step interval is 60 seconds
))
->setOptions(array(
'format' => 'H:i:s'
));
$form = new Form('my-form');
$form->add($time);
My original issue was validation. The suggestion by Stanimir did help and the dateTimeLocal format has great in pointing me in the right direction.
The whole issue was with the 'format' value.
My main problem was that when populating the \Zend\Form\Element\Time field the format was H:i:s but the HTML5 form only submitted H:i. (also due to my 'format' setting which is OK)
So, when populating the form the DB field returned H:i:s which populated the form correctly BUT on submission failed IF I didn't edit the Time field.
THEREFORE: the answer to this questions is basically make sure the format submitted [and $form->bind($object), $form->setData($post) etc] is EXACTLY the same as the form element definition [H:i != H:i:s] and when pulling from database format to correspond to your required setting.
var_dump($form->get('valid_to_time')->getFormat());
var_dump($form->get('valid_to_time')->getValue());
Once this is the same all will be well and you can split DateTime fields into individual Date and Time (or use DateTime as above).
Sounds simple but this was a headache to get right.

Symfony months in datepicker rendered as number

In my symfony app I had a date widget that rendered year/Month(full name)/day. I changed some settings in the formbuilder for ordering etc, but now I cant display the months as 3 letter names. How can I render them as Jan/Feb/...
->add(
'geboorteDatum', 'date', array(
'years' => range(date('Y') - 100, date('Y') - 5),
'empty_value' => array(
'year' => 'Jaar',
'month' => 'Maand',
'day' => 'Dag'
),
'required' => FALSE,
'format' => 'dMMy',
)
)
According to Date/Time Format Syntax, you should use triple M in order to format as short month:
'format' => 'dMMMy'
Hope this helps...

symfony2 form field label as an array

I'd like to have 3 separate texts for each field in my form as a label. They are separate, because they need to be styled differently. I tried this:
$builder->add('total_sales', 'text', array(
'label' => array('num' => '1', 'descr' => 'Total sales', 'category' => 'A'),
'required' => false,
'attr' => array(
'class' => 'field numeric_field',
'maxlength' => 10,
)));
Obviously the above don't work; it will display 'Array' in place of label.
How can I achieve desired effect?
first you'll need to create a custom form type that extends the text type, the reason for this is so you don't mess up other text types you might have elsewhere. After doing that you'll need to style it using a form_div_layout. you can see the details here:
http://symfony.com/doc/current/cookbook/form/form_customization.html

cakephp birthday helper customize the default values

I would like the to add "month", "day", "year" as the first choice in the birthday menu helper? Can this be done? I don't see any examples of how to do this? Below is the code I have:
<?php echo $this->Form->input('date_of_birth',
array(
'type' => 'date',
'label' => 'Date of Birth:<span>*</span>',
'dateFormat' => 'MDY',
'empty' => true,
'minYear' => date('Y')-130,
'maxYear' => date('Y'),
'options' => array('1','2')
)
);
?>
Thanks,
Bart
Here you go, the empty option accepts an array where you can specify the empty value for the individual fields using the keys month, year, day, hour, minute and meridian:
echo $this->Form->input('date_of_birth',
array(
'type' => 'date',
'label' => 'Date of Birth:<span>*</span>',
'dateFormat' => 'MDY',
'empty' => array(
'month' => 'Month',
'day' => 'Day',
'year' => 'Year'
),
'minYear' => date('Y')-130,
'maxYear' => date('Y'),
'options' => array('1','2')
)
);