Patrick Maciel
Patrick Maciel

Reputation: 4944

Radio button with same id in Laravel 4.1.x

I have this code:

<div class="form-group  {{ (isset($errors) AND $errors->has('indicacao_interna')) ? 'has-error' : '' }}">
  {{ Form::label('indicacao_interna', 'Indicação Interna', array('class' => 'col-lg-4 control-label')) }}
  <div class="col-lg-3">
    <div>

      <label class='radio-inline'>{{ Form::radio('indicacao_interna', 1, oldRadio('indicacao_interna', 1, true)) }} Sim</label>
      <label class='radio-inline'>{{ Form::radio('indicacao_interna', 0, oldRadio('indicacao_interna', 0, true)) }} Não</label>
      @include('partials.validators.message_field', array('field' => 'indicacao_interna'))
    </div>
  </div>
</div>

And it generates this code:

<div class="form-group  ">
  <label for="indicacao_interna" class="col-lg-4 control-label">Indicação Interna</label>                <div class="col-lg-3">
  <div>

    <label class="radio-inline"><input checked="checked" name="indicacao_interna" type="radio" value="1" id="indicacao_interna"> Sim</label>
    <label class="radio-inline"><input name="indicacao_interna" type="radio" value="0" id="indicacao_interna"> Não</label>
  </div>
</div>

As you can see, the radio button have the same id. This is wrong, right?

And... why the radio generate same id? Not just a name?



Sorry for my english... thanks

Upvotes: 0

Views: 256

Answers (2)

Damien Pirsy
Damien Pirsy

Reputation: 25435

Yeah, that's a small drawkback of automatic code creation. Behind the scenes, Form::radio() calls the same $this->input($type, $name, $value = null, $options = array()) general function for the inputs, which inside calls:

$id = $this->getIdAttribute($name, $options);

The method getIdAttribute() has this definition:

public function getIdAttribute($name, $attributes)
{
    if (array_key_exists('id', $attributes))
    {
        return $attributes['id'];
    }

    if (in_array($name, $this->labels))
    {
        return $name;
    }
}

That means, if it finds a specified ID in the options provided it returns that, else it returns the name.

Which is a comfortable and perfectly valid solution for the other input types, but doesn't work well for radios and checkboxes, where you need to provide your own id.

{{ Form::radio('indicacao_interna', 1, oldRadio('indicacao_interna', 1, true),
    array('id' => 'custom_id')) }}

Upvotes: 1

cagartner
cagartner

Reputation: 11

Tenta assim:

{{ Form::radio('indicacao_interna', 1, oldRadio('indicacao_interna', 1, true),
    array('id' => 'indicacao_interna_1')) }}

{{ Form::radio('indicacao_interna', 1, oldRadio('indicacao_interna', 1, true), 
    array('id' => 'indicacao_interna_2')) }}

Upvotes: 1

Related Questions