Php codeigniter中的字段未设置错误
验证表单时,未将“获取”字段设置为错误 这是我的看法Php codeigniter中的字段未设置错误,php,codeigniter,validation,Php,Codeigniter,Validation,验证表单时,未将“获取”字段设置为错误 这是我的看法 <div class="panel-body"> <h3>Basic Information</h3> <?php $atrrib = array('class' => 'form-horizontal','role' => 'form');?> <?php echo form_open('school/shule',$atrrib);?> <div c
<div class="panel-body">
<h3>Basic Information</h3>
<?php $atrrib = array('class' => 'form-horizontal','role' => 'form');?>
<?php echo form_open('school/shule',$atrrib);?>
<div class="col-md-6">
<div class="form-group">
<label for="add_name" class="col-md-2 control-label">School:</label>
<div class="col-md-7">
<input type="text" class="form-control" name="add_name" id="addSchoolName" placeholder="School name">
</div>
</div>
</div>
<button type="submit" class="btn btn-default">Next</button>
<?php echo form_close();?>
</div>
</div>
</div>
<div class="col-md-2"><?php include_once (APPPATH. 'views/admin/admin_right_column.php');?></div>
</div>
}
这是我的表单验证配置
当我将字段留空时,我得到一个字段未设置错误,并且当我输入一个字符时,我得到一个无法访问与您的字段名错误对应的错误消息。试试看
$this->form_validation->set_rules('add_name', 'School name', 'trim|required||min_length[2]');
if ($this->form_validation->run() == TRUE){
echo 'Valid';
}
else {
$data['title'] = "Add School";
$data['page'] = "admin| add school";
$this->load->view('admin/admin_header',$data);
$this->load->view('admin/add_school',$data);
$this->load->view('admin/admin_footer');
}
并检查视图中的错误
if(validation_errors()){
echo validation_errors();
}
我试着寻找CI引发你错误的地方 无法访问与字段名对应的错误消息 然后我找到了这个
if ($result === FALSE)
{
if ( ! isset($this->_error_messages[$rule]))
{
if (FALSE === ($line = $this->CI->lang->line($rule)))
{
$line = 'Unable to access an error message corresponding to your field name.';
}
}
else
{
$line = $this->_error_messages[$rule];
}
因此,这意味着CI检查您是否未将rule message设置为字段,然后检查是否有一行对应于表单_validation_lang.php文件中的激发函数/规则。
因此,我假设CI没有找到与字段名对应的错误消息,因为
- 您已编辑/删除了具有回调/规则名称的行
- 您已经在/application/language下创建了另一个语言文件,其名称与默认表单\u validation\u lang相同,并且尚未定义与字段名对应的规则李>
$lang['required'] = 'Name field is required";
或在自定义语言文件的验证文件/函数中定义的任何其他规则 您可以使用set_message设置自定义错误消息,并使用set_rules函数中的标签。您可以在这里找到文档 以下是一个例子:
// Set the validation rule
$this->form_validation->set_rules('project_name', 'Project Name', 'trim|required');
// Set the custom message with %s where the label should go
$this->form_validation->set_message('required', '%s is required. Please enter a value.');
// For an invalid project_name, you will get the error:
// Project Name is required. Please enter a value.
好啊让我来吧。我很快会给你回复的。对不起,很快花了几个小时。我已经找到了解决办法。谢谢你好,罗比。谢谢你的回答。虽然我已经定义了一个自定义的错误语言文件,但是我还有其他的表单可以正常工作。我已经删除了它,而另一个表单现在可以工作了。你能解释吗?是的你能。。wel您可以有一个自定义规则,它用与原始规则相同的名称定义所有规则。。很高兴它成功了。。
$lang['required'] = 'Name field is required";
// Set the validation rule
$this->form_validation->set_rules('project_name', 'Project Name', 'trim|required');
// Set the custom message with %s where the label should go
$this->form_validation->set_message('required', '%s is required. Please enter a value.');
// For an invalid project_name, you will get the error:
// Project Name is required. Please enter a value.