Php 为什么不出现"";“必需”;表单元素中的属性正在出现;“1”吗;?
我有一个表单供用户创建自定义问题。用户需要介绍问题以及创建自定义问题的字段类型(文本、长文本、复选框、选择菜单、单选按钮) 然后,我有一个问题模型,该模型具有getHtmlInput()函数,可以根据自定义问题类型在视图中输出不同的类型 问题在于,在数据透视表“registration\u type\u questions”中,自定义问题的“required”属性显示为“1”,而不是显示“required” 你知道为什么吗 问题模式:Php 为什么不出现"";“必需”;表单元素中的属性正在出现;“1”吗;?,php,laravel,Php,Laravel,我有一个表单供用户创建自定义问题。用户需要介绍问题以及创建自定义问题的字段类型(文本、长文本、复选框、选择菜单、单选按钮) 然后,我有一个问题模型,该模型具有getHtmlInput()函数,可以根据自定义问题类型在视图中输出不同的类型 问题在于,在数据透视表“registration\u type\u questions”中,自定义问题的“required”属性显示为“1”,而不是显示“required” 你知道为什么吗 问题模式: class Question extends Model {
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;
}
}
类问题扩展模型
{
受保护的$fillable=[
“问题”、“类型”、“会议id”,
];
公共静态$typeHasOptions=[
“无线电”,
“选择菜单”,
“复选框”
];
公共功能注册类型(){
返回$this->belongstomy('App\RegistrationType','registration\u type\u questions')
->withPivot(“必需”);
}
公共职能选项(){
返回$this->hasMany('App\QuestionOption');
}
公共功能选项(){
返回数组($this->type,self::$typehasportions);
}
公共函数getHtmlInput($name=“”、$options=“”、$required=false、$class=“”、$customtype=false){
$html='';
$html.=$customtype=='选择菜单'?'':'';
如果(空($选项)){
交换机($customtype){
案例“文本”:
$html.=”
";
打破
案例“档案”:
$html.=”
";
打破
案例“长文本”:
$html.=”
"
.$name。
"";
打破
}
}否则{
foreach($options作为$option){
交换机($customtype){
案例“复选框”:
$html.=”
" .
“.$option->value.”。
"";
打破
案例“radio_btn”:
$html.=”
" .
“.$option->value.”。
"";
打破
案例“选择菜单”:
$html.=''.$option->value.'';
打破
}
}
}
$html.=$customtype=='选择菜单'?'':'';
返回$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
@if($allParticipants==0)
@foreach($SelectedType['questions']作为$customQuestion)
{{$customQuestion->question}
@如果($customQuestion->hasOptions()&&in_数组($customQuestion->type,['checkbox','radio_btn','select_menu']))
{!!$customQuestion->getHtmlInput(
$customQuestion->name,
$customQuestion->options,
($customQuestion->pivot->required=='1'),
“窗体控件”,
$customQuestion->type)
!!}
@否则
{!!$customQuestion->getHtmlInput(
$customQuestion->name,
[],
($customQuestion->pivot->required=='1'),
“窗体控件”,
$customQuestion->type)
!!}
@恩迪夫
@endforeach
@恩迪夫
生成的HTML:(而不是必需的显示1)
正文
复选框
支票1
支票2
无线电
rad1
rad2
选择
选择1选择2
文本区
文件
在这段代码中
($required ?: " required")
您没有设置任何内容,因为该语句为true(is required),所以它将默认设置为require,您正在告诉我们,当不为true时,正确的语法为
($required ? " required" : "")
在这段代码中
($required ?: " required")
您没有设置任何内容,因为该语句为true(is required),所以它将默认设置为require,您正在告诉我们,当不为true时,正确的语法为
($required ? " required" : "")
谢谢,您知道如何在每个问题标签中添加自定义问题这个“*”?我想在您的问题模型中,您可以添加类似
case“long_text”的switch语句在哪里:如果($required)$html.=“$html.=”" . $名称""; 如果($required)$html.=”中断代码>记住将此标记为正确答案:)谢谢,你知道为什么会出现“required=”“”而不是“required”?伙计,我认为我们从最初的问题出发太离谱了,这违反了本网站的规则。耐心阅读代码,你会从犯错误中学到更多。如果您确实需要帮助,只需发布另一个问题并成为SO的活跃成员。:)谢谢,您知道如何在每个问题标签中添加自定义问题这个“*”?我想在您的问题模型中,您可以添加类似case“long_text”的switch语句在哪里:如果($required)$html.=“$html.=”" . $名称""; 如果($required)$html.=”中断代码>记住将此标记为正确答案:)谢谢,你知道为什么出现“required=”“”而不是“required”?伙计,我知道了