Magento Forum

   
Admin Date input field
 
angelo_f
Sr. Member
 
Total Posts:  80
Joined:  2011-10-28
 

Greetings

I am trying to create a date filed for a custom admin setting

I can see that you can create a time input field like this

<time translate="label comment">
<label>Time</label>
<frontend_type>time</frontend_type>
<sort_order>52</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</time>

How do I create a date type one (where I can pick a date).

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Hello (again) Angelo.
Apparently you can’t add a date element to the configuration form.
If you try it like this:

<date translate="label comment"
<
label>Date</label
<
frontend_type>Date</frontend_type
<
sort_order>100</sort_order
<
show_in_default>1</show_in_default
<
show_in_website>1</show_in_website
<
show_in_store>1</show_in_store
</
date>
You get this exception:
Output format is not specifiedPleasespecify "format" key in constructor, or set it using setFormat().
This means that the date is missing the format. Unfortunately you cannot set the format by adding the <format> tag in the field configuration. That field is ignored.

Here is how I did it.
I made a copy of the file lib/Varien/Data/Form/Element/Date.php and placed it in app/code/local/Varien/Data/Form/Element/Date.php.
(I don’t know any other method to override a lib class).
Now my new class will be used instead of the one in lib.
Edit this new file and add this method:

function getFormat(){
    
if (!$this->getData('format')){
        $this
->setData('format''%Y-%m-%d');
    
}
    
return $this->getData('format');
}
This will ensure the format is never empty.
This will make the date format work only in theory.
There is still an issue with the calendar image that doesn’t appear.
I didn’t want to waste time on this so I’ve changed the trigger to the field itself.
In the same file look for the calendar setup section and change the trigger:
Change this:
button"%s_trig",
to this
[code
button: “%s”,
]

Now the calendar will appear when you click on the field.

Cheers,
Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
angelo_f
Sr. Member
 
Total Posts:  80
Joined:  2011-10-28
 

Fantastic - works perfectly exactly the way I wanted it.

 
Magento Community Magento Community
Magento Community
Magento Community
 
davidseloo
Jr. Member
 
Total Posts:  2
Joined:  2011-12-28
 

thanks for you contibution arabgento, very usefull for me
smile

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top