Magento – Magento admin form validation doesn’t working

adminformform-validationmagento-1.9

I have a custom grid in Admin panel. When you want to edit entries to the grid it will load a form. The required field validations of this form are not working. I have define the fields as below.

$fieldset->addField('discount', 'text', array(
                'label'    => $helper->__('Discount Amount (%)'),
                'required' => true,
                'class'    => 'required-entry',
                'name'     => 'discount',
                'index'    => 'discount',
        ));

Adding below two attributes not working. It saves even if the fields are empty.

'required' => true,
'class'    => 'required-entry',

Am I missing something else here?

Best Answer

The form should be instantiated as a Varien_Form for validation to kick in. Find the ID of the form you are missing validation on, and add a little piece of Javascript to instantiate it as a Varien_Form:

<script type="text/javascript">
    //< ![CDATA[
        var myForm = new varienForm('myFormId');
    //]]>
</script>