I am trying to get the unix timestamp but can't seems to get it.
I have form where the users need to select the date and time
$builder->add('time', 'datetime', array(
'input' => 'timestamp',
))
More info:
http://symfony.com/doc/current/reference/forms/types/datetime.html#input
But all i have is an array.. when i need an integet with unix timestamp
$time = $formData['time'];
print_r($time);
Output:
Array(
[date] => Array(
[year] => 2015
[month] => 6
[day] => 7
)
[time] => Array(
[hour] => 6
[minute] => 6
))
When I need something like 1443197171
I'm assuming you're looking at the POST data that comes back from the form which is different from the normalised data that the Symfony Forms component provides.
The visual representation is controlled by the widget option of the field, so in your case you probably want to set it to single_text like so:
$builder->add('time', 'datetime', array(
'input' => 'timestamp',
'widget' => 'single_text',
));
Even then you'll probably get back an array with separate date and time entries. What you want to do is get the "data" attribute from the form component, e.g.
$form->get('time')->getData();
This gets the field component and then retrieves the data. As the entry you linked to states:
The value that comes back from the form will also be normalized back into this format.
At this point it might be best to look into the model / view structure of the form components.
Related
I'm using Wintercms (fork of Octobercms) to create a backend application which needs to display some data according to a date range.
I've used the list filters to be able to select custom date range: https://wintercms.com/docs/backend/lists#list-filters
But when we land on the list, I would like to have a default date range selected.
The rule would be: "From 1st of february to 31st of october of the current year":
I haven't found any way of doing so in the documentations nor in internet examples..
Calculate dynamic defaults by adding scope definition in ListFilterExtendScopes event: https://wintercms.com/docs/backend/lists#extend-filter-scopes
Default for daterange scope is array of :afterDate and :beforeDate values:
$filter->addScopes([
'latest' => [
'label' => 'Latest',
'type' => 'daterange',
'conditions' => 'latest >= \':afterDate\' AND latest <= \':beforeDate\'',
'yearRange' => '20',
'default' => [
0: Carbon::now()->subDays(10),
1: Carbon::now()->addDays(10),
],
],
]);
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
)
);
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
I have created a field for publish date (with time), but I found only date formating. My time field is under my date field and it looks strange (for me), like this:
[DAY] [MONTH] [YEAR]
[HOURS]:[MINUTES]
How can i format it like this?
[DAY] [MONTH] [YEAR] - [HOURS]:[MINUTES]
There it is, what I now have in my Form builder:
$builder->add('published', 'datetime', array('label' => 'Date of publish', 'date_format' => 'ddMMMMyyyy', 'empty_value' => array('year' => 'Year', 'month' => 'Month', 'day' => 'Day'), 'invalid_message' => 'Date is not valid',))
PS: I'm using selection.
I have had the same problem once and solved it using the answer of this question:
Form theming datetime widget Symfony 2.
Also, find more info on the matter by browsing the following section of the Symfony2 official documentation.
How to customize Form Rendering.
I've looked at loads of forums about validation errors not showing and tried various things but to no avail...
Basically, the validation is correctly recognising the fields do not have values when they should, however the error messages don't 'automagically' appear below the input boxes.
Model validation rule is shown below:
var $validate = array(
'description' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Please enter a description of the change'
)
);
echo pr($this->data); output is shown below:
Array
(
[Change] => Array
(
[0] => Array
(
[id] => 3237
[cn_id] => 5132
[req_id] => 25
[description] =>
)
[1] => Array
(
[id] => 3238
[cn_id] => 5132
[req_id] => 22
[description] =>
)
[2] => Array
(
[id] => 3239
[cn_id] => 5132
[req_id] => 4
[description] =>
)
)
)
echo pr($this->Change->invalidFields()); output is shown below:
Array
(
[0] => Array
(
[description] => Please enter a description of the change
)
[1] => Array
(
[description] => Please enter a description of the change
)
[2] => Array
(
[description] => Please enter a description of the change
)
[description] => Please enter a description of the change
)
So, it is generating the errors messages for display, but they don't actually display in the view, and I don't know why?
Excerpt from the 'view' code is show below:
<?php echo $form->input('Change.'.$i.'.description',
array('value' => $cn['Change'][$i]['description'],
'label' => $engReq['Req']['description'])); ?>
Does anybody have ideas why the error messages are not showing?
I experienced the same issue with a hasMany model (where the form had numerically indexed fields) and came up with a validation solution that worked for me.
Quick answer: Before trying to actually save the data, I validated the data separately like (notice 'validate'=>'only'):
if($this->ModelName->saveAll($this->data, array('validate' => 'only'))) {
// proceed to save...
}
Doing it this way gave me the model's validation error message in the form, right under the input field that failed the validation (the normal Cake way of showing the validation error).
Note: I could not use saveAll() to actually save my data (I'll explain why in a minute). If I could use saveAll() to actually save the data, I could have gotten the validation at the same time as I saved by using (notice 'validate' => 'first'):
if($this->ModelName->saveAll($this->data, array('validate' => 'first')))
However, I could not use saveAll() to actually save the data, due to the fact that I needed to use a transaction to save several models at once, where some of the models were not directly related to other models. saveAll() will only save the model on which it is called, plus models directly related to it. Since Cake does not currently support nested transactions, and saveAll() uses one transaction automatically, I had to use save() on my models and start and end my transaction manually. However, this caused me to loose the validation message in my form on the hasMany items, even if I saved by using "$this->ModelName->save($this->data, array('validate'=>'first')".
Further explanation: The issue does seem to be related to using numerically indexed fields in the form. For example:
$this->Form->input("ModelName.0.field_name");
It seems this indexing scheme is the proper way to handle hasMany items in the form, but the validation messages would not find their way to this form input. It is interesting to notice that my view did in fact have access to the validation error. This can be seen in the view by using (notice no numerical index in these lines):
if($this->Form->isFieldError("ModelName.field_name")) {
echo $this->Form->error("ModelName.field_name");
}
Putting these lines after the '$this->Form->input("ModelName.0.field_name")' inserted a the validation message into the page, just not in the same div as the input field (and thus it didn't look ideal).
I couldn't figure out a way to tell Cake to use that validation message in the '$this->Form->input("ModelName.0.field_name")'. So I resorted to the 'validate' => 'only' method described earlier, which is working well for me.
shouldnt it be
var $validate = array(
'description' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Please enter a description of the change'
)
)
);
?