我是codeigniter的新手。我想在文本框旁边显示错误消息。我的视图文件create.php
包含以下代码。
<?php
echo validation_errors(); //validation
echo form_open('news/create'); //create a form
?>
<div class="boxes">
<div class="common">
<div class="lables">
<label for="name">Name</label>
</div>
<div class="text_boxes">
<input type="text" name="uname" /><div id="infoMessage"><?php echo $message;?></br>
</div>
</div>`
以下是我的控制器型号:
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Insert Items';
$this->form_validation->set_rules('uname','required|min_length[6]|max_length[15]');
$this->data['message'] = enter your name;
$this->form_validation->set_rules('uaddress','enter your address','required');
$this->form_validation->set_rules('email','Enter emailid','required');
$this->form_validation->set_rules('mobile','Enter mobile number','required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('news/success');
}
/** View Details **/
$data['user_data'] = $this->news_model->get_details();
$data['title'] = 'Database Details';
$this->load->view('templates/header'); //used to call the view file(header) which is in templates/header
$this->load->view('news/index', $data); //used to call the view file(index) ie:news/index
$this->load->view('templates/footer'); //used to call the view file(footer) ie:templates/header
/** END **/
}
但我知道#infoMessage
将始终显示在username field
之后。但我真的不知道如何在自定义位置显示错误消息。
请帮我解决这个问题!!
编辑:添加后
<input type="text" name="uname" value="<?php echo set_value('uname'); ?>" />
<div id="infoMessage"><?php echo form_error('uname'); ?></div></br>
此代码显示如下图所示:
答案 0 :(得分:2)
如果您阅读CI documentation,就可以找到答案。
用此
替换你的行<input type="text" name="uname" value="<?php echo set_value('uname'); ?>" /><div id="infoMessage"><?php echo form_error('uname'); ?></div></br>
答案 1 :(得分:0)
echo validation_errors(); ----拿出来打印一切。
使用echo form_error(&#39; YOUR_FIELD_NAME&#39;);在具有字段名称的每个输入字段之后。