Rohit Khatri
Rohit Khatri

Reputation: 2028

Joomla throwing error for custom validation rule

I'm a beginner to the Joomla and trying to develop a component, but when I try to add rules to my backend panel, I keep getting this error. If anybody can tell me what I'm doing wrong, will be appreciated.

Here's the code for the rule which I've written.

com_mycomponent/models/rules/segment_name.php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/**
 * Form Rule class for the Joomla Framework.
 */
class JFormRuleSegmentName extends JFormRule
{
    /**
     * The regular expression.
     *
     * @access  protected
     * @var     string
     * @since   2.5
     */
    protected $regex = '^[^0-9]+$';
}

And this is my form code:

com_mycomponent/models/forms/segment.xml

<?xml version="1.0" encoding="utf-8"?>
<form addrulepath="/administrator/components/com_mycomponent/models/rules">
    <fieldset>
        <field
                name="id"
                type="hidden"
                />
        <field
                name="segment_name"
                type="text"
                label="COM_MYCOMPONENT_SEGMENT_NAME_LABEL"
                description="COM_MYCOMPONENT_SEGMENT_NAME_DESC"
                size="40"
                class="inputbox"
                validate="segment_name"
                required="true"
                default=""
                />
    </fieldset>
</form>

It is not working for me, this is what I get when I try to add or edit a new segment:

Error:

An error has occurred. 0 JForm::validateField() rule segment_name missing.

Upvotes: 0

Views: 718

Answers (1)

Amit Ray
Amit Ray

Reputation: 3485

There are some rules that are not followed

  1. You have used segment_name as file name but you have given SegmentName as class name. Both should match.

  2. Not mandatory but you can use in the rules file

    jimport('joomla.form.formrule');

    defined('_JEXEC') or die('Restricted access');

    // import Joomla formrule library jimport('joomla.form.formrule');

    /**

    • Form Rule class for the Joomla Framework. */ class JFormRuleSegment_rule extends JFormRule {
  3. In your form the addrule path should be given like this

    <form>
        <fieldset name="form_name" addrulepath="components/com_mycomponent/models/rules">
     ----------------fields
        </fieldset>
    </form>

form_name should be your form name The suffix of the classname (JFormRule[SUFFIX]) must be the same as the rule file name. https://docs.joomla.org/Server-side_form_validation

Upvotes: 1

Related Questions