直升机,
所以我在Codeigniter中有一个form_validation的问题,这里是控制器 -
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Records extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->view('includes/header');
$this->load->view('products');
$this->load->view('includes/footer');
}
public function addRecord() {
$this->load->helper(array('form', 'url'));
$this->load->helper('url');
$query = $this->db->get_where('users', array('uname' => $this->session->userdata('username')));
foreach($query->result_array() as $row) {
$data = array(
'username' => $row['uname'],
'email' => $row['email'],
'name' => $row['name'],
'description' => $row['description'],
'picture' => $row['picture'],
'gender' => $row['gender'],
);
}
if($this->session->userdata('logged_in')) {
$this->load->view('includes/header');
$this->load->view('my_records', $data);
$this->load->view('includes/footer');
}
else {
redirect('/home', 'refresh');
}
}
public function postRecord() {
$this->lang->load('form_validation', 'latvian');
$this->load->library('form_validation');
$this->load->model('records');
$this->load->helper(array('form', 'url'));
if(!$this->session->userdata('logged_in')) redirect('/home', 'refresh');
$this->form_validation->set_rules('message', 'Ziņa', 'required|trim|min_length[5]|max_length[250]');
$query = $this->db->get_where('users', array('uname' => $this->session->userdata('username')));
foreach($query->result_array() as $row) {
$data = array(
'username' => $row['uname'],
'email' => $row['email'],
'name' => $row['name'],
'description' => $row['description'],
'picture' => $row['picture'],
'gender' => $row['gender'],
);
}
// If doesn't pass validation, redirect's back with errors.
if ($this->form_validation->run() == FALSE)
{
$this->load->helper('url');
$this->load->view('includes/header');
$this->load->view('my_records', $data);
$this->load->view('includes/footer');
}
// If everything is correct add's user.
else
{
$this->load->view('includes/header');
$this->load->view('my_records', $data);
$this->load->view('includes/footer');
}
}
} ?>
并查看 -
<div class="list-products">
<div>
<?php if(validation_errors()) { ?><div class="error-submit"><p><?php echo validation_errors(); ?></p></div><?php } ?>
<form class="fix-this form" method="post" action="/savieno/records/postRecord">
<div class="formfield">
<span class="profile">Your message:</span>
<textarea id="message" name="message" class="with-label" rows="10" cols="10" placeholder="your message."></textarea>
</div>
<div class="formfield">
<input type="submit" id="submit" name="postRecord" value="Pievienot" class="fix-this" />
</div>
</form>
</div>
一开始,它给了我错误,找不到functio set_message(),但我在表单助手之前添加了form_validation,现在它可以正常工作,但是又有问题,因为它不显示表单错误代码。它根本就没有显示它,我为它设置了错误信息,它应该可以工作,但事实并非如此。可能是什么问题?
答案 0 :(得分:0)
在您看来,您不需要:
<?php if(validation_errors()) { ?><div class="error-submit"><p><?php echo validation_errors(); ?></p></div><?php } ?>
您可以使用:
<?php echo validation_errors('<div class="error-submit">', '</div>'); ?>
那里不需要if语句。通过浏览,可以解决您的问题。
作为旁注,在你的控制器中,没有必要关闭PHP标签,实际上认为最佳做法是保持未关闭。
哦,如果你还想制作一个构造,你可以这样做:
function __construct() {
parent::__construct();
$query = $this->db->get_where('users', array('uname' => $this->session->userdata('username')));
foreach($query->result_array() as $row) {
$data = array(
'username' => $row['uname'],
'email' => $row['email'],
'name' => $row['name'],
'description' => $row['description'],
'picture' => $row['picture'],
'gender' => $row['gender'],
);
}
}
尽管如此,我认为在这样的模型(某些)中提供该查询会更好(MVC架构):
$this->load->model('dude');
$result = $this->dude->get_dude_where('$this->session->userdata('username'));
foreach($result as $row) {
$data = $result['result_array'];
}
但是这需要你设置一个模型函数来传递/返回$ query-&gt; result_array()。
无论如何,我希望这解决了你的form_validation问题。