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

224
Vistas
Lumen provide a code for validation errors

Currently in lumen when you use the $this->validate($request, $rules) function inside of a controller it will throw a ValidationException with error for your validation rules(if any fail of course).

However, I need to have a code for every validation rule. We can set custom messages for rules, but I need to add a unique code.

I know there's a "formatErrorsUsing" function, where you can pass a formatter. But the data returned by the passed argument to it, has already dropped the names of the rules that failed, and replaced them with their messages. I of course don't want to string check the message to determine the code that should go there.

I considered setting the message of all rules to be "CODE|This is the message" and parsing out the code, but this feels like a very hacked solution. There has to be a cleaner way right?

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

0

I've solved this for now with the following solution:

private function ruleToCode($rule) {
    $map = [
        'Required' => 1001,
    ];

    if(isset($map[$rule])) {
        return $map[$rule];
    }

    return $rule;
}

public function formatValidationErrors(Validator $validator) {
    $errors = [];

    foreach($validator->failed() as $field => $failed) {
        foreach($failed as $rule => $params) {
            $errors[] = [
                'code' => $this->ruleToCode($rule),
                'field' => $field,
            ];
        }
    }

    return $errors;
}
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