2

Trying to create a custom validation rule in frontend, always get the same error:

0 Joomla\CMS\Form\Field\TextField::validate() rule Espaciocheck missing.

Please, will the path to the file be wrong? What could be missing?

com_aaprueba/forms/espacio.xml

<?xml version="1.0" encoding="utf-8"?>
<form addfieldprefix="Sfi\Component\Aaprueba\Site\Field">

    <fieldset addruleprefix="Sfi\Aaprueba\Site\Rule">
    
        <field 
            name="espacio" 
            type="text" 
            label="COM_AAPRUEBA_FIELD_ESPACIO" 
            description="COM_AAPRUEBA_FIELD_ESPACIO_DESC" 
            required="true"
            validate="Espaciocheck"
            />
...

com_aaprueba/tmpl/espacio/default.php

...
<form action="<?= Route::_('index.php?option=com_aaprueba&view=espacio&id='.(int) $this->item->id); ?>" method="post" name="adminForm" id="adminForm" class="form-validate">
...
<?= $this->form->renderField('espacio'); ?>
...

com_aaprueba/src/View/Espacio/HtmlView.php

namespace Sfi\Component\Aaprueba\Site\View\Espacio;
...
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\HTML\Helpers\Behavior;
use Joomla\CMS\Form\FormHelper;
...

class HtmlView extends BaseHtmlView
{
...
    public function display($tpl = null)
    {
        Behavior::formvalidator();
...

com_aaprueba/src/Rule/EspaciocheckRule.php

namespace Sfi\Component\Aaprueba\Site\Rule;

\defined('_JEXEC') or die;

use Joomla\CMS\Form\FormRule;

class EspaciocheckRule extends FormRule
{
    public function test(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
    {
        // Logic after working test
        return true;
    }
}
1
  • 2
    Wrong namespace in addruleprefix attribute.
    – Sharky
    Commented Dec 27, 2022 at 8:38

1 Answer 1

2

Thanks! Moreover I have also forgotten use Joomla\CMS\Form\Form; and use Joomla\Registry\Registry; in EspaciocheckRule.php. Now it's working fine.

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.