0

我有一个页面,上面有 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]');
4

2 回答 2

0

如果你为 thelogin和 for you ' 创建不同的规则会更好registration

如何?

创建一个Form_validation.php文件application/libraries

在里面指定你登录的规则和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 手册 - 验证配置文件中阅读更多内容

于 2013-07-18T02:37:29.277 回答
0

您是否在一页中使用两个提交?尝试通过 javascript 或 jquery 进行提交,因为当您提交提交时,它会在某些浏览器中提交整个页面

也试试这个显示每个字段的验证错误

 <?=form_error('loginEmail')?>

 <?=form_error('registerPassword')?>
于 2013-07-18T03:30:43.323 回答