简体   繁体   中英

How to change the input type based on the type stored in database?

I have a form for a user create custom questions. The user needs to introduce the question and also the type of field (text, long text, checkbox, select menu, radio button) to create a custom question:

<form method="post" class="clearfix" action="{{route('questions.store', ['conference_id' => $conference->id])}}" enctype="multipart/form-data">
    {{csrf_field()}}
    <div class="form-group">
        <label for="question">Question</label>
        <input type="text" class="form-control" name="question" id="question">
    </div>
    <div class="form-group">
        <label for="type" class="text-heading h6 font-weight-semi-bold">Type of field</label>
        <select class="form-control" name="type" id="type">
            <option value="text">Text</option>
            <option value="long_text">Long Text</option>
            <option value="checkbox">Checkbox</option>
            <option  value="radio_btn">Radio Button</option>
            <option  value="select_menu">Select menu</option>
        </select>
    </div>

    <div>
        <input type="submit" class="btn btn-primary" value="Store"/>
    </div>
</form>

In the database in the questions table is like:

id       question                    conference_id        type
1        Whats your phone?               1                 text
2        Want receive notifications?      1                 radio_btn
3       ..............                    1                 checkbox
4       ..............                    1                 long_txt
5       ..............                    1                 select_menu

Then in the registration.blade.php I show the custom questions to the user so he can answer. The question is already presented to the user with the code below. My doubt is how to change the input type based on the type of the question stored in database.

Do you know how this can be achieved? As it is it appears always the question with a type of text. But if the question type is for example a checkbox it should appear the question as a checkbox not as an input type text.

@foreach($selectedType['questions'] as $customQuestion)
    <div class="form-group">
        <label for="participant_question">{{$customQuestion->question}}</label>
        <input type="text"
               @if($customQuestion->pivot->required == "1") required @endif
               class="form-control" name="participant_question[]">
        <input type="hidden" name="participant_question_required[]"
               value="{{ $customQuestion->pivot->required }}">
        <input type="hidden" value="{{ $customQuestion->id }}" name="participant_question_id[]"/>
    </div>
@endforeach

In your Question model create a function to output the different types of input required.

Eg

public function getHtmlInput($name = "", $id = "", $required = false, class="", val = "", $customtype=false) 
{
    switch ($this->type) {
        case "text": 
              return "<input type='".($customtype?:"text")."' name='$name' id='$id' class='$class' value='$val'" . ($required?:"required") . ">";
        case "checkbox":
        ...
    }
}

Then in your view you can just do $question->getHtmlInput(<params>)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM