当前位置: 首页 > 知识库问答 >
问题:

生成HTML以显示具有正确类型的自定义问题(文本、复选框等)并正确添加所需的属性

桓智敏
2023-03-14

我有一个表单供用户创建自定义问题。为此,用户需要引入问题(例如:你的手机是什么?)以及字段类型(文本、长文本、复选框、选择菜单、单选按钮)。如果用户选择一个复选框字段,选择菜单或单选按钮,他还需要介绍问题的可用选项。

在数据库中,问题插入到问题和问题选项表中,如:

问题表:

id       question         type        conference_id
1          Text            text             1 
2        Checkbox         checkbox          1 
3          Radio           radio_btn        1 
4          select         select_menu       1 
5         textarea         long_text        1 
6           file             file           1 

注册\类型\问题透视表:

id registration_type_id   question_id  required
1         1                     1          1   
2         1                     2          1   
3         1                     3          0   
4         1                     4          0   
5         1                     5          0   
6         1                     6          1   

选项存储在questions_options表中:

   id   question_id   value

    1          2        check1  
    2          2        check2  
    3          3        rad1    
    4          3        rad2    
    5          4        select1
    6          4        select2 

然后在视图中,我想在视图中正确显示registration.blade.php输入(文本、单选按钮、复选框、选择、文本区域和输入文件)基于存储在问题表的类型列中的类型。如果数据透视表中所需的列是“1”,还可以添加所需的属性。

当问题的类型为文本、单选按钮、选择、文本区域或文件时,工作正常,所需的属性将添加到表单字段。

但是它不能正确地使用复选框,因为在复选框的情况下,如果问题是checkbox类型,并且问题是必需的,那么这意味着用户需要回答该问题,但不意味着用户需要选中所有复选框。

问题是,使用函数getHTMLInput()生成的html复选框在每个复选框输入中都需要,因此用户需要检查所有复选框:

 <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>

你知道如何解决这个问题吗?当需要自定义问题时,这意味着需要该问题,因此用户应至少选择一个复选框,但不意味着用户需要选中所有复选框。

如果需要自定义问题,您是否知道如何在每个问题标签中添加此“

GetHtmlInput()在问题模型中:

class Question extends Model
{
    protected $fillable = [
        'question', 'type', 'conference_id',
    ];

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

    public function registration_type()
    {
        return $this->belongsToMany('App\RegistrationType', 'registration_type_questions')
            ->withPivot('required');
    }

    public function options()
    {
        return $this->hasMany('App\QuestionOption');
    }

    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

使用getHTMLInput()生成超文本标记语言:

<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>

此外,在超文本标记语言验证器(如w3c验证器)中检查此表单会出现一些错误:

  • label元素的for属性必须引用非隐藏的表单控件。在“文本”中

共有3个答案

丁鸿云
2023-03-14

HTML5没有一个解决方案来实现需要一组复选框,因此通过一些更改您可以实现它。首先,在控制器上,您需要对其进行更改,以实现与选择菜单相同的效果。

// on top of your method:
$html .= $customtype == 'checkbox' ? "<div class='checkbox-group ".($required ? " required" : "")."'>" : '';
// at the bottom
$html .= $customtype == 'checkbox' ? "</div>" : '';

然后在复选框的情况下,您只需要打印所需复选框组中的选项:

case "checkbox":
            $html .= " 
            <div class='form-check'>
                <input type='checkbox' name='participant_question[]' value='" . $option->value . "' class='form-check-input' ><label class='form-check-label' for='exampleCheck1'>" . $option->value . "</label>
            </div>";

这将输出以下html:

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

然后在提交时,我不知道您是否使用ajax,但我假设您没有,所以如果您在表单中添加id-

$('#questionForm').submit(function() {
    if($('div.checkbox-group.required div :checkbox:checked').length > 0) {
        return true;//submit the form
    } else {
        return false; // do not submit the form
    }
});

不幸的是,只使用html5是不可能实现你想要的,无论你选择什么解决方案,它都可能必须用js来完成。

劳星晖
2023-03-14

将getHtmlInput()替换为以下内容

public function getHtmlInput($question_id, $index_position, $name = "", $options = "", $required = false, $class = "", $customtype = false) {

    //dd($name);
    $html = '';
    $html .= $customtype == 'select_menu' ? "<select name='participant_question[".$question_id."][".$index_position."]' class='form-control' " . ($required ? " required" : "")
        . ">" : '';

    if (empty($options)) {
        switch ($customtype) {
            case "text":

                $html .= " 


                <input type='text' name='participant_question[".$question_id."][".$index_position."]' class='form-control'" . ($required ? " required" : "")
                    . ">";


                break;

            case "file":

                $html .= " 


                <input type='file' name='participant_question[".$question_id."][".$index_position."]' class='form-control'" . ($required ? " required" : "") . ">";


                break;

            case "long_text":
                $html .= "

            <textarea name='participant_question[".$question_id."][".$index_position."]' 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[".$question_id."][".$index_position."][]' 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[".$question_id."][".$index_position."][]' 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;
}

刀片密码

@if ($allParticipants == 0)
{{ $index_position = 0 }}
@foreach($selectedRtype['questions'] as $customQuestion)

   {{ $question_id = [[QUESTION_ID]] }}

    <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(
                $question_id,
                $index_position,
                $customQuestion->name,
                $customQuestion->options,
                ($customQuestion->pivot->required == '1'),
                'form-control',
                $customQuestion->type)
            !!}

        @else
            {!! $customQuestion->getHtmlInput(
                $question_id,
                $index_position,
                $customQuestion->name,
                [],
                ($customQuestion->pivot->required == '1'),
                'form-control',
                $customQuestion->type)
            !!}
        @endif
    </div>
    {{ $index_position = $index_position+1 }}
@endforeach
@endif

您需要将同一组选项的name属性设置为相同,以便正确使用所需选项。

但在代码中,包括不同选项组的所有选项都共享相同的名称属性。

以上代码未经测试。但我希望它对你有用

毋修为
2023-03-14

这是因为在您的Foreach循环中有一个复选框组html:

 foreach ($options as $option) {
                switch ($customtype) {
                    case "checkbox":
                        $html .= " 
                <div class='checkbox-group' " . ($required ? " required" : "") . ">

您需要考虑如何使用类似于$checkboxesFound的变量,并在函数开头将其设置为0,当case为checkbox时,增加变量,如果$checkboxesFound==0,则回显组div。

 类似资料:
  • 问题内容: 我们都知道如何在HTML中形成复选框输入: 我不知道-选中的复选框的技术上正确的值是多少?我看过所有这些工作: 答案没关系吗?我看到标记为正确答案,没有证据这里)从规范]本身: 复选框(和单选按钮)是可以由用户切换的开/关开关。设置控制元素的选中属性时,开关为“ on”。提交表单后,只有“启用”复选框控件才能成功。表单中的多个复选框可以共享相同的控件名称。因此,例如,复选框允许用户为同

  • 我有一个用户创建自定义问题的表单。为此,用户需要引入问题(例如:接收通知?)以及字段的类型(文本、长文本、复选框、选择菜单、单选按钮)。如果用户选择复选框的字段、选择菜单或单选按钮,他还需要为该问题引入可用的选项。 在数据库中,问题插入到问题和问题选项表中,如: //问题表 //问题(u)选项表: 我的疑问是如何在注册中正确显示。刀身php根据问题表的“类型”列中存储的类型来输入(文本、单选按钮、

  • 我有一个用户创建自定义问题的表单。为此,用户需要引入问题(例如:接收通知?)以及字段的类型(文本、长文本、复选框、选择菜单、单选按钮)。如果用户选择复选框的字段、选择菜单或单选按钮,他还需要为该问题引入可用的选项。 在数据库中,问题插入到问题和问题选项表中,如: //问题表 //问题(u)选项表: 我的疑问是如何在registration.blade.php输入(文本、单选按钮、复选框、选择、文本

  • 使用带有React应用程序的JSDocs的更好的文档插件。它旨在解析PropType,并从PropType获取信息,并将其放入数组中。确实如此。但在表中显示必需为'否',即使当proType标记为是必需的。 https://github.com/SoftwareBrothers/better-docs#usage-2 https://softwarebrothers.github.io/admin

  • 我想根据我使用 AbstractTableModel 指定的列从数据库中的表中提取数据。表中有 8 列,但只想显示 8 列中的 3 列。 例如: 数据库有以下列:ID、First_Name、Last_Name、…、Phone_Number。如果我指定要在表中显示First_Name、Last_Name和Phone_Number,它将显示ID、First_ Name和Last_ Name。我认为应该

  • 考虑一个图的实现。考虑图节点的一个实现,,正确地覆盖和以镜像两个节点之间的逻辑相等。 现在,假设我们想给一个节点添加一些属性p。这样的属性绑定到节点的逻辑实例,即对于,暗示p()=p() 如果我只是将属性作为类的字段添加,则发生了以下情况: null 总结一下,当前行为: 以下所有的说法在我看来都是有道理的。没有一个能让我完全信服,所以我正在寻找基于软件工程规范的最佳实践指南。 S1-图形实现很差