0

I'm following along with this tutorial https://docs.joomla.org/J3.x:Server-side_form_validation

My form

<form>
    <fieldset name="form_name" addrulepath="components/com_mycomponent/models/rules/myrule.php">
       <field
         name="number" 
         type="text"
         validate="myrule" 
    </fieldset>
</form>

My myrule.php file in components/com_mycomponent/models/rules/

class JFormRuleMyRule extends JFormRule
{
    protected $regex = '[0-9]';
}

Everything is set accordingly and yet it won't work. I may be doing something wrong. Could you please tell me what it is?

The component's name is correct, the file name, class name, path...everything seems to be okay.

If I input letters instead of a number and submit the form, no error is displayed.

My form in the default.php file

<form action="<?php echo JRoute::_('index.php?option=com_my_component&view=my_view&id='.$this->myItemId['id']); ?>"
    method="post" name="adminForm" id="adminForm" class="form-validate">
       <fieldset name="details">
         <legend>Details</legend>
         <?php echo $this->form->renderFieldset('details');  ?>
       </fieldset>       
       <button type="button" class="btn btn-primary" onclick="Joomla.submitbutton('send.submit')">Send</button> 

    <?php echo JHtml::_('form.token'); ?>
</form>

My XML form myform.xml

    <form name="adminForm" addrulepath="components/com_mycomponent/models/rules">
        <fieldset name="details" 
           <field
             name="day" 
             type="text"
             validate="myrule"
            /> 
        </fieldset>
    </form>

My myrule.php file in components/com_mycomponent/models/rules

class JFormRuleMyRule extends JFormRule
{
    protected $regex = '[0-9]';
}

UPDATE:

My controller send.php with the getModel() overrided

class GalleryControllerSend extends JControllerForm
{   
    public function getModel($name = 'Gallery', $prefix = 'GalleryModel', $config = array('ignore_request' => true))
    {
        return parent::getModel($name, $prefix, $config);
    }

    public function submit($key = null, $urlVar = null)
    {   

        $this->checkToken();       
        $input=JFactory::getApplication()->input;
        $data  = $input->get('jform', array(), 'array');
        $model = $this->getModel();  // get a handle on your model
        $form = $model->getForm($data, false);  // use the model to build the form  
        $validData = $model->validate($form, $data);  // performs the validation
        $errors = $model->getErrors();  // gets a list of the validation errors 
        $app->enqueueMessage($errors[$i]->getMessage(), 'warning');  // output the error messages   
     }

}

gives me 0 - Call to a member function getForm() on bool

Call to a member function getForm() on bool [root]\components\com_gallery\controllers\send.php:35

0

1 Answer 1

2

Just do

addrulepath="components/com_mycomponent/models/rules"

and leave out the /myrule.php.

I presume you're terminating the <field .../> ok in the XML and adding <?php at the start of the rule file?

UPDATE

It looks like your submit button on the form is resulting in task="com_mycomponent.submit" being sent in the HTTP POST request, and is thus running the submit() method.

Usually Joomla uses a Save button, which sets task="com_xxx.save", which will result in the save() method being run.

This method is available in JControllerForm and usually covers what you need to do to handle the HTTP POST.

However, it's instructive to get this working by yourself in your submit function, but you have to add some extra lines. The relevant lines from JControllerForm::save which you want to incorporate are

$data = $this->input->post->get('jform', array(), 'array');  // just change your variable from $form to $data, to follow convention

$model = $this->getModel();  // get a handle on your model

$form = $model->getForm($data, false);  // use the model to build the form

$validData = $model->validate($form, $data);  // performs the validation

$errors = $model->getErrors();  // gets a list of the validation errors

$app->enqueueMessage($errors[$i]->getMessage(), 'warning');  // output the error messages

By the way, you might find https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Adding_verifications useful as well.

For your model override try:

public function getModel($name = 'Gallery', $prefix = 'GalleryModel', $config = array('ignore_request' => true))
{
    return parent::getModel($name, $prefix, $config);
}
13
  • Yes thank you. I misunderstood the documentation. Apr 10, 2020 at 11:26
  • It's still not working even after the changes. Maybe something to do with my form/submit button in default.php file? Apr 10, 2020 at 12:07
  • 1
    OK I've updated my answer with a suggestion for your getModel() function in your controller. Apr 10, 2020 at 21:22
  • 1
    I don't know Thiago - I put your code above into a component and it worked ok! I zipped up my component and put it up on my google drive at drive.google.com/file/d/1TgGtsM6hPLSk-iO3K9W3vM1iMEAd8Wyd/… so you can download it and have a look and see what the difference is if you like. Apr 11, 2020 at 19:36
  • 1
    Yeah, I thought it might have been something trivial like that! Glad to hear you got it sorted. I'll remove that zip file from my google drive. Apr 11, 2020 at 20:43

Your Answer

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

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