0

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...

3

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.

  • Hi @Sander...i check it out your answer,It not works for me... and also I update my question.Can you please guide me.. – VIVEK-MDU Oct 1 '13 at 12:23
  • I'll check it later on, don't have the time right now so hang in there :) – Sander Mangel Oct 1 '13 at 12:26
  • Thank you @Sander...I am waiting for your valuable answer.. – VIVEK-MDU Oct 1 '13 at 12:28
  • I did a quick check myself on an existing extension and adding the class and required=true values as described in my answer worked. Can you make sure both values are set correctly? – Sander Mangel Oct 1 '13 at 13:52
  • 1
    just saying it's required will perform basic validation (i.e. is the value present), however for an image you need a bit more validation that's what the required-file class is for. If this isn't working I think that there is a bigger issue here which I'm not able to spot at the moment. I suggest contacting the extension developer on this. – Sander Mangel Oct 1 '13 at 14:13

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.