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

Date validation not working as expected

I want to validate date from user input so I made this function:

private function validateData(string $date, string $returnUrl)
    {
        $validator = new Validation();

        $validator->add($this->request->getPost($date), new DateValidator([
            'format' => 'Y-m-d',
        ]));

        $messages = $validator->validate($this);

        if (count($messages)) {
            foreach ($messages as $message) {
                $this->flashSession->error($message->getMessage());
            }
            return $this->response->redirect($returnUrl)->send();
        }
    }

Now everytime I edit a post an error message get's thrown: "Field 2015-02-10 is not a valid date"

So I thought maybe it want's the actual name of the field. So I tried that but then I get: "Field publicationDate is not a valid date".

Can anyone explain to me why this is happening and why the validation isn't being stopped if the validation encounters an unsupported date format?

$validator->add($this->request->getPost($date), new DateValidator([
            'format' => 'Y-m-d',
        ]));

change to:

$validator->add($fieldName, new DateValidator([
            'format' => 'Y-m-d',
        ]));

        $messages = $validator->validate($this);

change to:

        $messages = $validator->validate($this->request->getPost());


2.2k

Why doesn't it work if I do?

        $messages = $validator->validate($this->request->getPost($postName));
$validator->add($this->request->getPost($date), new DateValidator([
           'format' => 'Y-m-d',
       ]));

change to:

$validator->add($fieldName, new DateValidator([
           'format' => 'Y-m-d',
       ]));

       $messages = $validator->validate($this);

change to:

       $messages = $validator->validate($this->request->getPost());


26.3k
Accepted
answer

The snippet from your code:

$validator->add($this->request->getPost($date), new DateValidator([
     'format' => 'Y-m-d',
]));

Let's try to figure out what you are getting?

$date - means field name?

$this->request->getPost($date) - evals as '2015-02-10', because it's the value of the field?

So finally you add '2015-02-10' field to validator and then try to validate through DateValidator.

Later you pass current object to validate:

$messages = $validator->validate($this);

So validator tries to find $this->{2015-02-10} and validate it. I think the value of $this->{2015-02-10} is not set at all?


You should pass array key (a.k.a. fieldName in this case) to validator, and later pass array which you want to validate.

Example:

private function validateData(string $date, string $returnUrl)
    {
        $validator = new Validation();

        $validator->add('publishDate', new DateValidator([
            'format' => 'Y-m-d',
        ]));

        $dataToValidate = ['name' => 'John', 'lastname' => 'Doe', 'publishDate' => '1999-01-01'];

        $messages = $validator->validate($dataToValidate);

        if (count($messages)) {
            foreach ($messages as $message) {
                $this->flashSession->error($message->getMessage());
            }
            return $this->response->redirect($returnUrl)->send();
        }
    }