0

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?

Rico
  • 398
  • 2
  • 6

1 Answers1

2

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
{

}
Paul
  • 249
  • 1
  • 8
  • Thanks, Paul, this looks promising. However this only shows how to validate a single field. But how can I get two fields at once in the test() function to validate/compare them? Or can I merge two fields with a separator to become one? – Rico May 15 '17 at 13:10
  • 1
    The `test()` function passes the `$form` object as its 5th parameter. You may be able to use this to get & check the data from the other paired field, perhaps with `$form->getData()`? – Paul May 16 '17 at 05:32
  • You pointed me to the right direction, Paul. Thanks again. For all who encounter the same problem with subforms: I had to set the addrulepath attribute to the fieldset containing the subform field (NOT the form in subform.xml). The validate="subform" attribute must be set in this field too instead of setting validate="date" in each field of the subform itself. What you get in your test() method as $value parameter is an OBJECT containing all subform fields which you can iterate over. – Rico May 16 '17 at 12:11
  • This looks something like this then: – Rico May 16 '17 at 12:12
  • No worries. Glad I could help-ish :) – Paul May 17 '17 at 05:11
  • One more question: Is there a way I can change the values in the test function somehow before they get saved? I tried to overwrite $value but that had no effect on the result. – Rico May 17 '17 at 16:11
  • 1
    You may be able to use the `$form` object to call `$form->setValue($name, $group = null, $value = null);` but if that doesn't work you're entering content plugin territory really. See [onContentPrepareData](https://docs.joomla.org/Plugin/Events/Content#onContentPrepareData) and [onContentAfterSave](https://docs.joomla.org/Plugin/Events/Content#onContentAfterSave) for the before and after events. – Paul May 18 '17 at 05:54
  • My trials to change data in JformRule were not successful. Seems if you really need to process module form data BEFORE saving, you can do that implementing a plugin, at least I managed to do it that way thanks to Paul's last hint. Because of the fact that this is another topic, I've posted my solution here: https://joomla.stackexchange.com/questions/20842/how-to-change-form-values-sent-by-a-module/20853#20853 – Rico May 26 '17 at 09:21