Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we make Widget Options validaton in magento?

If we add back end configurations in file system.xml, we can add validation to every field using validate tag like the following:

<duration>
   <label> ...</label>
   <frontend_type >text</frontend_type>
   <validate>required-entry validate-number</validate>
   <source_model>adminhtml/system_config_source_yesno</source_model>
   <sort_order>70</sort_order>
   <show_in_default>1</show_in_default>
   <show_in_website>1</show_in_website>
   <show_in_store>1</show_in_store>
</duration>

My question is the there similar way to make the same to widget options in magento? For example like this in widget.xml file:

<widget type="common/widget">
        <name>new widget</name>
        <description>new widget</description>
        <parameters>
            <template>
                <visible>0</visible>
                <value>template.phtml</value>
            </template>
            <after translate="label">
                <visible>1</visible>
                <label>...</label>
                <type>text</type>
                <validate>required-entry validate-number</validate>
            </after>
</parameters>
</widget>
like image 922
rramiii Avatar asked Nov 01 '22 18:11

rramiii


1 Answers

You cannot do this directly via XML. However, you can create a custom block, set the validation css class there, and use it as the type in widget.xml.

app/code/local/Mynamespace/Mymodule/etc/widget.xml:

<somefield>
    <required>1</required>
    <visible>1</visible>
    <label>Some number</label>
    <type>mynamespace_mymodule/element_numeric</type>
</somefield>

app/code/local/Mynamespace/Mymodule/Block/Element/Numeric.php:

class Mynamespace_Mymodule_Block_Element_Numeric extends Mage_Adminhtml_Block_Widget_Form_Renderer_Fieldset_Element
{
    public function render(Varien_Data_Form_Element_Abstract $element)
    {
        $element->setType('text');
        $element->addClass('validate-digits');
        parent::render($element);
    }
}

It is important that the custom element block extend the class Mage_Adminhtml_Block_Widget_Form_Renderer_Fieldset_Element so that it will display properly in the fieldset.

like image 119
Joe Mizzi Avatar answered Nov 09 '22 05:11

Joe Mizzi