Return form with data after Validator failure

Asked

Viewed 577 times

0

I have a user registration form on Laravel

<form class="form-horizontal" method="post" action="{{url('/usuarios')}}">
    @csrf

    @if(session()->has('message'))
        <div class="callout callout-danger">
            {{ session()->get('message') }}
        </div>
    @endif           

    <div class="box-body">

        <div class="form-group has-feedback {{ $errors->has('tipo') ? 'has-error' : '' }}">
            <label for="tipo" class="col-sm-2 control-label">Tipo</label>
            <div class="col-sm-10">
                <select id="tipo" class="form-control" name="tipo" required>
                    <option value="" selected>--- Escolha um tipo ---</option>
                    @foreach($tipos as $tipo)
                        <option value="{{ $tipo->id }}">{{$tipo->nome}}</option>
                    @endforeach
                </select>
                @if ($errors->has('tipo'))
                    <span class="help-block">
                        <strong>{{ $errors->get('tipo')[0] }}</strong>
                    </span>
                @endif
            </div>
        </div>

        <div class="form-group has-feedback {{ $errors->has('name') ? 'has-error' : '' }}">
            <label for="name" class="col-sm-2 control-label">Usuário</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="name" placeholder="" minlength="4" maxlength="50" required>
                @if ($errors->has('name'))
                    <span class="help-block">
                        <strong>{{ $errors->get('name')[0] }}</strong>
                    </span>
                @endif
            </div>
        </div>

        <div class="form-group has-feedback {{ $errors->has('email') ? 'has-error' : '' }}">
            <label for="email" class="col-sm-2 control-label">Email</label>
            <div class="col-sm-10">
                <input type="emais" class="form-control" name="email" id="email" placeholder="" minlength="7" maxlength="50" required>
                @if ($errors->has('email'))
                    <span class="help-block">
                        <strong>{{ $errors->get('email')[0] }}</strong>
                    </span>
                @endif
            </div>
        </div>

        <div class="form-group has-feedback {{ $errors->has('password') ? 'has-error' : '' }}">
            <label for="password" class="col-sm-2 control-label">Senha</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="password" id="password" placeholder="" minlength="6" maxlength="50" required>
                @if ($errors->has('password'))
                    <span class="help-block">
                        <strong>{{ $errors->get('password')[0] }}</strong>
                    </span>
                @endif
            </div>
        </div>

        <div class="form-group has-feedback {{ $errors->has('cidadao') ? 'has-error' : '' }}">
            <label for="cidadao" class="col-sm-2 control-label">Cidadão</label>
            <div class="col-sm-10">
                <select id="cidadao" class="form-control" name="cidadao" required>
                    <option value="" selected>--- Escolha um cidadão ---</option>
                    @foreach($cidadaos as $cidadao)
                        <option value="{{ $cidadao->id }}">{{$cidadao->nome}}</option>
                    @endforeach
                </select>
                @if ($errors->has('cidadao'))
                    <span class="help-block">
                        <strong>{{ $errors->get('cidadao')[0] }}</strong>
                    </span>
                @endif
            </div>
        </div>

    </div>

    <div class="box-footer">
        <button type="submit" class="btn btn-primary pull-right">Salvar</button>
    </div>

</form>

With this all right, on my controller also, cadastra and the validator works well

public function store(Request $request)
{
    $v = Validator::make($request->all(), [
        // user
        'tipo' => 'required|integer|not_in:--- Escolha um tipo ---',
        'name' => 'required|string|min:4|max:50|unique:users',
        'email' => 'required|string|email|min:7|max:50|unique:users',
        'password' => 'required|string|min:4|max:50',
        'cidadao' => 'required|integer|not_in:--- Escolha um cidadão ---',
    ]);

    if($v->fails()) {
        return back()->with('message', 'Confira os dados informados!')->withErrors($v)->withInput();
    }

    $user = new user;
    $user->name = $request->name;
    $user->email = $request->email;
    $user->password = Hash::make($request->password);
    $user->tipo_id = $request->tipo;
    $user->cidadao_id = $request->cidadao;
    $user->save();
    return redirect('/usuarios')->with('message', 'Cadastro realizado com sucesso!');
}

But I wish that when I fell in back() the form return with previously completed data

I tried some ways I thought, the only one that worked was to put a form on the @if(session()->has('message')) and return of $request filling in the fields, and another form in the @else hence normal

But I believe there must be some more practical way

1 answer

2


Apparently it’s all right, except for one thing missing, old():

In the form values you should put, ex:

<input type="emais" class="form-control" value="{{old('email')}}" name="email" id="email" placeholder="" minlength="7" maxlength="50" required>

For the selects:

<select id="tipo" class="form-control" name="tipo" required>
    <option value="" selected>--- Escolha um tipo ---</option>
    @foreach($tipos as $tipo)
        <option @if(old('tipo') == $tipo->id) selected @endif value="{{ $tipo->id }}">{{$tipo->nome}}</option>
    @endforeach
</select>

old() returns null if there is no withInput() in the answer, or there is no input submitted with the name you enter as argument in old().

DOCS

  • 1

    Thank you so much, you were hitting me trying here

Browser other questions tagged

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