3

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?

  • Now i have answer, but not have a privileges to answer my question. Ok, this is working code: code $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' ));` – MiMo Feb 3 '13 at 1:21
2

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.

  • waw. If extends from Mage_Adminhtml_Block_Widget_Form it's only "class" if extends from Mage_Adminhtml_Block_Widget_Grid it's "validate_class". Thank you for directions. – MiMo Feb 3 '13 at 0:48
1

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?

0

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')
    );
}

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.