Php Codeigniter表单\验证表单\错误()

Php Codeigniter表单\验证表单\错误(),php,codeigniter,validation,Php,Codeigniter,Validation,我创建了一个表单,将客户机添加到数据库中,并使用表单验证类验证表单,使用validation_errors()打印表单错误 但是现在我想更改它,以便在字段旁边显示错误。我曾尝试使用form_error(),但除非在if语句中对其进行检查,否则表单验证已运行,它不会显示错误消息 我的控制 class Dwg_issue extends CI_Controller { public function __construct(){ parent::__construct(); $th

我创建了一个表单,将客户机添加到数据库中,并使用表单验证类验证表单,使用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>
添加客户端详细信息
客户信息


在控制器中执行以下操作:

<?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), 
        );
    }
}


我希望我已经提供了帮助:)

您的问题是什么?您看到了吗?我想说的是表单验证类似乎没有将信息传递给视图。我可以让if语句的else部分在视图中回显错误,让form_error()根据输入进行回显。我得到了答案。我必须在if语句的else部分再次调用视图。
<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();

?>