{"id":42287,"date":"2013-10-28T15:25:27","date_gmt":"2013-10-28T10:25:27","guid":{"rendered":"http:\/\/www.cssauthor.com\/?p=42287"},"modified":"2021-03-16T13:33:05","modified_gmt":"2021-03-16T08:03:05","slug":"php-form-validation-tutorial","status":"publish","type":"post","link":"https:\/\/cssauthor.com\/php-form-validation-tutorial\/","title":{"rendered":"G2VALID \u203a An Easy PHP Form Validation Class"},"content":{"rendered":"<div id=\"cssau-2075324523\" class=\"cssau-before-all-posts cssau-entity-placement\" style=\"margin-bottom: 40px;\" role=\"region\" aria-label=\"Advertisements\"><a href=\"https:\/\/1.envato.market\/LnDkj\" aria-label=\"Ultimate design toolkit\"><img src=\"https:\/\/cdn.cssauthor.com\/wp-content\/uploads\/2024\/11\/Ultimate-design-toolkit.png?strip=all\" alt=\"\"  srcset=\"\/\/cdn.cssauthor.com\/wp-content\/uploads\/2024\/11\/Ultimate-design-toolkit.png?strip=all 1764w, \/\/cdn.cssauthor.com\/wp-content\/uploads\/2024\/11\/Ultimate-design-toolkit-800x631.png?strip=all 800w, \/\/cdn.cssauthor.com\/wp-content\/uploads\/2024\/11\/Ultimate-design-toolkit-1160x915.png?strip=all 1160w, \/\/cdn.cssauthor.com\/wp-content\/uploads\/2024\/11\/Ultimate-design-toolkit-1536x1212.png?strip=all 1536w, \/\/cdn.cssauthor.com\/wp-content\/uploads\/2024\/11\/Ultimate-design-toolkit.png?strip=all&w=176 176w, \/\/cdn.cssauthor.com\/wp-content\/uploads\/2024\/11\/Ultimate-design-toolkit.png?strip=all&w=352 352w, \/\/cdn.cssauthor.com\/wp-content\/uploads\/2024\/11\/Ultimate-design-toolkit.png?strip=all&w=529 529w, \/\/cdn.cssauthor.com\/wp-content\/uploads\/2024\/11\/Ultimate-design-toolkit.png?strip=all&w=705 705w, \/\/cdn.cssauthor.com\/wp-content\/uploads\/2024\/11\/Ultimate-design-toolkit.png?strip=all&w=882 882w, \/\/cdn.cssauthor.com\/wp-content\/uploads\/2024\/11\/Ultimate-design-toolkit.png?strip=all&w=1058 1058w, \/\/cdn.cssauthor.com\/wp-content\/uploads\/2024\/11\/Ultimate-design-toolkit.png?strip=all&w=1234 1234w, \/\/cdn.cssauthor.com\/wp-content\/uploads\/2024\/11\/Ultimate-design-toolkit.png?strip=all&w=1411 1411w, \/\/cdn.cssauthor.com\/wp-content\/uploads\/2024\/11\/Ultimate-design-toolkit.png?strip=all&w=1587 1587w, \/\/cdn.cssauthor.com\/wp-content\/uploads\/2024\/11\/Ultimate-design-toolkit.png?strip=all&w=450 450w\" sizes=\"(max-width: 1764px) 100vw, 1764px\" width=\"1764\" height=\"1392\"   \/><\/a><\/div><p style=\"text-align: justify;\">Are you among the web developers who struggles with form validation like i did ? tired of nesting if statements ? well here is a solution if you are using PHP.<\/p>\n<p style=\"text-align: justify;\">Let me introduce you to <b>G2VALID. <\/b>An Easy PHP Form Validation Class to make your life much easier and saves you plenty of time. This class uses OOP PHP so its much easier to use and manage on your projects.<\/p>\n<p><span class=\"seealso\"><strong>See also :<\/strong><a href=\"https:\/\/cssauthor.com\/designing-a-tablet-friendly-website\/\" target=\"blank\" rel=\"noopener noreferrer\"> Trends to Focus on When Designing a Tablet Friendly Website<\/a><\/span><\/p>\n<p>\/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;|<br \/>\nThis validation class requries php 5.0 onwards and few filtering options require C_TYPE |<br \/>\n<span style=\"font-size: small;\">enabled php configuration. If you are not sure about what this C_TYPE is, contact your |<\/span><br \/>\n<span style=\"font-size: small;\">hosting provider to check if it&#8217;s enabled, view your php info for <b>ctype functions<\/b>. This is usually |<\/span><br \/>\n<span style=\"font-size: small;\">turned on so nothing much to worry. ( tested on hostgator ) \/\/<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/span><\/p>\n<p style=\"text-align: justify;\">Okay so lets see what type validation options are available in the alpha stage for us to use. i&#8217;m going to be include the method names along with these for easy reference.<\/p>\n<pre><code class=\"language-php\">Checks for empty input\n\t|\tisEmpty($param) \t\t|\t(@param | String : custom error message)\nChecks for valid email\n\t|\tvalidEmail($param) \t|\t(@param | String : custom error message)\nChecks for valid URL according to http:\/\/www.faqs.org\/rfcs\/rfc2396\n\t|\tvalidURL($param) \t\t|\t(@param | String : custom error message)\nChecks if the $field is the same as the instace field ( used for password field matching )\n\t|\tisSame($field,$error)\t\n\t|\t(@$field  | String : field to be compared with)\t|\t (@error   | String : custom error message)\n\t| \tEx Use : -&amp;gt;field('passwd')-&amp;gt;isSame('passConfirm','Didnt Match');\nChecks for valid Ip Address\n\t|\tvalidIP($param) \t\t|\t(@param : custom error message)\nCheck for Minimum Length of a string\n\t|\tminLength($min,$error)\n\t|\t($min     | int : the minimum string size)\n\t|\t (@error   | String : custom error message)\nCheck for Maximum Length of a string\n\t|\tmaxLength($max,$error)\n\t|\t($max     | int : the minimum string size)\n\t|\t(@error   | String : custom error message)\nChecks for A-Za-z and space\n\t|\talpha($param)  \t\t|\t(@param | String : custom error message)\nChecks for A-Za-z 0-9\n\t|\talphanumeric($param) \t|\t(@param | String : custom error message)\nChecks for an valid int\n\t|\tisInt($param)\t\t|\t(@param | String : custom error message)\nChecks for an valid float\n\t|\tisFloat($param)\t\t|\t(@param | String : custom error message) \n<\/code><\/pre>\n<p style=\"text-align: justify;\">Now that you know what options available for use, lets simulate a simple form validation.<\/p>\n<p style=\"text-align: justify;\">See, its simple as that and you are good to go. . please also note that <b>sendEmail(); <\/b>function used their is for dummy purposes. you can replace it with what ever code you want. The demo above only displayed few possibilities of the the class, lets dig in deeper to see whats available for us to use<\/p>\n<p style=\"text-align: justify;\">Now that you know how easy it is to validate a form, lets dig in deeper to find out what types of reporting is avalible for validations.<\/p>\n<h2>1- Get all Errors of a field as array (@return array)<\/h2>\n<p style=\"text-align: justify;\">This wil return all the errors of a specific field as an array when more than 1 validation rule is set<\/p>\n<pre><code class=\"language-php\">Validation  : $instance-&amp;gt;field('username')-&amp;gt;isEmpty()-&amp;gt;minLength(5)-&amp;gt;maxLength(10);\nGet Errors  : $instance-&amp;gt;&amp;gt;field('username')-&amp;gt;getErrorsAsArray()\n<\/code><\/pre>\n<h2>2- Get Error of a field (@return string)<\/h2>\n<p style=\"text-align: justify;\">Get the error of a field, if multiple validation rules are set, the 1st error occurrence will be returned and also note that by setting getError(true) will return a detailed output with the error message and field name.<\/p>\n<pre><code class=\"language-php\">Validation  : $instance-&amp;gt;field('username')-&amp;gt;isEmpty('Enter a username');\nGet Errors  : $instance-&amp;gt;&amp;gt;field('username')-&amp;gt;getError()\n<\/code><\/pre>\n<h2>3- Get all Errors of a field as a string (@return string)<\/h2>\n<p style=\"text-align: justify;\">This wil return all the errors of a specific field as a string if multiple validation rules are set, also note that by setting getErrorAsString(true) will output the field names along with the error<\/p>\n<pre><code class=\"language-php\">Validation  : $instance-&amp;gt;field('website')-&amp;gt;isEmpty()-&amp;gt;validURL();\nGet Errors  : $instance-&amp;gt;&amp;gt;field('website')-&amp;gt;getErrorAsString()\n<\/code><\/pre>\n<h2>4- Get all Errors of the form or instance (@return array)<\/h2>\n<p style=\"text-align: justify;\">This might be good for debugging or if you plan to take validation to the next level, this will return all the errors of the instance as an array<\/p>\n<pre><code class=\"language-php\">Validation\n----------\n$instance-&amp;gt;field('website')-&amp;gt;isEmpty('Web site is required')-&amp;gt;validURL('Enter Valid Website');\n$instance-&amp;gt;field('username')-&amp;gt;isEmpty('username is required')-&amp;gt;minLength(5)-&amp;gt;maxLength(10);\n$instance-&amp;gt;field('email')-&amp;gt;isEmpty('email is required')-&amp;gt;validEmail('Enter Valid Email');\n\nAll errors are stored in the object it self\n-----------------------------------------------------\n$instance;\n\n\/\/ To simply inspect, you can use\nprint_r($instance);\n<\/code><\/pre>\n<h2>5- Get all Errors of the form or instance (@return string)<\/h2>\n<p style=\"text-align: justify;\">This is very useful if you want to report all the errors of the form at once as a string,It&#8217;s always easy right ?. if you haven&#8217;t set custom error messages, pass the additional parameter <b>true <\/b>to turn on detailed reporting like in example below<\/p>\n<pre><code class=\"language-php\">Validation\n----------\n$instance-&amp;gt;field('website')-&amp;gt;isEmpty()-&amp;gt;validURL();\n$instance-&amp;gt;field('username')-&amp;gt;isEmpty()-&amp;gt;minLength(5)-&amp;gt;maxLength(10);\n$instance-&amp;gt;field('email')-&amp;gt;isEmpty()-&amp;gt;validEmail();\n\nAll errors of instance as a string with detaild reporting\n------------------------------------------------------------------------\necho $instance-&amp;gt;getErrorAsString(true);\n<\/code><\/pre>\n<p style=\"text-align: justify;\">Now in some of the forms i have set custom field names, but in some cases i haven&#8217;t, in order to make sure the user gets a better understanding of whats going on, you can always turn on detailed reporting for each method if your aren&#8217;t specifying custom error messages.<\/p>\n<p style=\"text-align: justify;\">You can do this by adding an extra parameter (<b>true<\/b>) to error reporting methods to get an detailed report.<\/p>\n<pre><code class=\"language-php\">getErrorAsString(true);\ngetError(true);\n<\/code><\/pre>\n<p style=\"text-align: justify;\">Did you expect this amount of reporting options ? i guess you never did but here you go. Now you know that weather its get or post, you can validate the form. But do you only require validations for forms ? you often grab data from databases, external API&#8217;s and etc&#8230; We know its not only the form fields ( GET &amp; POST ) which should be validated, other stuff exists too. Don&#8217;t worry, G2 Valid has just the right options for you. So lets see how we can dynamically Do Stuff by creating a dynamic field, this code will be a continuation from the <b>basic demo [<a title=\"G2Valid\" href=\"https:\/\/github.com\/dsginvilla\/G2Valid#basic-demo\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/dsginvilla\/G2Valid#basic-demo<\/a>] <\/b>So lets begin by creating a new field<\/p>\n<pre><code class=\"language-php\">Adding the dynamic field by using addField() method\n$validator-&amp;gt;addField('db_id');\n\n\/* \n\tWe use setValue() method to add or change values of an exisitng field.\n\t$db-&amp;gt;getId()  | this is for demonstration purposes only, lets imagine this extracts an\n\tID of a record form the database based on the session username or something.\n*\/\n$validator-&amp;gt;field('db_id')-&amp;gt;setValue($db-&amp;gt;getId());\n\n\/* \n\tWithout using multiple lines, you can do it in a single line, fancy right ?\n*\/\n$validator-&amp;gt;addField('db_id')-&amp;gt;setValue($db-&amp;gt;getId())-&amp;gt;isEmpty('No Db record')-&amp;gt;isInt('Invalid id');\n<\/code><\/pre>\n<p style=\"text-align: justify;\">Now that you have seen what this can do, i&#8217;m ready to show you a complete demo, not just dummy code but a working solution. you are welcome to download the sample project and check out the source ;-)<\/p>\n<p style=\"text-align: left;\"><a class=\"btn-source\" href=\"http:\/\/goo.gl\/HgbFYu\" target=\"blank\" rel=\"noopener noreferrer\"> Download<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you among the web developers who struggles with form validation like i did ? tired of nesting if statements ? well here is a solution if you are using PHP. Let me introduce you to G2VALID. An Easy PHP Form Validation Class to make your life much easier and saves you plenty of time. [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":210642,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[893],"tags":[],"class_list":["post-42287","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/cssauthor.com\/wp-json\/wp\/v2\/posts\/42287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cssauthor.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssauthor.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssauthor.com\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/cssauthor.com\/wp-json\/wp\/v2\/comments?post=42287"}],"version-history":[{"count":0,"href":"https:\/\/cssauthor.com\/wp-json\/wp\/v2\/posts\/42287\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cssauthor.com\/wp-json\/wp\/v2\/media\/210642"}],"wp:attachment":[{"href":"https:\/\/cssauthor.com\/wp-json\/wp\/v2\/media?parent=42287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssauthor.com\/wp-json\/wp\/v2\/categories?post=42287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssauthor.com\/wp-json\/wp\/v2\/tags?post=42287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}