显示正确的错误验证消息

时间:2013-07-17 21:26:58

标签: php codeigniter validation error-handling controller

我有一个页面,上面有2个表格:注册表格和登录表格。每个表单都有一个提交按钮。现在我正在验证这两个表单,但是例如,如果我按下注册表单的提交按钮,我只想显示注册表单的错误消息,而不是登录表单的错误消息。目前正显示两条错误消息。有办法解决这个问题吗?

<div class="grid-container">

    <div class="grid-50 login">
        <h3>Inloggen</h3>
        <?php

            $loginEmail = array('placeholder' => "Email", 'name' => "loginEmail");
            $loginPassword = array('placeholder' => "Wachtwoord", 'name' => "loginPassword");
            $loginSubmit = array('name' => "loginSubmit", 'class' => "btn", 'value' => "Inloggen");
            $loginForgot = array('name' => "loginForgot", 'class' => "link", 'value' => "Wachtwoord vergeten?");

            echo form_open('login/inloggen', array('class' => 'grid-100 formc'));
            echo form_input($loginEmail);
            echo form_password($loginPassword);
            echo form_submit($loginSubmit);
            echo form_submit($loginForgot);
            echo form_close();
        ?>
        <?php echo validation_errors('<p class="error">');?>
    </div>



    <div class="grid-50 login">
        <h3>Registreren</h3>

        <?php

            $registerName = array('placeholder' => "Naam", 'name' => "registerName");
            $registerEmail = array('placeholder' => "Email", 'name' => "registerEmail");
            $registerPassword = array(  'placeholder' => "Wachtwoord", 'name' => "registerPassword");


    $registerSubmit = array('name' => "registerSubmit", 'class' => "btn", 'value' => "Registreer");

        echo form_open('login/register');
        echo form_input($registerName, set_value('registerName'));
        echo form_input($registerEmail, set_value('registerEmail'));
        echo form_password($registerPassword);
        echo form_submit($registerSubmit);
        echo form_close();
    ?>

    <?php echo validation_errors('<p class="error">');?>
</div>

控制器中的验证

$this->form_validation->set_rules('registerEmail', 'Email verkeerd', 'trim|required|valid_email');
        $this->form_validation->set_rules('registerPassword', 'Password te kort', 'trim|required|min_length[4]');

2 个答案:

答案 0 :(得分:0)

如果您为loginregistration创建不同的规则会更好。

如何?

Form_validation.php

上创建application/libraries个文件

inside指定您登录的规则和register示例

$config = array(
  'login_validation_rules'=>array(
        array(
        'field' => 'username',
        'label' => 'The User Name',
        'rules' => 'required'
    ),
        array(
        'field' => 'password',
        'label' => 'The Password',
        'rules' => 'required'
    )

   ),
   'registration_validation_rules'=>array(
        array(
        'field' => 'email',
        'label' => 'The Email',
        'rules' => 'required|valid_email'
    ),
        array(
        'field' => 'firstname',
        'label' => 'The Firstname',
        'rules' => 'required'
    )
   )
 );

然后,在您controller上,您可以了解他们提交的表单以及要使用的验证规则示例

if($this->input->post('registerSubmit'))
{
  if($this->form_validation->run('registration_validation_rules') == FALSE)
  {
   //error
  }else{
   //good
  }
}elseif($this->input->post('loginSubmit')){
  if($this->form_validation->run('login_validation_rules') == FALSE)
  {
   //error
  }else{
   //good
  }
}

与此同时,你的控制者不会被规则搞得一团糟。使阅读更容易,您可以在Codigniter manual - validation config file

上阅读更多内容

答案 1 :(得分:0)

你在一个页面中使用两个提交吗?尝试通过javascript或jquery提交一个 当你提交提交时,它会在某些浏览器中提交整个页面

也尝试为每个字段显示验证错误

 <?=form_error('loginEmail')?>

 <?=form_error('registerPassword')?>