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

Validation doesn't work in app

(I post this in security, but I'm not shure)

I've a problem, and that is that Phalcon\Validation doesn't works in one part of my app; in the other hand, it works perfectly in the login.

LoginController.php

$this->view->disable();

        $request = $this->request;

        if ($request->isPost() === true)
        {
            $response = new Response();
            $validacion = new Validation();
            $resultado = false;

            $validacion->add("usuario", new Regex(
                array(
                    'pattern' => "/^[a-zA-Z0-9]+$/",
                    'cancelOnFail' => true
                    )
                ));
            $validacion->add("contrasena", new Regex(
                array(
                    'pattern' => "/.[^\s]./",
                    'cancelOnFail' => true
                    )
                ));

            if ($validacion->validate($request->getPost()))
            {
                // Validates the user.
            }

            $response->setJsonContent(array('respuesta' => $resultado));

            return $response;
        }

ParteController.php

$this->view->disable();

        $request = $this->request;

        if ($request->isPost() === true)
        {
            $this->view->disable();

            $response = new Response();
            $validacion = new Validation();
            $resultado = false;

            $validacion->add("conductasElegidas", new Regex(
                array(
                    // No matter what regex I put, it will validate everything ok. My test string for this post var is "1234"
                    'pattern' => "/^abc$/",
                    'cancelOnFail' => true
                    )
                ));

            // Always happens.
            if ($validacion->validate($request->getPost()))
            {
                $resultado = true;
            }

            $response->setJsonContent(array('respuesta' => $resultado));

            return $response;
        }


10.0k
Accepted
answer
edited Sep '14
        $validacion->add("conductasElegidas", new Regex(array(
                'message' => 'Not valid!!!'
                'pattern' => "/^abc$/",
                'cancelOnFail' => true
                )
            ));

        // Always happens.
        $messages = $validacion->validate($request->getPost());
        if (count($messages)) {
            foreach ($messages as $message) {
                        echo $message, '<br>';
            }
        }

Try it.



33.8k

It works, but why? I do the same in both controllers.