2

I am using below code to add date field in one of the admin forms.

$fieldset->addField('newsletter_date', 'date', array(
    'label'     => Mage::helper('masters')->__('Send At'),
    'class'     => 'required-entry',
    'required'  => true,
    'name'      => 'newsletter_date',
    'format' => 'MM/dd/yyyy HH:mm',
    'image' => $this->getSkinUrl('images/grid-cal.gif'),
    'time' =>   'true'
));

How I can disable past dates from newsletter_date field?

2

You can't out of the box. But you can implement your own validation based on Magento's js/prototype/validation.js.

Inchoo has a good blogpost about this.

After Magentos validation.js is added you can just add your own and then use the CSS class to validate the field right before the form submit.

Validation.add('validate-email', 'Please enter a valid Gmail address. For example johndoe@gmail.com.', function(v) {
    return Validation.get('IsEmpty').test(v) || /^([a-zA-Z0-9]+[a-zA-Z0-9._%-]*@gmail\.com)$/i.test(v)
})

Example is copied from the inchoo article, I'm not good with JS, so I'll let the implementation of $date > now() up to you :-)

1

Using below method one can disable past dates from Magento admin form

            $datefield = $fieldset->addField('custom_date', 'date', array(
                    'label'        => Mage::helper('order')->__('Date'),
                    'name'         => 'custom_date',
                    'time'         => true,
                    'image'        => $this->getSkinUrl('images/grid-cal.gif'),
                    "class"        => "required-entry validate-date",
                    "required"     => true,
                    'format'       => $dateFormatIso
                )); 

            $datefield->setAfterElementHtml("<script type=\"text/javascript\">
            //<![CDATA[
                Calendar.setup({
                    inputField: 'custom_date',
                    ifFormat: '%m/%e/%Y',
                    showsTime: false,
                    button: 'custom_date_trig',
                    align: 'Bl',
                    singleClick : true,
                disableFunc: function(date) {
                    var now= new Date();
                    if(date.getFullYear()<now.getFullYear()){ return true; }
                    if(date.getFullYear()==now.getFullYear()){ if(date.getMonth()<now.getMonth()){ return true; } }
                    if(date.getMonth()== now.getMonth()){ if(date.getDate()<now.getDate()){ return true; } }
                    return date.getDay() === 0; // Disabled Sunday
                },
                });
            //]]>
            </script>");
0

Maybe someone will help this. This way i was able to add the datepicker to my field and disabled whatever days I wanted

$form->setHtmlIdPrefix('yourid');
$form = new Varien_Data_Form();
.....
$fieldset->addField('newsletter_date', 'text', [
    'label'     => Mage::helper('masters')->__('Send At'),
    'class'     => 'required-entry',
    'required'  => true,
    'name'      => 'newsletter_date',
]);

and add this after:

        $form->getElement('newsletter_date')->setAfterElementHtml("<script type='text/javascript'>
        //<![CDATA[
                 Calendar.setup({
                    inputField : 'yourid_newsletter_date',
                    ifFormat : '%Y-%m-%d',
                    button : 'call_date',
                    align : 'Bl',
                    singleClick : true,
                    disableFunc: function(date)  {
                        var now = new Date();

                        if(date.getFullYear() < now.getFullYear()) { return true; }
                        if(date.getFullYear() == now.getFullYear() && date.getMonth() < now.getMonth()) { return true; }
                        if(date.getMonth() == now.getMonth() && date.getDate() < now.getDate()) { return true; }
                    }
                  });
        //]]>
       </script>");
0
$fieldset->addField('newsletter_date', 'date', array(
'label'     => Mage::helper('masters')->__('Send At'),
'class'     => 'required-entry',
'required'  => true,
'name'      => 'newsletter_date',
'format' => 'MM/dd/yyyy HH:mm',
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'time' =>   'true',
disableFunc: function(date) {
//disable previous day in datepicker
var now= new Date();
if(date.getFullYear()<now.getFullYear())
{
return true;
}
if(date.getFullYear()==now.getFullYear())
{
if(date.getMonth()<now.getMonth())
{
return true;
}
}
if(date.getMonth()==now.getMonth())
{
if(date.getDate()<now.getDate())
{
return true;
}
}
}

));

just try this code i'm not sure if it works for you but it works for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.