这是我的简化视图
<?php
echo form_open('form/submit');
echo form_label('User Name:*', 'u_name');
$data= array('name' => 'u_name','placeholder' => 'Your Name');
echo form_input($data);
echo form_label('User emails:', 'u_email');
$data= array('type' => 'email','name' => 'u_email[0][email]', 'placeholder' => 'Email');
echo form_input($data);
$data= array('type' => 'email','name' => 'u_email[1][email]', 'placeholder' => 'Email');
echo form_input($data);
$data = array('type' => 'submit','value'=> 'Submit','class'=> 'submit');
echo form_submit($data);
echo form_close();
这是我的示例控制器
$this->form_validation->set_rules('u_name', 'Name', 'trim|required|max_length[20]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myview', $data);
}
与预期的偏差:
当我在未填写名称字段的情况下提交表单并且在服务器端表单验证失败后,我期待名称字段的表单验证错误。但它会产生Severity: Notice
一个Array to string conversion
遇到 PHP 错误
严重性:通知
消息:数组到字符串的转换
文件名:helpers/form_helper.php
行号:972
我的调试发现:
排除故障后,我发现我的input as array
字段导致了此错误通知。但是,当我通过填写所有必填字段提交时,它工作正常并且input as array
字段不会生成任何Notice
.
form_helper.php
function _parse_form_attributes($attributes, $default)
{
if (is_array($attributes))
{
foreach ($default as $key => $val)
{
if (isset($attributes[$key]))
{
$default[$key] = $attributes[$key];
unset($attributes[$key]);
}
}
if (count($attributes) > 0)
{
$default = array_merge($default, $attributes);
}
}
$att = '';
foreach ($default as $key => $val)
{
if ($key === 'value')
{
$val = html_escape($val);
}
elseif ($key === 'name' && ! strlen($default['name']))
{
continue;
}
$att .= $key.'="'.$val.'" '; //Line Number: 972
}
return $att;
}
请深入研究一下,并为此建议我一些解决方案。
编辑:
从我工作的实际场景中查看下面的代码,以便更好地理解
<?php
if(empty($additional_alert[0]))
{
echo '<td id ="empty">No additional alert data found.</td>';
}
else
{
foreach($additional_alert as $key=>$additional_alert):
?><div>
<?php
$data = array('placeholder'=>'Name', 'name'=>'additional_alert['.$key.'][name]', 'value'=>set_value('additional_alert', $additional_alert['name']), 'class'=>'');
echo form_input($data);
$data = array('placeholder'=>'Email', 'name'=>'additional_alert['.$key.'][email]', 'value'=>set_value('additional_alert',$additional_alert['email']), 'class'=>'');
echo form_input($data);
echo '<button type="button" name="remove" id="" class="btn btn-danger btn_removes"><i class="fa fa-trash-o"></i></button>';
?><br>
</div>
<?php
endforeach;
}
?>