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


40.8k
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';
            }
        }
    }
}