Symfony 3 DateType year format - php

Hi I've got a problem with year format on DateType, let take a look on this image
I want to change year from AD convert to BE in Thailand format
So It's will look like this AD = 2017 convert to BE = 2560
Is anyone know how to change this thing
and this is my form type code.
->add('birthday', DateType::class, [
'required' => false,
'widget' => 'single_text',
'label' => 'Birthdate',
])

You'll likely want to use an external plugin to manage this as the Symfony default won't handle conversion of "base year."
Something like this will be where I'd look: https://github.com/jojosati/bootstrap-datepicker-thai/blob/thai/README-thai.md
And then in your builder object, I'd add a class to select the thai datepicker elements:
$builder->add('birthday', 'text', array(
'attr' => array(
'class' => 'thai-datepicker') //for example
)
);

Related

Change order of Zend Framework 2 DateSelect drop downs

I have a form element in a ZF2 application that uses DateSelect to allow a user to enter their date of birth. Currently the fields are shown in the order d-m-y. I would like to reverse this order so it is displayed as y-m-d. I have come across posts on SO that recommend changing the locale in PHP to change the order but that is not an option for me. I have also tried
$this->add(array(
'type' => 'Zend\Form\Element\DateSelect',
'name' => 'dob',
'options' => array(
'label' => 'Date of Birth',
'create_empty_option' => true,
'pattern' => $this->options['isMobile'] ? 'd MMM y' : 'd MMM y',
'empty_options' => array(
'day' => 'DD',
'month' => 'MM',
'year' => 'YYYY',
),
'allowLabelHTML' => TRUE,
'required' => true,
)
));
$this->get('dob')->setFormat('Y-m-d');
Which was an accepted answer to another SO question but that produces an internal server error for me. I would be surprised if this is not possible, maybe using an helper file but I cannot find anything on the web to suggest how, apart from the above and changing the locale. Can anyone help with this?
You get fatal error because method setFormat() does not exist in Zend\Form\Element\DateSelect.
I don't think it is possible to achieve this without writing own view helper.
Zend\Form\Element\DateSelect is based on locale settings, so you can pass locale short code as parameter to view helper, so order of elements will be proper for provided region.
This view helper takes 3 parameters $this->formDateSelect($element, $intlFormat, $locale), so you use it like this:
echo $this->formDateSelect($form->get('dob'), \IntlDateFormatter::LONG, 'en_Gb');
or...
echo $this->formDateSelect()->setLocale('en_Gb')->render($form->get('dob'));
or... you can change locale settings in your php.ini file
intl.default_locale = en_Gb

range Symfony form input

I'm looking for something to make this kind of input in Symfony:
I don't know which kind of input and how to deal with the Symfony controller.
$form = $this->createFormBuilder($newLot)
->add('lotType',null,['required' => false])
->add('lotGenre',null,['required' => false])
->add('ville',null)
->add('quartier',null)
->add('prix',null,['required' => false])
->add('prix', null,['required' => false])
->add('superficie', RangeType::class,['required' => false])
->add('createdAt', RangeType::class,['required' => false])
->add('gestionnaire',null)
->add('lotStatut',null)
->getForm();
Here is a part of my controller but not sure it's useful.
If you want to represent a start date linked to an end date, I'd advise you to create two DateType fields, like this:
->add('startDate', DateType::class, [some_parameters])
->add('endDate', DateType::class, [some_parameters])
Then, you design your two fields in your template, by adding the "à" cell between them.
The RangeType is not appropriate to render dates, it renders this kind of input.

Symfony DateTime before 1902

I currently code a symfony project that requires saving many dates below 1091. In MySQL, DateTime recording is good. But when I refreshed the page, the value displayed is wrong. I use a datetime field including the following code:
$builder->add(
'datetimepublish',
DateTimeType::class,
[
'model_timezone' => date_default_timezone_get(),
'view_timezone' => date_default_timezone_get(),
'widget' => 'single_text',
'format' => 'dd/MM/yyyy HH:mm',
'label' => 'Date et heure',
'attr' => [
'placeholder' => 'Date et heure',
],
]
);
This behavior would be a 'normal', I searched a bit in the vendor, and I think I find when it did that, the DataTransformer. How to cure it?
I have the same issue here and discovered that it's most likely you work on a 32-bit system and symfony somewhere uses a timestamp representation of the date, which php on 32-bit systems can't handle if the year is one before 1902.
It's amazing.
#see:
https://github.com/symfony/symfony/issues/5227

Silex : how to set the value of a datetime input in the widget

I built a form with a datetime input on Silex, using the FormBuilderInterface. The validation works perfectly, but when the datas are loaded to rebuild the form and fill in the inputs' values, the datetime's stays empty. I've been doing some researches but nothing so far so here I am.
The form is built in a class called EventType and fills an Event object. The $dateOfEvent property's setter asks for a \DateTime object and the getter returns $dateOfEvent without formatting it.
Here is what the EventType's builder's date of event part looks like :
->add('dateOfEvent', 'datetime', array(
'label' => 'Date of the event',
'widget' => 'single_text',
'input' => 'datetime',
))
If the Event object is already filled, all the inputs' values of the form are filled, except for the file input (which is normal) and the datetime input (which is not normal).
Here is a screen of what it looks like.
you should fix the name of the form field, it should match the property's:
->add('event', 'datetime', array(
'label' => 'Date of the event',
'widget' => 'single_text',
'input' => 'datetime',
))

Can't set date format for Zend Framework 2 Date form element?

I'm just trying to make a Date element in a Form, but the date format is always "mm/dd/yyyy" no matter how I try to change it. I tried setting it up like this:
$this->add(array(
'name' => 'ct-start_date',
'options' => array(
'label' => 'Start Date',
'format'=>'Y-m-d'
),
'attributes' => array(
'class' => 'ct-date',
),
'type' => 'Zend\Form\Element\Date',
));
And displaying like this:
echo $this->formRow($form->get('ct-start_date'));
But it still comes out as "mm/dd/yyyy" even though when I do:
print_r($form->get('ct-start_date')->getFormat());
that comes out as "Y-m-d"! So I added this:
$this->get('ct-start_date')->setOptions(array('format'=>'Y-m-d'));
No change. Then I tried this:
$date = new Element\Date('ct-start_date');
$date
->setLabel('Start Date')
->setAttributes(array(
'class' => 'ct-date',
))
->setOptions(array(
'format' => 'Y-m-d'
));
$this->add($date);
Same thing! That's in Chrome. In Firefox it's just an empty text input. Maybe I should just fall back on jQuery.
The format option in the Date element doesn't affect the input or output format of dates within the element.
It is used by the element's getInputSpecification() method to determine the date format to be used with the validator.
Example:
$this->add(array(
'name' => 'ct-start_date',
'options' => array(
'label' => 'Start Date',
'format'=>'Y-m-d'
),
'attributes' => array(
'class' => 'ct-date',
),
'type' => 'Zend\Form\Element\Date',
));
//...
public function getInputFilter()
{
$filter = new InputFilter()
// add default filters and validators for the date element
// the date validator will automatically be set up to accept dates
// formatted according to your elemen's "format" option
$filter->add($this->get('ct-start-date')->getInputSpecification());
../
return $filter;
}
If you are going to populate the form element with a date value from the server side, it needs to be in the appropriate format (e.g. $element->setValue(date('Y-m-d', $date));)
On the client side, they will either need to type it in the appropriate format or the validator will return an error, or make sure whatever client side date input you are using puts it in the Y-m-d format.

Categories