Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

396
Vistas
Give name to custom validation Rule - Laravel 8

Laravel 8 makes it possible to create custom Validation rules: https://laravel.com/docs/8.x/validation#custom-validation-rules

php artisan make:rule Euro

But then you have to pass the rule as a object (instead of a string):

new Euro

instead of the regular string notation

'required|euro'

Is there anyway to "register" the new Rule classes to a string identifier and use them like you can you the pre-existing rules?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can use the extend function on the validator. Probably something like:

Validator::extend('euro', new Euro());

This code should be in your AppServiceProvider.

over 4 years ago · Santiago Trujillo Denunciar

0

You can do it the following way.

Create two additional methods in your rule, besides the default ones (passes(), message()):

  • handle() -- This one will return your rule's string handle. It's sole purpose is to keep track of that handle in a single place.
  • validate() -- That's the one which will be invoked by the validator. This should essentially be just a pass through to passes() so you can keep your validation logic in one place. Additionally you should pass the message into the validator. In most cases you also want to keep the message logic in one place.

use Illuminate\Validation\Validator;

public static function handle(): string
{
    return 'euro';
}


public function validate(string $attribute, $value, $params, Validator $validator): bool
{
    $handle = $this->handle();


    $validator->setCustomMessages([
        $handle => $this->message(),
    ]);

    return $this->passes($attribute, $value);
}

Register your class using the Validator facade or the factory in a Service Provider's boot() method:


namespace App\Providers;

use App\Rules\Euro;
use Illuminate\Contracts\Validation\Factory as ValidatorFactory;
use Illuminate\Support\ServiceProvider;

class ValidationServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot(ValidatorFactory $validator)
    {
        $validator->extend(Euro::handle(), Euro::class);
    }
}

That's it. Don't forget to register your service provider if you created a dedicated one.

over 4 years ago · Santiago Trujillo Denunciar

0

This solution work for me.

php artisan make:rule NoLink

App\Rules\NoLink.php

<?php
namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Illuminate\Validation\Validator;

class NoLink implements Rule
{
    // ...

    public function message()
    {
        return __('validation.' . self::handle());
    }

    public static function handle(): string
    {
        return 'no_link';
    }

    public function validate(string $attribute, $value, $params, Validator $validator): bool
    {
        $handle = $this->handle();

        $validator->setCustomMessages([
            $handle => $this->message(),
        ]);

        return $this->passes($attribute, $value);
    }
}

App\Providers\AppServiceProvider.php

    public function boot()
    {
        // ...
        Validator::extend(NoLink::handle(), NoLink::class);
    }
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda