{"id":12145,"date":"2020-03-14T06:29:34","date_gmt":"2020-03-14T06:29:34","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=12145"},"modified":"2021-11-12T06:39:56","modified_gmt":"2021-11-12T06:39:56","slug":"request-validation-using-validatesrequest-trait-in-laravel","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/request-validation-using-validatesrequest-trait-in-laravel\/","title":{"rendered":"Request Validation using ValidatesRequest Trait in Laravel."},"content":{"rendered":"\n<h2 class=\"has-vivid-red-color has-text-color wp-block-heading\">Q1. What is Validation?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Validation is used to protect or validate the request we&#8217;re getting from the user. Suppose we make a form and we&#8217;ve not used validation in the form so when a user submits a blank form then it might give error or exception and also we get blank data from the submitted form, so we use validation to protect the request. For example, we&#8217;ve added a field to get an email of the users and we want to get unique email id whenever user fill the field, so we use email validation in our page to protect the request. <\/p>\n\n\n\n<h2 class=\"has-vivid-red-color has-text-color wp-block-heading\">Q2. How to Validate the Request?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To validate the request, we use a form to get input and validate it. At first, we make a simple HTML form to get input and it is a view file so we name it <strong>form.blade.php<\/strong>. See the below code of the form:-<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/SushantSinghRajput03\/11b9ea13ddb660dcc5c0d28305026609.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\">In the form, we used an available object of message bank called <strong>error<\/strong> to pass the message in the form. It is predefined in all the view file. After that, we used the csrf token in the form and then our form code. In the code we also used value as <strong>old<\/strong>. <strong>why ?<\/strong>. To <strong>preserve <\/strong>the value we filled in the given field we use value as <strong>old<\/strong>. When we press the submit button then it calls the  <strong>submitmyform<\/strong> page to give output.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To set the customise message we pass it as a 3rd parameter. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, we make function of our <strong>form <\/strong>as well as <strong>submitmyform<\/strong> in our controller, see below:- <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"533\" height=\"221\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/request-validation-controller.png\" alt=\"Laravel controller code using $this-&gt;validate to require name, email and age, with a custom name message\" class=\"wp-image-12158\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/request-validation-controller.png 533w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/request-validation-controller-300x124.png 300w\" sizes=\"auto, (max-width: 533px) 100vw, 533px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Now we have to make the <strong>Route<\/strong>. But careful when making the route of <strong>submitmyform<\/strong> because the method is <strong>post <\/strong>for submitmyform so we write <strong>post <\/strong>in place of <strong>get<\/strong>. See the route below:-<\/p>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\"><em>Route::<strong>get<\/strong>(&#8220;myform&#8221;, &#8220;ProductController@myform&#8221;);<br>Route::<strong>post<\/strong>(&#8220;submitmyform&#8221;, &#8220;ProductController@submitmyform&#8221;);<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now See the output whether it returns a message when we did not provide value in the given field.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/Form-output.png\" alt=\"Empty form submitted, showing required-field errors for name, email and age\" class=\"wp-image-12178\" width=\"616\" height=\"308\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/Form-output.png 600w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/Form-output-300x150.png 300w\" sizes=\"auto, (max-width: 616px) 100vw, 616px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Now, When we fill 1-2 field and left other then what message returns. See below:- <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"248\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/fill-output.png\" alt=\"Form with age left blank returns The age field is required and keeps the filled values\" class=\"wp-image-12187\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/fill-output.png 600w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/fill-output-300x124.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When we fill all the fields, then what returns:- <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"145\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/All-filled-output.png\" alt=\"All form fields filled and submitted, so submitmyform shows Form Submitted\" class=\"wp-image-12192\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/All-filled-output.png 600w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/All-filled-output-300x73.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to get valid Email then add the below code in the controller. See below:-<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"415\" height=\"185\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/Valid-email-code.png\" alt=\"Laravel submitmyform validate code with email rule required|email|min:8 and custom Enter a Valid E-mail message\" class=\"wp-image-12195\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/Valid-email-code.png 415w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/Valid-email-code-300x134.png 300w\" sizes=\"auto, (max-width: 415px) 100vw, 415px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">See when we provide invalid email then what message appears:- <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"578\" height=\"234\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/Valid-email-output.png\" alt=\"Laravel form submitted with invalid email dgdcmkduid returns the Enter a Valid E-mail message\" class=\"wp-image-12200\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/Valid-email-output.png 578w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/Valid-email-output-300x121.png 300w\" sizes=\"auto, (max-width: 578px) 100vw, 578px\" \/><\/figure><\/div>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_88721\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_86254\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_88721\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Q1. What is Validation? Validation is used to protect or validate the request we&#8217;re getting from the user. Suppose we make a form and we&#8217;ve not used&#8230; <\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5201],"tags":[26327],"class_list":["post-12145","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-laravel-validation"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/12145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=12145"}],"version-history":[{"count":38,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/12145\/revisions"}],"predecessor-version":[{"id":24897,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/12145\/revisions\/24897"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=12145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=12145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=12145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}