We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Custom Validation appenMessage() not working

hi,

I created custom rule for Validation. But not working. I tried to log to system and everything is ok. Rule is working but appendMessage is not working...

Code

    public function validate($attribute)
    {
        $logger = new FileAdapter("app/logs/validateelements.log");
        $logger->begin();

        $field = $this->getOption('field');
        $fieldvalue = $attribute->getValue($field);

        if (($fieldvalue != 'allowed') || ($fieldvalue != null)) {

            $logger->alert("Rule working"); // Log working
            $logger->commit();

            $message = $this->getOption('message');
            $type = "InvalidValue";
            $this->appendMessage(new Message($message, $field, $type)); // This is not working

            return false;
        }
        else
        {
            return true;
        }
    }

Why this is not working, anybody can explain me ?



2.0k

try like this:

 public function validate($validator, $attribute)
    {
        $value = $validator->getValue($attribute);
        if ($this->getOption('allowEmpty') === true && empty($value)) {
            return true;
        }
        $isValid = true;
        if (!filter_var($value, FILTER_VALIDATE_URL)) {
            $isValid = false;

            $message = $this->getOption('message');
            if (!$message) {
                $message = sprintf('Wrong url: %s', implode(',', $fields));
            }
            $validator->appendMessage(new Message($message, $attribute, 'MyUrl'));
        }

        return $isValid;
    }

Its's works for me