here is what i did when adding custom validation. this is for laravel 5.1
- run
PHP Artisan make:request MyFormValidationRequest file app\Requests\MyFormValidationRequest.php
Here is the initial code:
<?php namespace App\Http\Requests; use App\Http\Requests\Request; class MyFormValidationRequest extends Request { public function authorize() { return true; } public function rules() { return [
IMPORTANT : change the return value of the authorize() method to true if you are not authenticating. this initial value is false. otherwise, you will receive a white page with the "Forbidden" error message.
I added a rule to the rules() function, here is what it looks like
public function rules() { return [ 'activeuntil' => 'today_onwards' ]; }
today_onwards is my new check.
I created a folder called "Services" in the "Application" folder
I created a file called "ValidatorExtended.php" in the App \ Services folder, here is the code below:
<?php namespace App\Services; use Illuminate\Validation\Validator; use Carbon\Carbon; class ValidatorExtended extends Validator { private $_custom_messages = array( "today_onwards" => "The :attribute must be today onwards", ); public function __construct( $translator, $data, $rules, $messages = array(), $customAttributes = array() ) { parent::__construct( $translator, $data, $rules, $messages, $customAttributes ); $this->_set_custom_stuff(); } protected function _set_custom_stuff() {
Note. The validateTodayOnwards method is where you put your logic. the method name should always begin with "verification" and then in the name of the new verification key, which should be in the header,
One more note . Your verification key must be separated by an underscore and all lowercase letters, in this case "today_onwards". An underscore must be placed before all uppercase letters in the method name. Hope I explained it well.
The TodayOnwards method is equivalent to the verification name "today_onwards",
another example, if I created validateOldPassword, your validation key should be "old_password".
I added the code below app\Providers\AppServiceProvider.php inside the boot() method.
Validator::resolver(function($translator, $data, $rules, $messages = array(), $customAttributes = array()) { return new ValidatorExtended($translator, $data, $rules, $messages, $customAttributes); });
Remember to add the library below, one is the Validator class and the other is your own class, which is the " ValidatorExtended ".
use App\Services\ValidatorExtended; use Illuminate\Support\Facades\Validator;
This is what the whole file looks like, [ app\Providers\AppServiceProvider.php ]
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use App\Services\ValidatorExtended; use Illuminate\Support\Facades\Validator; class AppServiceProvider extends ServiceProvider { public function boot() {
What is it. made. You have created your own validation.
In addition, if you want to use it in your controller, below is the code:
class testController extends Controller { public function updatePass(MiscValidation $request){
Instead of using the Request class, you use your own class, which is an extension of the Request class.
Rabb-bit
source share