Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda