0

It will try to include new rules to validate the fields of a form and it does not work. I can not even get messages on the screen. Am I missing something in the code?

Here is my code:

    <form addrulepath="/administrator/components/com_mycomponent/models/rules">
    <field 
        name="custom_price" 
        label="Price" 
        description="Type price" 
        type="text" 
        validate="CustomNumber"
        required="true"
        message="Custom validation messsage"
    />
    </form>

In the /rules directory I have included a file with the name "Custom", which includes:

class JFormRuleCustomNumber extends JFormRule
{
    public function test(SimpleXMLElement $element, $value, $group = null, JRegistry $input = null, JForm $form = null)
    {
        if ($value < 1) {
            $element->attributes()->message = 'The value ' . $value . ' is not valid because it is less than 1';
            return false;
        }

        if ($value > 1000) {
            $element->attributes()->message = 'The value ' . $value . ' is not valid because it is greater than 1000';
            return false;
        }

        return true;
    }
}

Am I missing some code? Why do not you even access the file inside /rules? Please, if there is any other way to validate on the server side I would appreciate it, thanks

0

The filename must be lowercase and match the name of the rule (not including casing). So rename Custom.php to customnumber.php.

Setting the message through $element->attributes()->message doesn't work for me. $element['message'] works. But the recommended way to handle validation error is to return an Exception:

if ($value < 1)
{
    return new UnexpectedValueException('The value ' . $value . ' is not valid because it is less than 1');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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