joomlamoduleserver-side-validation

How to add form validation to a joomla module


I'd like to know if there's a way to add custom form validation to a module in joomla 3.7. I want my customer to be able to fill in two date fields [start] and [end] in the module administration. But I can't find a proper way to check something like "both fields are optional but if one field is filled in the other one must be a valid date too". I know I can do this with a Joomla component, but I'm coding a module here.

I've also read about coding and copying custom validation rule files to the administrator folder, but this feels a bit "dirty" to me because a module should work right after installation and be update-safe furthermore.

So my question is: Does anyone know a "proper" way to achieve this?


Solution

  • For server sided validation you can use a custom validation rule.

    Joomla's documentation tutorial show it for a component but you can modify it for use with modules, without losing any future update ability:

    <config>
        <fields name="params">
            <fieldset name="basic" addrulepath="modules/mod_module/rules">
                <field name="field" rule="dualdate" />
            </fieldset>
        </fields>
    </config>
    

    Then you would create your custom rule class in the module's rule folder:

    class JFormRuleDualDate extends JFormRule
    {
    
    }