0

I've created a new custom field type name "lazyload" on Joomla 4.

Basically it is a list field that places an empty (no options) HTML select tag and uses the select2 plugin to fetch the options via ajax (this is necessary because some fields can have as much as 7000+ options).

I'm using it on articles and everything is working fine apart from the moment I save the article: it returns "Invalid field: [field name]" and it does not store the selected values.

Of course this is because a safety good practice: saved results must be validated against existing options, but how can I disable this validation? - or, better still, create my own validation rules?

2
  • The validation rules for a field are determined by the attributes. If it's validating against options, then there's probably an entry for validate="options" in the form's XML definition. Remove that attribute and it should stop trying to validate the options. If you want to create your own validation rules have a look at docs.joomla.org/J3.x:Server-side_form_validation. It's the J3 version, but it's a good starting point Jun 7, 2023 at 21:15
  • @MattGarrod I did add $fieldNode->setAttribute('validate', '') and it removed validation. Still struggling on how to create the rules: documentation is for J3 and not quite clear on how to create the rules for a field plugin on J4 :( Jun 8, 2023 at 0:18

1 Answer 1

0

There is nothing special about creating validation rules for use in custom field plugins. The rule itself is a loose class file and the plugin does not interact with it on its own. To use it, you have to set the validate attribute on the field element in the form. You're already aware that in a custom field plugin this is done by manipulating the field node in onCustomFieldsPrepareDom event:

public function onCustomFieldsPrepareDom($field, DOMElement $parent, Form $form)
{
    $fieldNode = parent::onCustomFieldsPrepareDom($field, $parent, $form);
    $fieldNode->setAttribute('addrulepath', 'plugins/fields/mylist/rule');
    $fieldNode->setAttribute('validate', 'my.test');

    return $fieldNode;
}

The code above loads MyFormRuleTest rule class located in plugins/fields/mylist/rule/test.php file. If your plugin uses namespaced classes, use addruleprefix attribute instead:

$fieldNode->setAttribute('addruleprefix', 'My\\Plugin\\Fields\\MyList\\Rule');
$fieldNode->setAttribute('validate', 'Test');

This code would load My\Plugin\Fields\MyList\Rule\TestRule class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.