Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Messages object not present in $form->isValid() with validation events #16702

Open
raicabogdan opened this issue Feb 26, 2025 · 0 comments
Labels
bug A bug report status: unverified Unverified

Comments

@raicabogdan
Copy link

raicabogdan commented Feb 26, 2025

Describe the bug
When using $form->isValid() to validate form and you have a validation class with beforeValidation() method, you get error:
Trying to call method count on a non-object

To Reproduce
Steps to reproduce the behavior:
As per documentation https://docs.phalcon.io/5.8/filter-validation/#events

class LoginValidation extends Validation
{
    public function initialize()
    {
        // ...
    }

    public function beforeValidation($data, $entity, $messages)
    {
        if ($this->request->getHttpHost() !== 'admin.mydomain.com') {
            $messages->appendMessage(
                new Message(
                    'Only users can log on in the admin domain'
                )
            );

            return false;
        }

        return true;
    }
}

Details

  • Phalcon version: 5.8
  • PHP Version: php 8.3

Additional context
The issue starts from $form->isValid() when validation->validate(data, entity); is being called

/**
* Perform the validation
*/
let messages = validation->validate(data, entity);
if messages->count() {

and expects back a Phalcon\Messages object.

In the Phalcon\Filter\Validation class, in the validate() method, the messages variable is getting passed along to event beforeValidation and once the status returned from validate() is false (as per documentation), the messages do not get returned back.

/**
* Implicitly creates a Phalcon\Messages\Messages object
*/
let messages = new Messages();
if entity !== null {
this->setEntity(entity);
}
/**
* Validation classes can implement the 'beforeValidation' callback
*/
if method_exists(this, "beforeValidation") {
let status = this->{"beforeValidation"}(data, entity, messages);
if status === false {
return status;
}
}
let this->messages = messages;

Not entirely sure but, is it not possible to do something along the lines

// Phalcon\Forms\Form
        /**
        * Perform the validation
        */
        let result = validation->validate(data, entity);
        if result === false {
            let messages = validation->getMessages();
        } else {
            let messages = result;
        }
        
        if messages->count() {
            // Add validation messages to relevant elements
            for elementMessage in iterator(messages) {
                this->get(elementMessage->getField())->appendMessage(elementMessage);
            }

            messages->rewind();

            let validationStatus = false;
        }

and also

// Phalcon\Filter\Validation
         /** 
          * Implicitly creates a Phalcon\Messages\Messages object 
          */ 
         let this->messages = new Messages(); 
  
         if entity !== null { 
             this->setEntity(entity); 
         } 
  
         /** 
          * Validation classes can implement the 'beforeValidation' callback 
          */ 
         if method_exists(this, "beforeValidation") { 
             let status = this->{"beforeValidation"}(data, entity); 
  
             if status === false { 
                 return status; 
             } 
         }

and use this->appendMessage() in the event method instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: unverified Unverified
Projects
None yet
Development

No branches or pull requests

1 participant