0
I have a Request that validates through a class Request
:
The Controller:
public function store(PagtoRequest $request)
{
... resto da lógica
A Request:
namespace App\Http\Requests;
use App\Rules\ValidarValor;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
class PagtoRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules(Request $request)
{
$arrayParcelas = $request->input("arrayParcelas"); // guardando no dados das percelas
return [
'arrayParcelas.*.vcto_parcela' => ['required','date_format:"d/m/Y"'], // verifica se as datas informadas são válidas
'arrayParcelas.*.valor_parcela' => ['required',new ValidarValor], // verifica se os valores informados são válidos
];
}
public function messages() {
return [
'arrayParcelas.*.vcto_parcela.date_format' => 'Existem parcelas com datas inválidas',
'arrayParcelas.*.valor_parcela.ValidarValor' => 'Existem parcelas com valores inválidos',
];
}
}
A Rule:
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class ValidarValor implements Rule
{
public function __construct()
{
}
public function passes($attribute, $value)
{
// verifica se está no formato 9.999.999.999,99 e quantos milhares forem necessários.
$expressao = "/^([1-9]{1}[\d]{0,2}(\.[\d]{3})*(\,[\d]{0,2})?|[1-9]{1}[\d]{0,}(\,[\d]{0,2})?|0(\,[\d]{0,2})?|(\,[\d]{1,2})?)$/";
return (preg_match($expressao,$value));
}
public function message()
{
return "O campo :attribute está no formato incorreto.";
}
}
The first rule, I can customize the error message because I use an Laravel validation rule (in the case of field validation vcto_parcela
)
But in the case where I use the Rule
ValidarValor
, how to personalize the message?
The way I did, it doesn’t work because it’s returning to Rule’s message instead of the message I set in Request.
That is, (exemplifying if one of the portions is in incorrect format) is returning this message (coming from Rule
):
O campo arrayParcelas.2.valor_parcela está no formato incorreto.
Instead of this, which I set in Request
:
Existem parcelas com valores inválidos.
Of course, that’s because this isn’t right:
'arrayParcelas.*.valor_parcela.ValidarValor' => 'Existem parcelas com valores inválidos',
But how to do so to display the error message I set, when I use a Rule?
It does not deserve a 'no' vote, another 'no' vote that has not been adopted
– novic
Post the entire request @
– novic
removes Request from the method
public function rules(Request $request)
leaving as the standardpublic function rules()
?– novic
I edited the question with all the information
– bur