No Code Attached Yet
avatar PhocaCz
PhocaCz
29 Oct 2021

Hi,

can somebody paste some example to this:

#12414

Joomla 3:

  • XML Form defined "customemailrule" for form field
  • In controller: $validate = $model->validate($form, $data);// Controller extends FormController
  • parent Model ask the validate rule: components/com_custom/models/rules/customemailrule.php (FormRuleCustomEmail extends EmailRule)

Joomla 4
This triggers: Joomla\CMS\Form\Field\EmailField::validate() rule customemailrule missing in libraries/src/Form/Form.php (1236)

What is the right way to implement custom rules in Joomla 4? (in comparison to J3)

Thank you.

EDIT:

Possible problem can be in libraries/src/Form/FormHelper.php

where loadClass method sets wrong path:

Joomla\CMS\Form\Rule\CustomEmailRule

istead of

components/com_custom/models/rules/customemail.php

even the XML Form defined it:
<fieldset addfieldpath="/components/com_custom/models/fields" addrulepath="/components/com_custom/models/rules">

avatar PhocaCz PhocaCz - open - 29 Oct 2021
avatar joomla-cms-bot joomla-cms-bot - change - 29 Oct 2021
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 29 Oct 2021
avatar PhocaCz PhocaCz - change - 29 Oct 2021
The description was changed
avatar PhocaCz PhocaCz - edited - 29 Oct 2021
avatar PhocaCz PhocaCz - change - 29 Oct 2021
The description was changed
avatar PhocaCz PhocaCz - edited - 29 Oct 2021
avatar PhocaCz PhocaCz - change - 29 Oct 2021
The description was changed
avatar PhocaCz PhocaCz - edited - 29 Oct 2021
avatar Fedik
Fedik - comment - 29 Oct 2021

old way still should work, make sure validate="" attribute is customemail and not CustomEmail

can somebody paste some example to this:

it basically create a validate() method inside your field. This is default:

/**
* Method to validate a FormField object based on field data.
*
* @param mixed $value The optional value to use as the default for the field.
* @param string $group The optional dot-separated form group path on which to find the field.
* @param Registry $input An optional Registry object with the entire data set to validate
* against the entire form.
*
* @return boolean|\Exception Boolean true if field value is valid, Exception on failure.
*
* @since 4.0.0
* @throws \InvalidArgumentException
* @throws \UnexpectedValueException
*/
public function validate($value, $group = null, Registry $input = null)
{

avatar PhocaCz
PhocaCz - comment - 29 Oct 2021

Hi, I am testing Joomla 3 component, so it works in Joomla 3 (this is not new component), so things like

validate="" attribute is customemail and not CustomEmail

are OK there.

It just does not load the class in this file:
components/com_custom/models/rules/customemail.php

BTW the field is core field:
<field name="email" type="email" ... validate="customemail" filter="string" />

only rule is custom rule from the custom component.

Jan

avatar PhocaCz
PhocaCz - comment - 29 Oct 2021

Hmmm, the behaviour is completely different and broke the current working feature :-(

avatar joomdonation
joomdonation - comment - 29 Oct 2021

Do you mind attach a copy of your current code and tell us what we need to do to see the error? That would help us to see the error and try to figure out what's wrong.

avatar Fedik
Fedik - comment - 29 Oct 2021

I cannot reproduce, I tried with mod_custom:
Create a field <field type="text" name="test" label="test" validate="customemail"/>;
Add addrulepath="/modules/mod_custom/rules" to fieldset;
Create a rule: modules/mod_custom/rules/customemail.php;
with:

class JFormRuleCustomemail
{
	public function test(\SimpleXMLElement $element, $value)
	{
		return false;
	}
}

On save I got "invalid field: Test", as expected.

avatar PhocaCz
PhocaCz - comment - 29 Oct 2021

Do you get the same with type="email" and class JFormRuleCustomEmail extends EmailRule?

avatar PhocaCz PhocaCz - close - 29 Oct 2021
avatar PhocaCz
PhocaCz - comment - 29 Oct 2021

Hi @Fedik , thank you for your comments, based on this I have found the problem. When using legacy MVC structure, this is not valid: FormRuleCustomEmail extends EmailRule, but this is valid: JFormRuleCustomEmail extends EmailRule

So this seems to be solved, I will do some more tests on different rules, but I hope, this was the reason for the issue.

Thank you, Jan

avatar PhocaCz PhocaCz - change - 29 Oct 2021
Status New Closed
Closed_Date 0000-00-00 00:00:00 2021-10-29 19:10:30
Closed_By PhocaCz

Add a Comment

Login with GitHub to post a comment