How Add Custom Validation Rules When Using Form Request Validation in Laravel 5

How add Custom Validation Rules when using Form Request Validation in Laravel 5

Using Validator::extend() like you do is actually perfectly fine you just need to put that in a Service Provider like this:

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ValidatorServiceProvider extends ServiceProvider {

public function boot()
{
$this->app['validator']->extend('numericarray', function ($attribute, $value, $parameters)
{
foreach ($value as $v) {
if (!is_int($v)) {
return false;
}
}
return true;
});
}

public function register()
{
//
}
}

Then register the provider by adding it to the list in config/app.php:

'providers' => [
// Other Service Providers

'App\Providers\ValidatorServiceProvider',
],

You now can use the numericarray validation rule everywhere you want

Laravel 5.5 use custom validation rule in validation request file?

Rutvij Kothari answered the question in the comments.

It seems you are validating string with a regular expression, the same logic can be achieved by regex buit-in validation method. Check it out. laravel.com/docs/5.5/validation#rule-regex No need to create your own validation rule. – Rutvij Kothari

If you want to use your validation pass it into an array. like this. 'email' => ['required', 'email', new employeemail]

Laravel Form Request validation with custom validation Rule, didn't work to find all space characters

@foo.ar,

In App\Http\Kernel.php, you can see TrimStrings middleware is applied. it will trim all white spaces. if u enter only spaces in any input, it will trim those spaces and u will end up with no values in that input.

so if u want to validate spaces, u have to add the below code in

\App\Http\Middleware\TrimStrings.php

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\TrimStrings as Middleware;

class TrimStrings extends Middleware
{
/**
* The names of the attributes that should not be trimmed.
*
* @var array
*/
protected $except = [
'password',
'password_confirmation',
];
}

Laravel form request validation on store and update use same validation

Ok.. i can do it like @porloscerros Ψ suggest

    public function rules()
{
$rules = [
'name' => 'required|string|unique:products|max:255',
];

if (in_array($this->method(), ['PUT', 'PATCH'])) {
$product = $this->route()->parameter('product');

$rules['name'] = [
'required',
'string',
'max:255',
Rule::unique('loan_products')->ignore($product),
];
}

return $rules;
}

How do I add a custom validator in my FormRequest?

It is possible to have your custom validation method by creating a validator property to your class and setting it to app('validator'). You can then with that property run extend, just like with the facade.

Create a __construct method and add this:

public function __construct() {
$this->validator = app('validator');

$this->validateFoobar($this->validator);
}

Then create a new method called validateFoobar that take the validator property as the first argument and run extend on that, just like with the facade.

public function validateFoobar($validator) {
$validator->extend('foobar', function($attribute, $value, $parameters) {
return ! MyModel::where('foobar', $value)->exists();
});
}

More detail about extend is available here.

In the end, your FormRequest could look like this:

<?php namespace App\Http\Requests;

use App\Models\MyModel;
use App\Illuminate\Foundation\Http\FormRequest;

class MyFormRequest extends FormRequest {
public function __construct() {
$this->validator = app('validator');

$this->validateFoobar($this->validator);
}

public function rules() {
return [
'id' => ['required', 'foobar']
];
}

public function messages() {
return [
'id.required' => 'You have to have an ID.',
'id.foobar' => 'You have to set the foobar value.'
];
}

public function authorize() { return true; }

public function validateFoobar($validator) {
$validator->extend('foobar', function($attribute, $value, $parameters) {
return ! MyModel::where('category_id', $value)->exists();
});
}
}


Related Topics



Leave a reply



Submit