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 - "There is no data to validate"

This code gives me white screen with "There is no data to validate" only. $email is not empty. What is wrong???

use Phalcon\Validation;
use Phalcon\Validation\Validator\Email;

class IndexController extends BaseController
{
    public function indexAction()
    {
        if ($this->request->isPost()) {
            $email = $this->request->getPost('email');

            $validation = new Validation();

            $validation->add('email', new Email(array(
                'message' => 'The e-mail is not valid'
            )));

            $messages = $validation->validate($email);
        }
    }
}


27.4k
Accepted
answer
edited Mar '14

If you want to validate with phalcon then you should validate $_POST array not scalar value

use Phalcon\Validation;
use Phalcon\Validation\Validator\Email;

class IndexController extends BaseController
{
    public function indexAction()
    {
        if ($this->request->isPost()) {
            $email = $this->request->getPost('email');

            $validation = new Validation();

            $validation->add('email', new Email(array(
                'message' => 'The e-mail is not valid'
            )));

            $messages = $validation->validate($_POST);
        }
    }
}

You can filter values in Phalcon $request->getPost("post viariable", "filter")

class IndexController extends BaseController
{
    public function indexAction()
    {
        if ($this->request->isPost()) 
        {
            $email = $this->request->getPost('email','email');
            //or check in $_GET and $_POST arrays
            $email = $this->request->get('email','email');
            if($email == false)
            {
                $messages[] = 'The e-mail is not valid';
            }
        }
    }
}

You can validate by PHP function filter_var() too

class IndexController extends BaseController
{
    public function indexAction()
    {
        if ($this->request->isPost()) {
            $email = $this->request->getPost('email');

            if(!filter_var($email, FILTER_VALIDATE_EMAIL))
            {
                $messages[] = 'The e-mail is not valid';
            }
        }
    }
}