简体   繁体   English

如何根据存储在数据库中的类型更改输入类型?

[英]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. 然后在registration.blade.php中我向用户显示自定义问题,以便他可以回答。 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. 在您的Question模型中,创建一个函数以输出所需的不同类型的输入。

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>) 然后在你的视图中你可以做$question->getHtmlInput(<params>)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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