Edit a datetime value in Symfony - forms

I want to save a datetime value, but when I try to render the form, it does not display a date/time picker, just an empty field. If I only use a DateType class, it displays the picker, but not with a DateTimeType. Here's the form:
->add('datetimestart',
DateTimeType::class,
array(
'widget'=> 'single_text',
"attr" => array
('class' => 'form-control col-7')))
Is there a way to show a datetime picker? Or do I have to create a form field for date and another one for time?

You can use an html datepicker more info here
Actually I'm not sure if the Symfomy form engine supports this type natively, but you should be able to create a custom one extending the standard Input class.
Or if you are using a js datepicker, you should trigger that code on the twig template in which you are rendering the form.

I ended up separating both fields: One for date and another one for time. Using the HTML datepicker mentioned by Carlos is better way to do that, but I don't have enough time

Related

CakePHP 4 Date format for FormHelper->date() how to change to UK format

I have the following code in a template:
<?= $this->Form->date('selected_date', ['required' => true]) ?>
This displays a lovely new dynamic date picker, but with the US format "mm/dd/yyyy". What I want is "dd/mm/YYYY"
In app.php I've set the APP_DEFAULT_LOCALE to en-GB.
In AppController.php I've set the following:
I18n::setLocale('en-GB');
Time::setDefaultLocale('en-GB'); // For any mutable DateTime
FrozenTime::setDefaultLocale('en-GB'); // For any immutable DateTime
Date::setDefaultLocale('en-GB'); // For any mutable Date
FrozenDate::setDefaultLocale('en-GB'); // For any immutable Date
How do I change the format? I can't find anything in the docs or online.
You can't change it, at least there's no reliable, cross browser/device compatible way, the control is rendered by the browser, and the current state of things is that browsers use the locale the browser currently runs in to format the control.
If you want something solid, then you'll have to use a custom JavaScript datepicker. If you want to walk on the edge, look into web components.
See also Is there any way to change input type="date" format?.

Use localized date format with date picker in cakephp 3

I'm building a cakephp 3 app (cakephp v3.3.2 that is) and I have a problem getting dates displayed in the chosen locale (de_DE).
The database field in my example is invoice_date, which is of type DATE (stored in a mysql DB).
I want to use a date picker in the web page ("zebra_datepicker"), so in the view I use a single text entry field for the date instead of the three drop-downs that are displayed by default:
echo $this->Form->input('invoice_date', [ 'type' => 'text' ]);
To get the date displayed in the German format dd.mm.YY I changed the global locale setting to
ini_set('intl.default_locale', 'de_DE');
in config/bootstrap.php.
For testing, I created a new record in the database and set the invoice_date to 2016-09-02 (Y-M-D).
If I call the edit action of my controller now, I get a default value of 02.09.16 in the form, which is correct. But if I submit the form back, the database value of the field is changed into 2002-09-16!
Do I need some extra code in the controller to change the date format back into its original form? Is this a bug in the localization code of cakephp or did I miss something? Note, that my controller just uses patchEntity to process the posted data.
I already tried adding date('invoice_date', 'dmy') in the validationDefault method of the table object, but this didn't change anything:
$validator
->date('invoice_date', 'dmy')
->requirePresence('invoice_date', 'create')
->notEmpty('invoice_date');
Thanks for your help!
I just found the solution to my problem: in bootstrap.php, I had to add:
Type::build('date')->useLocaleParser();
Type::build('datetime')->useLocaleParser();
With that, I can enter dates like 31.12.16 (d.m.y) and the date gets correctly saved in the database. This works with the simple validator:
$validator
->date('invoice_date')
->notEmpty('invoice_date');
To also support dates like 31.12.2016 (d.m.Y), I had to add an extra parameter to the date validator, like this:
$validator
->date('invoice_date', 'dmy')
->notEmpty('invoice_date');

Symfony 1.4 - are forms always in <table>?

I'm using sfDoctrineAllowPlugin. I need form which has Twitter's Bootstrap semantics. I looked into template and I found, that there is only $form, which is a <table>. How can I format it in my way? I don't want to use table, rows and cols.
There are plenty of render* functions available to display each item in your form.
renderRow
renderLabel
render
renderError
etc ...
But you can also define a decorator (a custom sfWidgetFormSchemaFormatter) for your form to define the way each item will be display. Check this example of adding * for each required field.

How to add a custom field button to a field in SugarCRM

I looked through the developer blogs and I can only find ways to create a custom global form button or to replace a field with custom code. I don't want to replace the field, I want to add a button beside a field.
Here's what I have in mind:
[Date Field][date_picker_button] [time field][time_is_now]
The button I want to add is the time_is_now, allowing the user to populate the time field with the current time.
I have tried updating custom/modules/Notes/metadata/quickcreatedefs.php with:
array (
'name' => 'billable_start_c',
'label' => 'LBL_BILLABLE_START',
'customCode' => '<button</button>',
)
But this replaces the input field with the custom code. I could just put in the stock code and then my custom code, but date fields use yui and a widget, so I'd prefer not to.
Is there anyway to do something like:
'customCode' => '{default} <button></button>',
so that the default code gets output and then the custom code?
You could create a new SugarField template, which will essentially allow you to create the markup for your field. If you named your SugarField TimeIsNow, you could create a field with that type (instead of text,int, date or whatever). You should take a look at some of the other SugarFields to see how they are formatted, but it's really just HTML. When the field is set to that type, it pulls in that HTML template. You can tie your JS to the onclick function. Sugar is very easy to customize, so I definitely wouldn't get in the habit of solving all your customization needs this way...It's another way to look at it though.
Don't use custom code if you can help it. Either copy over the tpl to custom/modules/[yourmodule]/tpls/[yourtemplate] and code the bitton in there or use an after_ui_frame hook and some js to insert you custom button into the DOM.

How do I get a regular Checkbox in a Zend Form?

I have a form in Zend_Form that needs some checkboxes and I'd like them to be regular old checkboxes. You know, you give em a name and a value. If they are checked your post data contains name=>value.
Zend_Form is generating two inputs fields. One, the checkbox with a value=1 and the second a hidden input with a value=2. Both have the same name. I understand in theory how Zend expects the checkbox to work, but that's not how I expect it to work and it's not how I want it to work. How do I get my old fashion HTML checkbox back?
I have tried using $this->createElement, $this->addElement and creating a Zend_Form_Element_Checkbox manually. None allow me to set the checkbox's value and all generate the hidden input.
The final and REALLY correct answer is to add an option to the element :
$this->addElement('checkbox', 'my_element', array(
'label' => 'My Element Label',
'name' => 'my_element_name',
'disableHidden' => true
));
Zend_Form_Element_MultiCheckbox is what you're looking for.
The standard Checkbox element is meant to represent "yes/no" scenarios.
You could extend Zend library and add your own custom form element to render it just like you expect it. I did it for having a date field and it worked just fine.
I wonder why that does not work for you. You can set the values to anything you want (setCheckedValue() and setUncheckedValue()). So the only difference to normal checkbox is
if (null == $this->_getParam('checkbox', null)) {
//vs.
if ($unchecked == $this->_getParam('checkbox')) {
What exactly are you trying to do?