Cederman's avatar

Handling radio button values in forms where the name attribute is an array

Hi! I have a form that lists mulptiple photographers for an event. The form hold information about the cost to pay each photographer, along with a note and if a specific photographer is paid or not (the radio button, yes/no). Since the number of photographers for a given event is ambigous, I have the form field names defined as arrays so I iterate trough the amount of added photographers and update their fields accordingly.

This is the snippet of the form I'm describing:

@foreach($event_photographers as $key => $photographer)
<div class="row" style="margin-bottom:10px;">
    <div class="col-xs-3">
        <h5><i class="fa fa-user"></i> Fotograf {{ $event_photographers->count() >1 ? $key+1 : '' }}</h5>
        {{ $photographer->getFullnameAttribute() }}
    </div>
    <div class="col-xs-3">
        <i class="fa fa-money"></i> {{ Form::label('photographer_payment_sum', 'Arvode') }}
        {{ Form::text('photographer_payment_sum[]', $photographer->pivot->photographer_payment_sum, array('class' => 'form-control', 'placeholder' => 'Arvode i kronor', 'autocomplete' => 'off')) }}
        {{ $errors->first('photographer_payment_sum[]', '<span class="text-danger">:message</span>') }}
    </div>
    <div class="col-xs-4">
        <i class="fa fa-pencil"></i> {{ Form::label('photographer_payment_notes', 'Kommentar') }}
        {{ Form::text('photographer_payment_notes[]', $photographer->pivot->photographer_payment_notes, array('class' => 'form-control', 'placeholder' => 'Kommentar', 'autocomplete' => 'off')) }}
        {{ $errors->first('photographer_payment_notes[]', '<span class="text-danger">:message</span>') }}
    </div>
    <div class="col-xs-2 text-right">
        {{ Form::label('photographer_is_paid', 'Fått betalt?') }}<br/>
        <div class="btn-group" data-toggle="buttons">
            <label id="photographer_is_paid_true" class="btn btn-default {{ $photographer->pivot->photographer_is_paid ? 'active' : '' }}">
                <input type="radio" name="photographer_is_paid[]" value="1" {{ $photographer->pivot->photographer_is_paid ? 'checked' : '' }}> Ja
            </label>
            <label id="photographer_is_paid_false" class="btn btn-default {{ !$photographer->pivot->photographer_is_paid ? 'active' : '' }}">
                <input type="radio" name="photographer_is_paid[]" value="0" {{ !$photographer->pivot->photographer_is_paid ? 'checked' : '' }}> Nej
            </label>
        </div>
        {{ $errors->first('photographer_is_paid', '<span class="text-danger">:message</span>') }}                       
    </div>
</div>
@endforeach

However, when I post the form I see that I do have multiple values being sent for the note field along with the cost field for each photographer. But the only value I get from the radio buttons is the "true" values. I.e. when the toggle is set to "Ja".

Have any of you run into the same issue before?

0 likes
5 replies
bashy's avatar

By default a radio type input will only be set if it's set to something other than false/0. You will need to do a check for that.

You can get around this by using yes/no as the values. Or setting a default if it's not set

$request->input('name', 0);
Cederman's avatar

Thanks for your reply, but it doesn't seem to work anyway.

In my case, where I have four photographers, I get four values for all the attributes except for the four radio buttons which I also have.

This is the post values I get. As you can see I have four values in all the arrays I have, except for the radio button values (photographer_is_paid[]), there I only have one. In this scenario, all the radio buttons was switched to "no":

photographer_ids[]:1
photographer_ids[]:5
photographer_ids[]:6
photographer_ids[]:7
photographer_payment_sum[]:
photographer_payment_notes[]:
photographer_payment_sum[]:
photographer_payment_notes[]:
photographer_payment_sum[]:
photographer_payment_notes[]:
photographer_payment_sum[]:
photographer_payment_notes[]:
photographer_is_paid[]:no
bashy's avatar
bashy
Best Answer
Level 65

If you have multiple radios that each need to be grouped, you may need to set the array number.

name="photographer_is_paid[1]"
name="photographer_is_paid[2]"

Just an idea

Cederman's avatar

Awesome, that did it, thanks! I didn't even know that was a "thing" hehe. Was just recently I learned that you can treat the input name as an array =)

Please or to participate in this conversation.