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

Fatal error when using Validator

Hi everybody, when I execute the following code :

use Phalcon\Validation\Validator\PresenceOf;

class AuthorController extends  \Phalcon\Mvc\Controller
{
    public function getAction() {

        $params = array();

        $validation = new Phalcon\Validation();

        $validation->add('id', new PresenceOf(array(
            'message' => _("No author id given")
        )));

        $messages = $validation->validate($params);

    }
}

I've got the following fatal error : Fatal error: Maximum recursion depth exceeded in ..../AuthorController.php on line 18

That seems pretty strange to me as the example is very simple. Does anyone have an explanation ?

Why is your message wrapped in brackets and an underscore? Shouldn't it be more like:

$validation->add('id', new PresenceOf(array(
    'message' => 'No author id given'
)));


943

This is just a shortcut to the getttext notation (https://php.net/manual/en/book.gettext.php) _("my string to translate") is equivalent to gettext("my string to translate")

Even, If i remove the gettext notation and put 'No author id given' instead of _("No author id given"), I sill have the fatal error



943

I don't know if it's relevant but I'm working with :

  • php 5.5.3 on Ubuntu
  • phalcon 1.3.2


26.3k

I think that the problem is with namespaces. Try this and write it it works.


public function getAction() {

        $params = array();

        $validation = new \Phalcon\Validation();

        $validation->add('id', new \Phalcon\Validation\Validator\PresenceOf(array(
            'message' => _("No author id given")
        )));

        $messages = $validation->validate($params);

        var_dump($messages);

    }


943
edited Jul '14

Thanks for your answer. As far as I can see there's nothing in the document preventing the validators to be used in controller. https://docs.phalcon.io/en/latest/reference/validation.html

In fact, the first example suggests validators could be used in controllers as in the example validation is made on the $_POST array.

What I want to achieve is validating data before inserting into database (mongodb or mysql). My application should work with both (mongo + mysql) so I want to validate data outside the model.



26.3k
edited Jul '14

In your code, when creating an entity of Validation(), Phalcon is looking for this class: Phalcon\Validation\Validator\PresenceOf\Phalcon\Validation.

You shoud add at the begining of the file this line:


use Phalcon\Validation;

Or when creating object you can write complete namespace path with \ at the very begining, \Phalcon\.... I have deleted my first post about what you want to achive.. It was wrong:)



943

This is know what I have in my controller and it still does not work : I have the same php fatal error

    <?php
                    class AuthorController extends ApplicationController
                    {
                        public function getAction() {

                            $params = array();

                            $validation = new \Phalcon\Validation();

                            $validation->add('id', new \Phalcon\Validation\Validator\PresenceOf(array(
                                'message' => _("No author id given")
                            )));

                            $messages = $validation->validate($params);

                            var_dump($messages);

                        }
            }


26.3k
edited Jul '14

Hmmm so I cannot help you. I have tried it, and it works on my laptop. But:

  1. Your error is pointig to line 18 (or whatever the line is now) of AuthorController.php. What is in that line?

  2. Try if you get this error with that code as well?

<?php
class AuthorController extends ApplicationController {

  public function getAction() {

    echo "Wtf?";
  }

}

I am beginner to, heh.. You need "the guy" I think:)



943
edited Jul '14

Thanks again. The line 18 is the line that does validation

$messages = $validation->validate($params);

The controller works fine as long as long as I do not call the validators. If it works on your laptop may be it somehing else in my applicaiton like the bootstrap for instance. I guess I have to dig further. Thanks again for your time !!



943

@Conradaek : could you tell me please the php version and phalcon version you are using ?



943
edited Jul '14

It's definitely not a bootstrap issue as if i run the bode below at the beginning of my bootstrap file, I still have the same error.

<?php
$params = array();

$validation = new Phalcon\Validation();

$validation->add('id', new Phalcon\Validation\Validator\PresenceOf(array(
'message' => _("No author id given")
)));

$messages = $validation->validate($params);
exit;


26.3k

I have Phalcon 1.3.2, PHP 5.4.25 on WAMP by Bitnami on Windows XP.

In the last code you provided there is no slashes at beginings of classname paths. Did you tried with these slashes as I proposed?

This is wrong: Phalcon\Validation(). This is ok: \Phalcon\Validation()

This is wrong: Phalcon\Validation\Validator\PresenceOf(.... This is ok: \Phalcon\Validation\Validator\PresenceOf(...



943

Thanks for the informations. Yes indeed, I've tried with the backslash at the beginning and I still have the same fatal error



943
Accepted
answer

I Finally found my problem. I had 2 phalcon configurations files in /etc/php5/apache2/conf.d directory (25-phalcon.ini and 20-phalcon.ini)

I remove on of them, restarted apache and that solved my problem.

Silly mistake that made me lost 2 days :( Fortunately, phalcon worth it.