dimanche 17 janvier 2016

Laravel 5.1 :Form Model Binding - Radio buttons

Scenario:

I want to make something like questioner so I have multi forms in one page each form related to question and has its own submit button, each question has 4 answers so the user can only select one answer for the question using radio buttons. I need to bind the data to the related field using Form data binding , all the other data is binding perfectly but the problem on the radio button .

Controller

/* get all answers that related to the topic */
$question_answers = ['criteria_id' => $criteriaId, 'sector_id' => $sector_id];
        $answers = Answer::where($question_answers)->get();
        $questions = $criteria->question()->get();
/* check existing data */
        $check_data = ['organisation_id' => $organisation_id, 'customize_criteria_id' => $criteriaId];
        $exist_data = RatingDatum::where($check_data)->get();

View:

 @foreach($question as $index=>$question)
                                <div class="plan bg-plan">
                                    <h5>{{$question->question}}</h5>
                                    <hr>
    @foreach($exist_data as $datum)
                                            @if($datum->question_id == $question->id)
                                                {!! Form::model($datum,['method' => 'PATCH','action'=>['RatingDataController@update'],$datum->id]) !!}
                                            @endif
                                        @endforeach
                                        @foreach($answers as $answer)
                                            @if($answer->question_id == $question->id)
                                                <div class="radio">
                                                    <label>

                                                        {!! Form::radio('answer_id['.$index.']', $answer->id, $datum->answer_id != null ? 'true' : 'false')!!}
                                                    {{$answer->score}} {{$answer->answer}}
                                                    </label>
                                                </div>
                                            @endif
                                        @endforeach
                                        <div class="form-group">
                                            {!! Form::label('comment','Comment :') !!}
                                            {!! Form::textarea('comment' ,null,['class'=>'form-control', 'rows' => 4]) !!}

                                            {!! Form::label('reference','Reference:') !!}
                                            {!! Form::textarea('reference',null,['class'=>'form-control', 'rows' => 1]) !!}
                                        </div>
                                        {!! Form::submit('Submit Data', ['class' => 'btn btn-success submit']) !!}
                                        {!! Form::close() !!}
                                                <!--/.Plan -->
                                    </div>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire