{"id":12254,"date":"2020-03-16T06:59:18","date_gmt":"2020-03-16T06:59:18","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=12254"},"modified":"2021-11-12T06:37:19","modified_gmt":"2021-11-12T06:37:19","slug":"request-validation-using-custom-validator-in-laravel","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/request-validation-using-custom-validator-in-laravel\/","title":{"rendered":"Request Validation using Custom Validator in Laravel."},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">To use the custom validator, first, we have to load it in our controller by writing <strong><em>use Validator<\/em><\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"382\" height=\"166\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/Load-Validator.png\" alt=\"ProductController.php with use Validator line added to load the Laravel Validator\" class=\"wp-image-12278\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/Load-Validator.png 382w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/Load-Validator-300x130.png 300w\" sizes=\"auto, (max-width: 382px) 100vw, 382px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">After that, we have to use <strong>make<\/strong> method of validator in which we pass all the parameters of the request object, in the second parameter we pass our validation rules and, in the third parameter we pass our custom messages. <br>If any <strong>error <\/strong>found in the <strong>object <\/strong>or <strong>parameter <\/strong>then, <strong>fails()<\/strong> catch it and redirects to the same page with all <strong>errors<\/strong> else print the given output. <strong>See below:-<\/strong> <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"520\" height=\"321\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/Validator-controller.png\" alt=\"Validator::make in controller with validation rules, custom messages, fails redirect and Form Submitted output\" class=\"wp-image-12285\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/Validator-controller.png 520w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/03\/Validator-controller-300x185.png 300w\" sizes=\"auto, (max-width: 520px) 100vw, 520px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"> <strong>See the code below:<\/strong>&#8211; <\/h3>\n\n\n\n<script src=\"https:\/\/gist.github.com\/SushantSinghRajput03\/d7504f2fa925bb26518d68294f214c80.js\"><\/script>\n\n\n\n<h1 class=\"has-text-align-center wp-block-heading\">Output<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">See when we provide invalid email then our <em>customised message<\/em> appears or not:- <\/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\n<h1 class=\"has-text-align-center wp-block-heading\">Output<\/h1>\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<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_88614\"><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_66251\"  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_88614\"  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\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>To use the custom validator, first, we have to load it in our controller by writing use Validator. After that, we have to use make method of&#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-12254","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\/12254","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=12254"}],"version-history":[{"count":27,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/12254\/revisions"}],"predecessor-version":[{"id":24894,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/12254\/revisions\/24894"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=12254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=12254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=12254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}