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

Incorrect operation of the validator

There is a code validations enter e-mail address:

    class emailTemplateFormValidation extends Validation
    {
        public function initialize()
        {
            $this->add('assignedEmail', new Email(array(
                'message' => 'Неверный e-mail адрес',
            )));
        }
    }

    ...................

    $messages = $emailTemplateFormValidation->validate($cleanedPostData);
    if (count($messages)) {
        foreach ($messages as $message) {
            $message = $message.'<br>';
        }
        $this->flashSession->error($message);
    }

it is expected that it works like this:

  • if a valid e-mail ("[email protected]"), then the validation passes;
  • if you entered an incorrect e-mail ("[email protected]"), then the validation fails;
  • if you enter an empty string (""), then the validation passes.

in fact: when entering an empty string ("") - validation does not pass.

I need: if the field is empty (""), then the validation passes successfully

edited Mar '15

your validation in not complete. try this:

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


14.3k

You misunderstood me. I need to when the field is not filled (empty value), validation passed. In the above code on an empty e-mail field error: 'The e-mail is required'

edited Mar '15

Ok, sorry. so in this case you should use a custom validator instead of built-in Email validator like this:

<?php

use Phalcon\Validation\Validator\Email;

class MyCustomEmailValidator extends Email
{

    /**
     * Executes the validation
     *
     * @param Phalcon\Validation $validator
     * @param string $attribute
     * @return boolean
     */
    public function validate(\Phalcon\Validation $validator, $attribute)
    {
        $value = trim($validator->getValue($attribute));

        if (empty($value)) { // valid if value is empty
            return true;
        }

        return parent::validate($validator, $attribute);
    }
}


14.3k

Thanks, it works, but I want to implement it a little differently.

Can it implement inside the extension class "Validation"? I need to know how to define a resource type, obtained for validation, specifically, how to determine the type of Email.

    class emailTemplateFormValidation extends Validator
    {

        /**
         * Executes the validation
         *
         * @param Phalcon\Validation $validator
         * @param string $attribute
         * @return boolean
         */
        public function validate($validator, $attribute)
        {
            if (/* if resource type == Phalcon\Validation\Validator\Email  <-tell me, what to write here*/) {
                ...
            }
            else {
                parent::validate();
            }
        }
    }


10.5k
Accepted
answer

I don't know what you want to do exactly! please give more information or an example.