Magento 1.7 – How to Validate Image in Admin Custom Form

adminjavascriptmagento-1.7validation

I create custom form in admin panel in magento.Here I tried in many way to validated image tags in admin panel.

/app/code/community/company_name/module_name/Block/Adminhtml/module_name/Edit/Tab/image.php

my Image tag containing code :

protected function _prepareForm()
    {
        /**
         * Checking if user have permissions to save information
         */
        if (Mage::helper('magentostudy_news/admin')->isActionAllowed('save')) {
            $isElementDisabled = false;
        } else {
            $isElementDisabled = true;
        }

        $form = new Varien_Data_Form();

        $form->setHtmlIdPrefix('news_image_');

        $model = Mage::helper('magentostudy_news')->getNewsItemInstance();


        $fieldset = $form->addFieldset('image_fieldset', array(
            'legend'    => Mage::helper('magentostudy_news')->__('Pattern Image Thumbnail'), 'class' => 'fieldset-wide'
        ));

        $this->_addElementTypes($fieldset);

        $fieldset->addField('image', 'image', array(
            'name'      => 'image',
            'class'     => 'required-entry',
            'label'     => Mage::helper('magentostudy_news')->__('Pattern Image'),
            'title'     => Mage::helper('magentostudy_news')->__('Pattern Image'),
            'required'  => true,
            'disabled'  => $isElementDisabled
        ));

        Mage::dispatchEvent('adminhtml_news_edit_tab_image_prepare_form', array('form' => $form));

        $form->setValues($model->getData());
        $this->setForm($form);

        return parent::_prepareForm();
    }

Here I put class required value and also gives 'required' => true, in my form.

Updated:

Here, same functionality can be applied in text box, it working fine ..

Here path location for text box.

/app/code/community/company_name/module_name/Block/Adminhtml/module_name/Edit/Tab/Main.php

and also working fine in this coding….

protected function _prepareForm()
    {
        $model = Mage::helper('magentostudy_news')->getNewsItemInstance();

        /**
         * Checking if user have permissions to save information
         */
        if (Mage::helper('magentostudy_news/admin')->isActionAllowed('save')) {
            $isElementDisabled = false;
        } else {
            $isElementDisabled = true;
        }

        $form = new Varien_Data_Form();

        $form->setHtmlIdPrefix('news_main_');

        $fieldset = $form->addFieldset('base_fieldset', array(
            'legend' => Mage::helper('magentostudy_news')->__('Pattern Item Info')
        ));

        if ($model->getId()) {
            $fieldset->addField('news_id', 'hidden', array(
                'name' => 'news_id',
            ));
        }

        $fieldset->addField('title', 'text', array(
            'name'     => 'title',
            'label'    => Mage::helper('magentostudy_news')->__('Pattern Title'),
            'title'    => Mage::helper('magentostudy_news')->__('Pattern Title'),
            'required' => true,
            'disabled' => $isElementDisabled
        ));

        $fieldset->addField('author', 'text', array(
            'name'     => 'author',
            'label'    => Mage::helper('magentostudy_news')->__('Pattern Model Id'),
            'title'    => Mage::helper('magentostudy_news')->__('Pattern Model Id'),
            'required' => true,
            'disabled' => $isElementDisabled
        ));
 [...............]

and also I attached my text box validation.
Sample validation working function

Same functionality is implement in file type, but not working in file type. I just putting class =>'required-entry',it not working well,Please guide me…

Best Answer

For files you can use the class required-file. So add that to the class index of the addField array

[...]
$fieldset->addField('image', 'image', array(
    'name'      => 'image',
    'class'     => 'required-entry required-file',
    'label'     => Mage::helper('magentostudy_news')->__('Pattern Image'),
    'title'     => Mage::helper('magentostudy_news')->__('Pattern Image'),
    'required'  => true,
    'disabled'  => $isElementDisabled
));
[...]

Checkout this article by Inchoo on validations to learn more about the different types

[EDIT]

I recommend also doing some checks in the controller that handles the post to check for the appropriate file types etc. Javascript validation is by no means bulletproof.

Related Topic