Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Flash object protected?!

Phalcon 4.0.0 CODE:

1 $usuario = new Usuario(); 2 $usuario->nome = $this->request->getPost("nome"); 3 4 if ( ! $usuario->save() ) { 5 foreach ($usuario->getMessages() as $message) { 6 $this->flash->error($message); 7 } 8 }

Line 6 points as an error that $messege must be a string. Some how it's not a string, but an object, as print_r($message) shows:

Phalcon\Messages\Message Object ( [code:protected] => 0 [field:protected] => nome [message:protected] => nome is required [type:protected] => PresenceOf [metaData:protected] => Array ( ) ) Phalcon\Messages\Message Object ( [code:protected] => 0 [field:protected] => email [message:protected] => email is required [type:protected] => PresenceOf [metaData:protected] => Array ( ) ) Phalcon\Messages\Message Object ( [code:protected] => 0 [field:protected] => senha [message:protected] => senha is required [type:protected] => PresenceOf [metaData:protected] => Array ( ) ) Phalcon\Messages\Message Object ( [code:protected] => 0 [field:protected] => perfil [message:protected] => perfil is required [type:protected] => PresenceOf [metaData:protected] => Array ( ) )

I tried with each: Phalcon\Flash\Direct and also Phalcon\Flash\Session

I can't use $this->flash->error( $message->message ) because it's a protected object, so what should I do?

Thanks



73.9k
Accepted
answer
edited Jan '20
foreach($model->getMessages() as $message) {
    $this->flash->error( $message->getMessage() );
}

At line 35 you can see there's a get keyword, which automatically generates a getter for the property in question.

Thanks, I should have thought that, sorry. Thank you again!

For future readers: INVO exemples ($message) are steel wrong and Flash tutorial (https://docs.phalcon.io/4.0/pt-br/api/phalcon_flash) do not yet have the method.

foreach($model->getMessages() as $message) {
  $this->flash->error( $message->getMessage() );
}

At line 35 you can see there's a get keyword, which automatically generates a getter for the property in question.