Question

I create custom module and would like to validate the input fields such date.

I tried in Form.php

public function _prepareForm()
{        
    $form = new Varien_Data_Form(array(
        'id'     => 'edit_form',
        'method' => 'post'
    ));

    $f = $form->addFieldset('User', array(
        'legend' => 'Add User',
        'class'  => 'fieldset-wide'
    ));

    $f->addField('to', 'date', array(
        'name'     => 'to',
        'label'    => 'Date to',
        'format'   => 'DD-MM-YYYY',
        'class'    => 'validate-date',
        'required' => true
    ));
}

But the class is not added, and nothing does it.

What am I doing wrong?

Was it helpful?

Solution 3

Ok, this is my working code:

public function _prepareForm()
{
    $form = new Varien_Data_Form(array(
        'id'     => 'edit_form',
        'method' => 'post'
    ));

    $f = $form->addFieldset('User', array(
        'legend' => 'Add User',
        'class'  => 'fieldset-wide'
    ));

    $f->addField('from', 'date', array(
        'label' => 'From',
        'after_element_html' => '<small>Comments</small>',
        'image' => $this->getSkinUrl('images/grid-cal.gif'),
        'format' => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
        'class' => 'validate-date')
    );
}

OTHER TIPS

Try validate_class, not class

$f->addField('to', 'date', array(
    'name'     => 'to',
    'label'    => 'Date to',
    'format'   => 'DD-MM-YYYY',
    'validate_class'    => 'validate-date',
    'required' => true
));

look at Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Related as an example.

Actually I'm not sure what you question is exactly about. I guess you are trying to assign the class validate-date to your date "to" field?

My suggestion to test and/or solve your problem: Try adding just a plain text-field and add validation to the form. Does this work?

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top