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.

How to add a custome Validator?

Hi,

it's not new, that the validatior only has ten functions which you can call for validate your form. I want to know how I can create my own class and add this to my validator.

In my Validation I want to add something like this:

    ->add('myField', new myClass(array(
        'message'     =>       'you are doing it wrong!',
        'myRule'      =>       'rule what should be validated',      //maybe if there is a function where you can add parameters
    )))

My first question is, would it work if I create my own Validator-Class?

My second question is where should I save my class?

And the third question is what did I need to create my class?

I have seen a lot of topics, in which people says that they have different problems. But I didn't find a topic in which is declared how you can do it.

My Problem is definetly not how I can create a Validator. On normal php I have written a Validator with about 70 validaton functions. But I want to know how I can get this working in phalcon. I don't know how the validator works in phalcon. So there are many easy questions to much. But they are important because I have to know it, before I make something wrong.

At first I want to try a simple test function. I only want to see how it works. Currently I have this:

use Phalcon\Validation\Message;
use Phalcon\Validation\Validator;
use Phalcon\Validation\ValidatorInterface;

class IsInteger extends Validator implements ValidatorInterface {

    public function validate(Validation $validator, $attribute) {
        $integer = $validator->getValue($attribute);                         //if you read my second post, here was my mistake number 1

        if(!is_int(intval($integer)) and ctyp_alpha($integer)) {            //if you read my second post, here was my mistake number 2
            $message = $this->getOption('message');

            if(!$message) {
                $message = "The value is not an integer";
            }

            $validator->appendMessage(new Message($message, $attribute, 'Integer'));

            return false;
        }

        return true;
    }
}

Should I change anything? Have I done any mistakes so far?

And where should I save this file in my Project? app/... ? I have saved the file currently in app/plugins/validators. Is it right/ok?

Currently this error appears:

Fatal error: Class 'Phalcon\Validation\Validator\IsInteger' not found in H:\Config\PortableApps\PortableApps\XAMPP\App\xampp\htdocs\wettermelder-phalcon\app\controllers\FormularController.php on line 196

This is the line 196:

 ->add('postalcode', new IsInteger(array(
    'message' =>   'no Integer',
)))

I think this i not the problem because this works fine:

->add('postalcode', new PresenceOf(array(
    'message'     => "Die Postleitzahl muss eingegeben werden!",     //sorry, I am from Germany ;)
)))

In the "head" of the document I loaded this:

use Phalcon\Validation\Validator\IsInteger;

I know that a postalcode is not a integer, but it's only a test.



106.1k
Accepted
answer

Read about PSR-0 etc. You need to load this class with phalcon loader. Also dont use Phalcon namespaces imho, create your own like:

namespace Michdo93\Validation\Validator;

Also you shouldnt put classname in namespace.

Thank you very much. It hate namespaces...

I think I have an error in my code, too. Here the right solution:

use Phalcon\Validation\Message;
use Phalcon\Validation\Validator;
use Phalcon\Validation\ValidatorInterface;

class IsInteger extends Validator implements ValidatorInterface {

    public function validate(Validation $validator, $attribute) {
        $integer = $validator->getValue($attribute);

        if(is_int(intval($integer)) and !ctype_alpha($integer)) {
            return true;
        } else {
            $message = $this->getOption('message');

            if(!$message) {
                $message = "The value is not an integer";
            }

            $validator->appendMessage(new Message($message, $attribute, 'Integer'));

            return false;           
        }
    }
}

It work's fine and maybe there is a person who need the same code or will try this code as simple test.

Read about PSR-0 etc. You need to load this class with phalcon loader. Also dont use Phalcon namespaces imho, create your own like:

namespace Michdo93\Validation\Validator;

Also you shouldnt put classname in namespace.