Support the ongoing development of Laravel.io →
Database Eloquent Validation
Last updated 2 years ago.
0

You can create custom validation rule. You can do that within service provider, e.g. in AppServiceProvider :

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Carbon\Carbon;
use Validator;

class AppServiceProvider extends ServiceProvider
{

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Validator::extend('age_match', function($attribute, $value, $parameters, $validator) {
            $camper = \App\Models\Camper::find($parameters[0]);
            $camp   = \App\Models\Camp::find($parameters[1]);

            $now = Carbon::now();
            $age = $now->diffInYears($camper->birthdate);

            return $age >= $camp->min_age && $age <= $camp->max_age;
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {        
    }
}

Latter, validation is something like:

....
$a = $request->all();
$validator = Validator::make($a, [
        'camp_id' => 'required|numeric',
        'camper_id' => 'required|numeric|age_match:' .$a['camper_id'] . ',' . $a['camp_id'],
]);
...
Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

elikmiller elikmiller Joined 11 May 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.