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.

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;
        }


5.2k
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.



18.6k

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