In this Laravel Validator, what is the right code with variable?

Asked

Viewed 43 times

0

In this Validator of Laravel, what is the right code with variable?

public function salvar()
{
    $n =Request::input('campos');
    for($i=1; $i < $n; $i++){

    $pontosa =Request::input('pontosa'.$i);
    $pontosb =Request::input('pontosb'.$i);
    $validator = Validator::make(
        [ "'pontosa'.$i" => $pontosa,
          "'pontosb'.$i" => $pontosb
        ],
        [
          'pontosa.$i' => 'required|numeric',
          'pontosb.$i' => 'required|numeric'
        ]);

if($validator->fails())
{
  return redirect()
             ->action('UsersController@lista');

1 answer

1

I wear it like this (part of the login code):

public function logar(Request $request)
{

    $credenciais = $request->only('email', 'senha');
    $validator = Validator::make($credenciais, [
        'email' => 'required',
        'senha' => 'required',
    ]);

    if($validator->fails()) 
    {
        return response()->json([
            'error' => true, 
            'mensagem'=> 'Formato de dados inválidos', 
            'data'=> $credenciais
        ], 401);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.