我创建了一个表单来向我的数据库添加客户端,我使用表单验证类来验证表单,并使用validation_errors()来打印表单错误。
但是现在我想改变它,以便错误显示在字段旁边。我曾尝试使用form_error(),但除非我在if语句中回显它,检查表单验证已运行它不会显示错误消息。
我的控制
class Dwg_issue extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->library('form_validation');
$this->load->helper('form');
$this->load->model('model_issue');
}
public function client_add()
{
$data['main_content'] = 'client_add';
$this->load->view('includes/template.php', $data);
$this->form_validation->set_rules('clientName', 'Name', 'required|trim');
$this->form_validation->set_rules('clientSurname', 'Last name', 'required|trim');
$this->form_validation->set_rules('clientEmail', 'Email address', 'required|trim|valid_email|is_unique[client.clientEmail]');
$this->form_validation->set_rules('clientCom', 'Company Name', 'required|trim');
$this->form_validation->set_rules('clientPhone', 'Mobile number', 'required|trim');
$this->form_validation->set_rules('clientOfficeNo', 'Office number', 'required|trim');
$this->form_validation->set_rules('clientAddress', 'Office address', 'required|trim');
$this->form_validation->set_rules('clientPostel', 'Postel address', 'required|trim');
$this->form_validation->set_rules('clientVat', 'Vat numbrer', 'required|trim|numeric');
$this->form_validation->set_message('is_unique', 'This email address has already been registered. Please try again.');
if ($this->form_validation->run())
{
$this->model_issue->client_add();
redirect ('dwg_issue/client_info');
}
else
{
echo validation_errors();
}
}
我的观点
<h1>Add client details</h1>
<div id="body">
<p>Client information.</p>
<?php
echo form_open('dwg_issue/client_add');
echo validation_errors();
echo "<p><lable>Name:</lable>";
echo form_input('clientName',$this->input->post('clientName'));
echo form_error('clientName');
echo "</p>";
echo "<p><lable>Last name:</lable>";
echo form_input('clientSurname',$this->input->post('clientSurname'));
echo "</p>";
echo "<p><lable>Email address:</lable>";
echo form_input('clientEmail',$this->input->post('clientEmail'));
echo "</p>";
echo "<p><lable>Company Name:</lable>";
echo form_input('clientCom',$this->input->post('clientCom'));
echo "</p>";
echo "<p><lable>Mobile number:</lable>";
echo form_input('clientPhone',$this->input->post('clientPhone'));
echo "</p>";
echo "<p><lable>Office number:</lable>";
echo form_input('clientOfficeNo',$this->input->post('clientOfficeNo'));
echo "</p>";
echo "<p><lable>Office address:</lable>";
echo form_textarea('clientAddress',$this->input->post('clientAddress'));
echo "</p>";
echo "<p><lable>Postel address:</lable>";
echo form_textarea('clientPostel',$this->input->post('clientPostel'));
echo "</p>";
echo "<p><lable>Vat numbrer:</lable>";
echo form_input('clientVat',$this->input->post('clientVat'));
echo "</p>";
echo "<p>";
echo form_submit('edit_submit', 'Add');
echo "</p>";
echo form_close();
?>
<a href="<?php echo base_url() . "index.php/main/logout"; ?>">Logout</a>
<a href="<?php echo base_url() . "index.php/main/members"; ?>">Members Page</a>
<?php
echo anchor(base_url(). 'index.php/dwg_issue/client_info','Client list');
?>
<?php
if ($this->session->userdata('userlevel') == 1)
{
echo anchor(base_url().'index.php/user_admin/user_main','User maintenance');
}
else echo "User maintenance";
?>
</div>
答案 0 :(得分:0)
在控制器中执行以下操作:
<?php
class Dwg_issue extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->helper('form');
$this->load->model('model_issue');
}
public function client_add()
{
$data['main_content'] = 'client_add';
$this->load->view('includes/template.php', $data);
$this->form_validation->set_rules('clientName', 'Name', 'required|trim');
$this->form_validation->set_rules('clientSurname', 'Last name', 'required|trim');
$this->form_validation->set_rules('clientEmail', 'Email address', 'required|trim|valid_email|is_unique[client.clientEmail]');
$this->form_validation->set_rules('clientCom', 'Company Name', 'required|trim');
$this->form_validation->set_rules('clientPhone', 'Mobile number', 'required|trim');
$this->form_validation->set_rules('clientOfficeNo', 'Office number', 'required|trim');
$this->form_validation->set_rules('clientAddress', 'Office address', 'required|trim');
$this->form_validation->set_rules('clientPostel', 'Postel address', 'required|trim');
$this->form_validation->set_rules('clientVat', 'Vat numbrer', 'required|trim|numeric');
$this->form_validation->set_message('is_unique', 'This email address has already been registered. Please try again.');
if ($this->form_validation->run())
{
$this->model_issue->client_add();
redirect ('dwg_issue/client_info');
}
else
{
$data['form_errors'] = $this->_get_validation_errors();
$this->session->set_flashdata($data);
redirect('path/of/your/view');
}
}
/**
* @return array The errors generated during the validation.
*/
private function _get_validation_errors()
{
return array(
'clientName' => form_error('clientName', NULL, NULL),
'clientSurname' => form_error('clientSurname', NULL, NULL),
'clientEmail' => form_error('clientEmail', NULL, NULL),
'clientCom' => form_error('clientCom', NULL, NULL),
'clientPhone' => form_error('clientPhone', NULL, NULL),
'clientOfficeNo' => form_error('clientOfficeNo', NULL, NULL),
'clientAddress' => form_error('clientAddress', NULL, NULL),
'clientPostel' => form_error('clientPostel', NULL, NULL),
'clientVat' => form_error('clientVat', NULL, NULL),
);
}
}
在视图中进行修改,以使其保持这种状态:
<div id="body">
<p>Client information.</p>
<?php
$form_errors = $this->session->flashdata('form_errors');
echo form_open('dwg_issue/client_add');
echo validation_errors();
echo "<p><lable>Name:</lable>";
echo form_input('clientName',$this->input->post('clientName'));
if (count($form_errors)) {
echo "<div>" . $form_errors['clientName'] . "</div>";
}
echo "</p>";
echo "<p><lable>Last name:</lable>";
echo form_input('clientSurname',$this->input->post('clientSurname'));
if (count($form_errors)) {
echo "<div>" . $form_errors['clientSurname'] . "</div>";
}
echo "</p>";
echo "<p><lable>Email address:</lable>";
echo form_input('clientEmail',$this->input->post('clientEmail'));
if (count($form_errors)) {
echo "<div>" . $form_errors['clientEmail'] . "</div>";
}
echo "</p>";
echo "<p><lable>Company Name:</lable>";
echo form_input('clientCom',$this->input->post('clientCom'));
if (count($form_errors)) {
echo "<div>" . $form_errors['clientCom'] . "</div>";
}
echo "</p>";
echo "<p><lable>Mobile number:</lable>";
echo form_input('clientPhone',$this->input->post('clientPhone'));
if (count($form_errors)) {
echo "<div>" . $form_errors['clientPhone'] . "</div>";
}
echo "</p>";
echo "<p><lable>Office number:</lable>";
echo form_input('clientOfficeNo',$this->input->post('clientOfficeNo'));
if (count($form_errors)) {
echo "<div>" . $form_errors['clientOfficeNo'] . "</div>";
}
echo "</p>";
echo "<p><lable>Office address:</lable>";
echo form_textarea('clientAddress',$this->input->post('clientAddress'));
if (count($form_errors)) {
echo "<div>" . $form_errors['clientAddress'] . "</div>";
}
echo "</p>";
echo "<p><lable>Postel address:</lable>";
echo form_textarea('clientPostel',$this->input->post('clientPostel'));
if (count($form_errors)) {
echo "<div>" . $form_errors['clientPostel'] . "</div>";
}
echo "</p>";
echo "<p><lable>Vat numbrer:</lable>";
echo form_input('clientVat',$this->input->post('clientVat'));
if (count($form_errors)) {
echo "<div>" . $form_errors['clientVat'] . "</div>";
}
echo "</p>";
echo "<p>";
echo form_submit('edit_submit', 'Add');
echo "</p>";
echo form_close();
?>
我希望我有所帮助:)