{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"Validation","type":"basic","slug":"supporting-structure-validator","excerpt":"","body":"# Validation\n\nWe definitely have something we need to validate before executing script going to other 3rd party resources such as sql server.\n\nWe validate data to match our needs.\n\nThe class **Phalcon\\Validation** will do the job; We have sample validation classes in our ``project-name/components/Validation``\n\n\n---\n\n\n## Creating Class Validator\n\n```php\n<?php\n...\n\nclass JustAnotherValidator extends Validator\n{\n public function initialize()\n {\n $this->add('email', new PresenceOf([\n 'message' => 'Email is required',\n ]));\n\n $this->add('email', new Email([\n 'message' => 'Email is not valid',\n ]));\n\n $this->add('email', new Uniqueness([\n 'model' => User::class,\n 'message' => 'Email already exist'\n ]));\n\n $this->add('password', new PresenceOf([\n 'message' => 'Password is required',\n ]));\n\n $this->add('password', new Confirmation([\n 'with' => 'repassword',\n 'message' => 'Password and Repeat Password must match',\n ]));\n\n $this->add('repassword', new PresenceOf([\n 'message' => 'Repeat Password is required',\n ]));\n }\n}\n```\n\nThe above code uses ***PresenceOf***, ***Email***, ***Uniqueness*** and ***Confirmation***, those classes are validators that filters a value from your data.\n\nLet's assume we have this kind of data \n\n```php\n$data = [\n 'email' => 'johndoe',\n 'password' => '123qwe',\n 'repassword' => 'qwe123',\n];\n\n# this throws error\n# - email format error\n# - password and repassword not match\n$validator = new SomeValidator();\n$validator->validate($data);\n```\n\nand we have this:\n\n```php\n$data2 = [\n 'email' => 'johndoe:::at:::gmail.com',\n 'password' => '123qwe',\n 'repassword' => '123qwe',\n];\n\n# this is ok, but assuming we have existing '[email protected]', so this throws error\n# - email exists\n$validator = new SomeValidator();\n$validator->validate($data2);\n```\n\n\n---\n\n\n## Available Validators\n\nThe lists below are available and mostly used by several developers, you could use them as your reference.\n\n| Name | Explanation |\n|--------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| PresenceOf | Validates that a field's value is not null or empty string. |\n| Identical | Validates that a field's value is the same as a specified value |\n| Email | Validates that field contains a valid email format |\n| ExclusionIn | Validates that a value is not within a list of possible values |\n| InclusionIn | Validates that a value is within a list of possible values |\n| Regex | Validates that the value of a field matches a regular expression |\n| StringLength | Validates the length of a string |\n| Between | Validates that a value is between two values |\n| Confirmation | Validates that a value is the same as another present in the data |\n| Url | Validates that field contains a valid URL |\n| CreditCard | Validates a credit card number |\n\n\n---\n\n\n## Custom Validator\n\nYou can create your own validation class to extend more or to suit your needs.\n\n```php\n<?php\n\nuse Phalcon\\Validation\\Message;\nuse Phalcon\\Validation\\Validator;\nuse Phalcon\\Validation\\ValidatorInterface;\n\nclass IpValidator extends Validator implements ValidatorInterface\n{\n /**\n * Executes the validation\n *\n * @param Phalcon\\Validation $validator\n * @param string $attribute\n * @return boolean\n */\n public function validate(Validation $validator, $attribute)\n {\n $value = $validator->getValue($attribute);\n\n if (!filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {\n\n $message = $this->getOption('message');\n if (!$message) {\n $message = 'The IP is not valid';\n }\n\n $validator->appendMessage(new Message($message, $attribute, 'Ip'));\n\n return false;\n }\n\n return true;\n }\n}\n```\n\n***Note:*** always use boolean to indicate that it is validated or not.","updates":[],"order":6,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"56e26e85353e060e00b96828","category":{"sync":{"isSync":false,"url":""},"pages":["56e26e3a8d79b50e0031d8f6","56e26e42353e060e00b96826","56e26e4d95d1c60e00a969db","56e26e555ab0871700957c04","56e26e6f5ab0871700957c06","56e26e7d7d54ba0e004635a3","56e26e85353e060e00b96828"],"title":"Supporting Structure","slug":"supporting-structure","order":3,"from_sync":false,"reference":false,"_id":"56c42826c0c4630d004e86cb","__v":7,"createdAt":"2016-02-17T07:58:30.542Z","project":"56c111095abfe40d00be875a","version":"56c111095abfe40d00be875d"},"createdAt":"2016-03-11T07:06:45.716Z","githubsync":"","parentDoc":null,"project":"56c111095abfe40d00be875a","user":"56c1105874f0b417004baadc","__v":8,"version":{"version":"1.3.0","version_clean":"1.3.0","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["56c1110a5abfe40d00be875e","56c413a254b6030d00ec299d","56c4275048213b1700af6e33","56c42826c0c4630d004e86cb","56c4282cbc41330d009f2607","56c4284ad1f6d91700d3697e","56e271c195d1c60e00a969ee"],"_id":"56c111095abfe40d00be875d","releaseDate":"2016-02-14T23:43:05.566Z","__v":7,"createdAt":"2016-02-14T23:43:05.566Z","project":"56c111095abfe40d00be875a"}}