How to keep a radio input selected in a view in Laravel?

Asked

Viewed 1,022 times

0

How would you keep one radio input selected in my view, I have the following codes:

View:

 <div class="col-md-3"> 
      <div class="form-group">
          <label>Tipo de Conta</label>
          <input type="radio" name="tipo" value="CC"> Conta Corrente
          <input type="radio" name="tipo" value="CP"> Conta Poupança
      </div>
  </div>

Controller:

public function edit($id_conta_bancaria) {
$bancos = Banco::orderBy('nome')->get();
$conta = ContaBancaria::find($id_conta_bancaria);
return view('conta_bancaria.edit')->with(['conta' => $conta, 'bancos' => $bancos]);
}

Model:

namespace App;

use Illuminate\Database\Eloquent\Model;

class ContaBancaria extends Model
{

protected $dates = ['deleted_at'];
protected $table = 'conta_bancaria';
protected $primaryKey = 'id_conta_bancaria';
protected $fillable = ['id_banco', 'agencia', 'conta', 'tipo', 'operacao'];

}

3 answers

2

So that the Laravel can rescue the tipo chosen use the classe Request (namespace Illuminate\Http\Request), as the code to follow:

public function index(\Illuminate\Http\Request $request)
{

    if ($request->has('tipo'))
    {

        $tipo = $request->input('tipo');

    }

}

EDITION: the user did not report the problem correctly, he wanted to create his view through his model or set of information by selecting the type that is written in his database.

Controller

public function edit($id)
{
    if (isset($id) && is_numeric($id)) 
    {        
        $data['conta'] = $model->find($id);
        return view('editor', $data);
    }
}

View

<div class="col-md-3"> 
    <div class="form-group">
        <label>Tipo de Conta</label>
        <p><input type="radio" name="tipo" value="CC" {{ $conta->tipo == 'CC' ? 'checked' : '' }}> Conta Corrente</p>
        <p><input type="radio" name="tipo" value="CP" {{ $conta->tipo == 'CP' ? 'checked' : '' }}> Conta Poupança</p>
   </div>
</div>
  • Sorry John I think I did not elaborate my question well, what I really want is to return with the field marked when editing the "Account Type"

  • 1

    @geekcom you always have to be very specific in your question, to avoid situations like this. Please, as soon as you can, edit your question by adding details, but just be careful not to invalidate the answers already given.

  • @geekcom made the edit so that my answer is correct, regardless of anything, always put the next question broadly, because, your question gives to understand the receipt and not the creation.

1

I recommend using the Laravelcollection/html to generate their formularies, problems like this can be easily solved with the technique Form Model Binding. Since this package is no longer an official dependency of Windows, you should install it via Composer.

In addition to making your code more readable, the values are automatically assigned to your due fields according to the attributes of the "model" attached to the form, for example. This also works automatically with the values that were explicitly assigned to the session, eliminating the need for the use of Input::old('field'). See a simple example:

{!! Form::model(new App\User(), ['action' => 'Auth\AuthController@postRegister']) !!}

    ...

    <div class="col-md-4">
        <div class="form-group">
            {!! Form::label('email', 'E-mail', ['class' => 'control-label']) !!}
            {!! Form::email('email', null, ['class' => 'form-control']) !!}
        </div>
    </div>

    <div class="col-md-4">
        <div class="radio">
            <label>
                {!! Form::radio('gender', 0, $user->gender == 0) !!}
                Homem
            </label>
            <label>
                {!! Form::radio('gender', 1, $user->gender == 1) !!}
                Mulher
            </label>
        </div>
    </div>

    ...

{!! Form::close() !!}

This way, the field for the CSRF token is automatically added, and you can assign the method the same way I assign the class to the form.

{!! Form::model(new App\User(), ['action' => 'Auth\AuthController@postRegister', 'method' => 'post']) !!}
  • That would be a different approach to solving the same problem, good tip.

-3


The general solution was as follows.

View:

<div class="col-md-3"> 
    <div class="form-group">
        <label>Tipo de Conta</label>
        <p><input type="radio" name="tipo" value="CC" {{ $conta->tipo == 'CC' ? 'checked' : '' }}> Conta Corrente</p>
        <p><input type="radio" name="tipo" value="CP" {{ $conta->tipo == 'CP' ? 'checked' : '' }}> Conta Poupança</p>
   </div>
</div>

Controller:

 public function edit($id_conta_bancaria) {
    $bancos = Banco::orderBy('nome')->get();
    $conta = ContaBancaria::find($id_conta_bancaria);
    return view('conta_bancaria.edit')->with(['conta' => $conta, 'bancos' => $bancos]);
}

Model:

class ContaBancaria extends Model
{
protected $table = 'conta_bancaria';
protected $primaryKey = 'id_conta_bancaria';
protected $fillable = ['id_banco', 'agencia', 'conta', 'tipo', 'operacao'];

}

  • Sorry, but this is only part of the solution, where the variable comes from $conta? The way the answer is now only helps if I know your code. Of course it’s something simple, but for many who are starting in Laravel the concept of passing the controller variables to the view is not intuitive, I recommend editing the answer.

  • You’re right, I’m sorry for the failure, I made edits to the answer.

Browser other questions tagged

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