{"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>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>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>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>To set the customise message we pass it as a 3rd parameter. <\/p>\n\n\n\n<p>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=\"\" 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>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\"><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>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=\"\" 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>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=\"\" 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>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=\"\" 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>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=\"\" 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>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=\"\" 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_84515\"><iframe loading=\"lazy\"  id=\"_ytid_85939\"  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_84515\"  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 class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>  <\/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 validation in the&#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":[5203],"class_list":["post-12145","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-laravel"],"_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}]}}