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

Closed
raicabogdan opened this issue Feb 26, 2025 · 1 comment
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report status: medium Medium

Comments

@raicabogdan
Copy link
Contributor

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.

@raicabogdan raicabogdan added bug A bug report status: unverified Unverified labels Feb 26, 2025
@niden niden added status: medium Medium 5.0 The issues we want to solve in the 5.0 release and removed status: unverified Unverified labels Mar 3, 2025
@niden niden added this to Phalcon v5 Mar 3, 2025
@niden niden moved this to Implemented in Phalcon v5 Mar 3, 2025
@niden
Copy link
Member

niden commented Mar 3, 2025

Resolved in #16704

Thank you @raicabogdan

@niden niden closed this as completed Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report status: medium Medium
Projects
Status: Implemented
Development

No branches or pull requests

2 participants