{"id":3191,"date":"2020-08-27T04:27:52","date_gmt":"2020-08-27T04:27:52","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=3191"},"modified":"2021-01-20T04:59:59","modified_gmt":"2021-01-20T04:59:59","slug":"moodle-form-validation","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/moodle-form-validation\/","title":{"rendered":"Moodle Form Validation"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>Forms are a great way to gather information from visitors and allow them to submit information to your website, for example, <strong>Submit feedback<\/strong> to the website. Mostly, we want to ensure that customers don&#8217;t leave specific form fields blank, or we want to limit a field to a certain type of input such as characters, numbers, email addresses, etc. At this time we need to validate form fields. If we validate form input fields before the data is sent to the database or website, then the visitor gets a chance to correct the data. The goal of <strong>Form Validation<\/strong> is to ensure that the user provides necessary and properly formatted information needed to complete the form submission.<br \/>\nAs we all know, Moodle has its own Forms library which has lots of accessibility code and error checking by default. <strong>Form API<\/strong> is used to create a form. When we create the form by using this <strong>Form API<\/strong> we need to include <strong>formslib.php<\/strong>. So we have two ways to add Form Validation in the <strong>forms lib<\/strong>. These are explained as follows:<\/p>\n<ol>\n<li>MoodleQuickForm::addRule()<\/li>\n<li>moodleform::validation()<\/li>\n<\/ol>\n<h3>MoodleQuickForm::addRule()<\/h3>\n<p><strong>Moodle Quick Forms<\/strong> are standard and most used method for form creation. This is the easiest method to apply validation on moodle form. We use this inside the moodleform<strong>::definition<\/strong> method. First, we add an element in form after that we can apply <strong>addRule()<\/strong> for validation. There are some common rule types that we use via this method like required, maxlength, minlength, email, regex, numeric, etc. Below is the syntax to use <strong>addRule()<\/strong>. <\/p>\n<p><code><br \/>\n$mform->addRule('elementname', get_string('error'), 'rule type', 'extraruledata', 'client\u2019);<br \/>\n<\/code><\/p>\n<p>Now we are going to describe the syntax:<\/p>\n<ol>\n<li>The first param <strong>\u2018elementname\u2019<\/strong> is an element name.<\/li>\n<li>The second <strong>\u2018get_string(&#8216;error&#8217;)\u2019<\/strong> is the error message that will be displayed to the user.<\/li>\n<li>The third parameter <strong>\u2018rule type\u2019<\/strong> is a type of rule like (required, minlength, etc).<\/li>\n<li>The fourth param <strong>\u2018extraruledata\u2019<\/strong> is used for extra data needed with some rules such as minlength(7), regex(a-z), etc. <\/li>\n<li>The fifth parameter <strong>\u2018client\u2019<\/strong> validates input data on the server or client-side, if validation is done on the client-side then it will be checked on the server-side as well. This is an optional parameter.<\/li>\n<\/ol>\n<p>Now here is the example of fullname field with required and maxlength rules :<br \/>\n<code><br \/>\nrequire_once(\"$CFG->libdir\/formslib.php\");<br \/>\n      class edit_form extends moodleform {<br \/>\n      \/\/Add elements to form<br \/>\n       public function definition() {<br \/>\n              global $CFG;<br \/>\n        $mform = $this->_form; \/\/ Don't forget the underscore!<br \/>\n        $mform->addElement('text','fullname',     get_string('fullnamecourse'), ['size' => 50]);<br \/>\n        $mform->addHelpButton('fullname', 'fullnamecourse');<br \/>\n        $mform->addRule('fullname', get_string('maximumchars', '', 1333), 'maxlength', 1333); \/\/maxlength rule applied<br \/>\n        $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client'); \/\/required rule applied<br \/>\n       $mform->setType('fullname', PARAM_TEXT); \/\/Set type of element<\/p>\n<p>    }<br \/>\n}<br \/>\n<\/code><\/p>\n<h3>moodleform::validation()<\/h3>\n<p>This is another method to apply validation on moodle form. This is commonly used for custom validation. When we create a form by extending <strong>moodleform <\/strong>class, we need to define a method <strong>validation()<\/strong> to make our own custom validation for the form. After that, we need to call the moodleform <strong>parent validation()<\/strong> method. Then we will apply our own rules. As a result, this will return an array of <strong>\u2018errors\u2019<\/strong> or <strong>\u2018null\u2019<\/strong>. Here is the syntax:<\/p>\n<p><code><br \/>\nclass edit_form extends moodleform {<br \/>\n    \/\/ Perform some extra moodle validation<br \/>\n    function validation($data, $files) {<br \/>\n        $errors = parent::validation($data, $files);  \/\/ call parent validation function<br \/>\n          \/\/ then apply your own rules<br \/>\n         return $errors;<br \/>\n      }<br \/>\n }<br \/>\n<\/code><\/p>\n<p>Now here is the example of full name field already existing rule :<\/p>\n<p><code><br \/>\nclass edit_form extends moodleform {<br \/>\n    function definition() {<br \/>\n         \/\/ form creation code go here;<br \/>\n    }<\/p>\n<p>    \/\/ Perform some extra moodle validation<br \/>\n    function validation($data, $files) {<br \/>\n        $errors = parent::validation($data, $files);<br \/>\n        global $DB;<br \/>\n       \/\/ Add field validation check for duplicate fullname.<br \/>\n        if ($course = $DB->get_record('course', array('fullname'=> $data['fullname'), '*', IGNORE_MULTIPLE)) {<br \/>\n            if (empty($data['id']) || $course->id != $data['id']) {<br \/>\n                $errors['fullname'] = get_string('fullname'taken, '', $course->fullname);<br \/>\n            }<br \/>\n        }<br \/>\n              return $errors;<br \/>\n    }<br \/>\n}<\/p>\n<p><\/code><\/p>\n<p><small><em>If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level <a href=\"https:\/\/www.studysection.com\/php-web-developer-advanced-diploma\">PHP Certification Exams<\/a> are offered by StudySection along with other programming certification exams.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Forms are a great way to gather information from visitors and allow them to submit information to your website,<\/p>\n","protected":false},"author":1,"featured_media":3193,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[528,527],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>StudySection Blog - Moodle (CMS) Form Validation<\/title>\n<meta name=\"description\" content=\"As we all know, Moodle has its own Forms library present in it which has lots of accessibility code and error checking by default.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/moodle-form-validation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"StudySection Blog - Moodle (CMS) Form Validation\" \/>\n<meta property=\"og:description\" content=\"As we all know, Moodle has its own Forms library present in it which has lots of accessibility code and error checking by default.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/moodle-form-validation\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-27T04:27:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-20T04:59:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/08\/moodle.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/moodle-form-validation\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/moodle-form-validation\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Moodle Form Validation\",\"datePublished\":\"2020-08-27T04:27:52+00:00\",\"dateModified\":\"2021-01-20T04:59:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/moodle-form-validation\/\"},\"wordCount\":513,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"Moodle\",\"Validation\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/moodle-form-validation\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/moodle-form-validation\/\",\"url\":\"https:\/\/studysection.com\/blog\/moodle-form-validation\/\",\"name\":\"StudySection Blog - Moodle (CMS) Form Validation\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2020-08-27T04:27:52+00:00\",\"dateModified\":\"2021-01-20T04:59:59+00:00\",\"description\":\"As we all know, Moodle has its own Forms library present in it which has lots of accessibility code and error checking by default.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/moodle-form-validation\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/moodle-form-validation\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/moodle-form-validation\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Moodle Form Validation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"StudySection Blog - Moodle (CMS) Form Validation","description":"As we all know, Moodle has its own Forms library present in it which has lots of accessibility code and error checking by default.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/moodle-form-validation\/","og_locale":"en_US","og_type":"article","og_title":"StudySection Blog - Moodle (CMS) Form Validation","og_description":"As we all know, Moodle has its own Forms library present in it which has lots of accessibility code and error checking by default.","og_url":"https:\/\/studysection.com\/blog\/moodle-form-validation\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2020-08-27T04:27:52+00:00","article_modified_time":"2021-01-20T04:59:59+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/08\/moodle.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/moodle-form-validation\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/moodle-form-validation\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Moodle Form Validation","datePublished":"2020-08-27T04:27:52+00:00","dateModified":"2021-01-20T04:59:59+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/moodle-form-validation\/"},"wordCount":513,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["Moodle","Validation"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/moodle-form-validation\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/moodle-form-validation\/","url":"https:\/\/studysection.com\/blog\/moodle-form-validation\/","name":"StudySection Blog - Moodle (CMS) Form Validation","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2020-08-27T04:27:52+00:00","dateModified":"2021-01-20T04:59:59+00:00","description":"As we all know, Moodle has its own Forms library present in it which has lots of accessibility code and error checking by default.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/moodle-form-validation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/moodle-form-validation\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/moodle-form-validation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Moodle Form Validation"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":1487,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3191"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=3191"}],"version-history":[{"count":2,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3191\/revisions"}],"predecessor-version":[{"id":3860,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3191\/revisions\/3860"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/3193"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=3191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=3191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=3191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}