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

[2.0.1]RuntimeException: Trying to call method appendmessage on a non-object #10405

Closed
wojewsky opened this issue May 26, 2015 · 10 comments
Closed
Assignees
Labels
bug A bug report status: medium Medium
Milestone

Comments

@wojewsky
Copy link

Hi,

with phalon 1.3.x we've added error messages to form elements by ->appendMessage.
With phalcon 2.0.1 i've got an runtime exception:
RuntimeException: Trying to call method appendmessage on a non-object

For example:

    $form = new \Phalcon\Forms\Form();
    $element = new \Phalcon\Forms\Element\Text('field');
    $form->add($element);
    $element->appendMessage(new \Phalcon\Validation\Message('error message'));
    die(var_dump($form->getMessages()));
@luiz-brandao
Copy link

Any update on this? I'm using Phalcon 2.1.X and I'm getting the same error!

@luiz-brandao
Copy link

I was checking the source code and the problem seems to occur when appendMessage() is called before a call to validate(). The method appendMessage depends on this->_messages being set (line 346) but it is only set in the validate() method at line 111

It seems to me that the logic is that appendMessage should be called only in validators, but maybe there is a validity to allow messages to be added before that. If not, throwing a exception would make the error way clear.

@Zaszczyk
Copy link
Contributor

Up, I also have this problem.

@knallcharge
Copy link

Same here. If I call "method_exists($validator, 'appendMessage')" it returns true, calling appendMessage on the next line will say that validator is not an object ... !?

@Jurigag
Copy link
Contributor

Jurigag commented Jun 21, 2016

@knallcharge what phalcon version you are using ?

@knallcharge
Copy link

@Jurigag 2.0.8

@Jurigag
Copy link
Contributor

Jurigag commented Jun 22, 2016

I have 2.0.13 and can't reproduce problem with code from OP. Can you post your code @knallcharge to reproduce this ?

@sergeyklay sergeyklay added Need information need script to reproduce Script is required to reproduce the issue and removed Bug - Medium labels Jun 22, 2016
@knallcharge
Copy link

knallcharge commented Jun 23, 2016

Using the validator in a unit-test will result in the described behavior.

$myValidator = new MyValidator();
$validation = new Phalcon\Validation();
$validation->bind(new stdClass(),
    array(
        'day'   => date('d'),
        'month' => date('m'),
        'year'  => date('Y')+1
    ));

$myValidator->validate($validation, '');

printing out $validator in the validator itself will return it all right, but using "$validator->appendMessage" in the next line will tell me, it's not an object

@Jurigag
Copy link
Contributor

Jurigag commented Jun 23, 2016

Just write whole script to reproduce without necessary to have tests classes. With this printing etc.

What is MyValidator class ? This code can't reproduce this problem beacause i don't know what is MyValidator.

If you will try to use $validator->appendMessage it's obvious it's not an object beacause $validator in code above it's null beacause you didn't even defined it.

Well actually this code is reproducing a problem:

$myValidator = new Phalcon\Validation\Validator\PresenceOf();
$validation = new Phalcon\Validation();
$validation->bind(new stdClass(),
array(
'day' => date('d'),
'month' => date('m'),
'year' => date('Y')+1
));

$myValidator->validate($validation, '');

This line is producing a problem https://github.com/phalcon/cphalcon/blob/2.0.x/phalcon/validation.zep#L322

Well messages are created here - https://github.com/phalcon/cphalcon/blob/2.0.x/phalcon/validation.zep#L106

With this code those messages group is just null on this point.

This is why it doesn't work. But i don't really know why you can't just add validator to validation and use $validation->validate() ?

I guess that we need to create Phalcon\Validation\Message\Group and set _messages in construct method and that's it.

@sergeyklay sergeyklay added Bug - Medium and removed Need information need script to reproduce Script is required to reproduce the issue labels Jun 27, 2016
@sergeyklay sergeyklay added this to the 2.1.0 milestone Jun 27, 2016
@sergeyklay sergeyklay self-assigned this Jun 27, 2016
@sergeyklay
Copy link
Contributor

Fixed in the 2.1.x branch.

@niden niden added bug A bug report status: medium Medium and removed Bug - Medium labels Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: medium Medium
Projects
None yet
Development

No branches or pull requests

8 participants