首页 > 解决方案 > 应该需要一组复选框,而不是每个单独的复选框

问题描述

我有一个表格供用户创建自定义问题。用户需要介绍问题以及字段类型(文本、长文本、复选框、选择菜单、单选按钮)来创建自定义问题。

然后我有一个具有 getHtmlInput() 函数的 Question 模型,可以根据自定义问题类型在视图中输出不同的类型。

它工作正常,但有一个问题:当自定义问题类型是复选框并且需要问题时,这应该意味着用户需要回答该问题,但不应该意味着用户需要选择所有复选框。但是使用下面的代码,当需要一个复选框字段时,与该字段相关的所有复选框都需要被选中。你知道如何解决这个问题吗?

如果需要自定义问题,您还知道如何在每个问题标签内添加此“ <span class="text-primary">*</span>”吗?

问题模型:

class Question extends Model
{

    public static $typeHasOptions = [
        'radio_btn',
        'select_menu',
        'checkbox'
    ];


    public function hasOptions() {
        return in_array($this->type, self::$typeHasOptions);
    }

    public function getHtmlInput($name = "", $options = "", $required = false, $class = "", $customtype = false) {
        $html = '';
        $html .= $customtype == 'select_menu' ? "<select name='participant_question' class='form-control' " . ($required ? " required" : "")
            . ">" : '';

        if (empty($options)) {
            switch ($customtype) {
                case "text":
                    $html .= " 
                    <input type='text' name='participant_question' class='form-control'" . ($required ? " required" : "")
                        . ">";
                    break;

                case "file":
                    $html .= " 
                    <input type='file' name='participant_question' class='form-control'" . ($required ? " required" : "") . ">";
                    break;

                case "long_text":
                    $html .= "

                <textarea name='participant_question' class='form-control' rows='3'" . ($required ? " required" : "") . ">"
                        . $name .
                        "</textarea>";
                    break;
            }
        } else {
            foreach ($options as $option) {
                switch ($customtype) {
                    case "checkbox":
                        $html .= " 
                <div class='form-check'>
                    <input type='checkbox' name='participant_question[]' value='" . $option->value . "' class='form-check-input'" . ($required ? " required" : "") . ">" .
                            '    <label class="form-check-label" for="exampleCheck1">' . $option->value . '</label>' .
                            "</div>";
                        break;
                    case "radio_btn":
                        $html .= " 
                <div class='form-check'>
                    <input type='radio' name='participant_question[]' value='" . $option->value . "' class='form-check-input'" . ($required ? " required" : "") . ">" .
                            '    <label class="form-check-label" for="exampleCheck1">' . $option->value . '</label>' .
                            "</div>";
                        break;
                    case "select_menu":
                        $html .= "<option value='" . $option->value . "'>" . $option->value . "</option>";
                        break;
                }
            }
        }
        $html .= $customtype == 'select_menu' ? "</select>" : '';

        return $html;
    }
}

在视图中使用 getHtmlInput():

@if ($allParticipants == 0)
    @foreach($selectedRtype['questions'] as $customQuestion)
        <div class="form-group">
            <label for="participant_question">{{$customQuestion->question}}</label>
            @if($customQuestion->hasOptions() && in_array($customQuestion->type, ['checkbox', 'radio_btn', 'select_menu']))
                {!! $customQuestion->getHtmlInput(
                    $customQuestion->name,
                    $customQuestion->options,
                    ($customQuestion->pivot->required == '1'),
                    'form-control',
                    $customQuestion->type)
                !!}

            @else
                {!! $customQuestion->getHtmlInput(
                    $customQuestion->name,
                    [],
                    ($customQuestion->pivot->required == '1'),
                    'form-control',
                    $customQuestion->type)
                !!}
            @endif
            <input type="hidden"
                   name="participant_question_required[]"
                   value="{{ $customQuestion->pivot->required }}">
            <input type="hidden"
                   value="{{ $customQuestion->id }}"
                   name="participant_question_id[]"/>
        </div>
    @endforeach
@endif

生成的 HTML:

<form method="post" action="">


  <div class="form-group">
    <label for="participant_question">Text</label>
    <input type="text" name="participant_question" class="form-control" required="">
    <input type="hidden" name="participant_question_required[]" value="1">
    <input type="hidden" value="1" name="participant_question_id[]">
  </div>

  <div class="form-group">
    <label for="participant_question">Checkbox</label>
    <div class="form-check">
      <input type="checkbox" name="participant_question[]" value="check1" class="form-check-input" required="">  
      <label class="form-check-label" for="exampleCheck1">check1</label>
    </div> 
    <div class="form-check">
      <input type="checkbox" name="participant_question[]" value="check2" class="form-check-input" required="">    
      <label class="form-check-label" for="exampleCheck1">check2</label>
    </div>

    <input type="hidden" name="participant_question_required[]" value="1">
    <input type="hidden" value="2" name="participant_question_id[]">
  </div>

  <div class="form-group">
    <label for="participant_question">Radio</label>
    <div class="form-check">
      <input type="radio" name="participant_question[]" value="rad1" class="form-check-input">  
      <label class="form-check-label" for="exampleCheck1">rad1</label>
    </div> 
    <div class="form-check">
      <input type="radio" name="participant_question[]" value="rad2" class="form-check-input">   
      <label class="form-check-label" for="exampleCheck1">rad2</label>
    </div>
    <input type="hidden" name="participant_question_required[]" value="0">
    <input type="hidden" value="3" name="participant_question_id[]">
  </div>

  <div class="form-group">
    <label for="participant_question">select</label>
    <select name="participant_question" class="form-control">
      <option value="select1">select1</option>
      <option value="select2">select2</option>
    </select>

    <input type="hidden" name="participant_question_required[]" value="0">
    <input type="hidden" value="4" name="participant_question_id[]">
  </div>

  <div class="form-group">
    <label for="participant_question">textarea</label>
    <textarea name="participant_question" class="form-control" rows="3"></textarea>
    <input type="hidden" name="participant_question_required[]" value="0">
    <input type="hidden" value="5" name="participant_question_id[]">
  </div>

  <div class="form-group">
    <label for="participant_question">file</label>
    <input type="file" name="participant_question" class="form-control" required="">
    <input type="hidden" name="participant_question_required[]" value="1">
    <input type="hidden" value="6" name="participant_question_id[]">
  </div>

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

标签: phptwitter-bootstraplaravel

解决方案


推荐阅读