0

Here's the code which is not working:

$email = $this->request->get('emailFromPost','email','');

What I want is getting a right validated email variable $email. While it's not working anyhow. Can anyone help me please?

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
X.Herry
  • 65
  • 1
  • 3
  • Filters are sanitizing your input, they do not validate it. Can you provide sample email addresses that you want to test? – Nikolay Mihaylov Aug 16 '16 at 06:26
  • Thanks, but I'm a starter on Phalcon. So I can't catch your meaning. In my code example, I post a emailFromPost variable like 'test'. And I got $email as 'test'. I hope I can get $email as false. – X.Herry Aug 16 '16 at 06:35
  • By sanitizing I mean that it will filter unwanted data, for examples you can check this link: http://stackoverflow.com/documentation/phalcon/4917/filtering-and-sanitizing/17367/convenient-in-model-sanitizing#t=201608160647303667478 If you want returned as false you have to use the validation as suggested by Daison below. – Nikolay Mihaylov Aug 16 '16 at 06:48
  • Thank you so much. Gotcha now. Maybe Daison's solution is the best while I think it's a little complex. Can I just use another way:`filter_var($email, FILTER_VALIDATE_EMAIL)` instead? – X.Herry Aug 16 '16 at 07:07
  • Well depends on what you need. If you have a form you should use [Phalcon forms](https://docs.phalconphp.com/en/latest/reference/forms.html) – Nikolay Mihaylov Aug 16 '16 at 07:14
  • I really appreciate your help. Learning the doc. – X.Herry Aug 16 '16 at 07:49

1 Answers1

0

Follow this kind of way:

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

$validation = new Validation;
$this->add('email', new Email(['message' => 'Email is not valid']));


if (count($messages = $validation->validate($data)) {
   // iterate $messages
   foreach ($messages as $message) {
       echo $message."\n";
   }
}
NosiaD
  • 587
  • 1
  • 6
  • 18