dqhbuwrwq692118284 2014-05-30 17:40
浏览 49
已采纳

CodeIgniter,在相应的div中显示验证错误

I have three forms in a page. Say, one for login, one for register and one for forgot password. All these form actions and the same. During the submit, submitted form is identified by a hidden input value.

echo form_hidden('formname', 'registration');
echo form_hidden('formname', 'login');
echo form_hidden('formname', 'assistance');

Every form have set of validation rules such as valid_email, min_length etc. and errors are thrown if invalid submission. But the problem is, when errors are thrown it is shown all above the three forms. Because every form have validation_errors() above it. And I cannot have single validation_errors in my page.

So is there any way I can place the error messages corresponding to login form in a <div> which ID is login-errors and registration form's errors in <div> which ID is registration-errors etc.?

<div id='registration-errors'>
<?php
validation_errors(); // For error messages of registration form
echo form_open('account', array('method'=>"POST", 'autocomplete'=>"off", 'class'=>'user-register'));
// Hidden input + Other inputs and labels + Submit button
?>
</div>


<div id='login-errors'>
<?php
validation_errors(); // For error messages of registration form
echo form_open('account', array('method'=>"POST", 'autocomplete'=>"off", 'class'=>'user-login'));
// Hidden input + Other inputs and labels + Submit button
?>
</div>

<div id='assistance-errors'>
<?php
validation_errors(); // For error messages of forgot password form
echo form_open('account', array('method'=>"POST", 'autocomplete'=>"off", 'class'=>'user-assistance'));
// Hidden input + Other inputs and labels + Submit button
?>
</div>
  • 写回答

3条回答 默认 最新

  • douhan8892 2014-05-30 18:09
    关注

    Define your validation errors in your controller.

    In the view

    <?php if(isset($registration_errors)){
         echo $registration_errors;
    }
    
    echo form_open('account', array('method'=>"POST", 'autocomplete'=>"off", 'class'=>'user-register'));
    // Hidden input + Other inputs and labels + Submit button
    ?>
    
    <?php if(isset($login_errors)){    
         echo $login_errors;
    }
    
    echo form_open('account', array('method'=>"POST", 'autocomplete'=>"off", 'class'=>'user-login'));
    // Hidden input + Other inputs and labels + Submit button
    ?>
    
    
    <?php if(isset($assistance_errors)){    
         echo $assistance_errors;
    }
    echo form_open('account', array('method'=>"POST", 'autocomplete'=>"off", 'class'=>'user-assistance'));
    // Hidden input + Other inputs and labels + Submit button
    ?>
    

    In the controller where you form validation takes place set errors based on which submit button is hit.

    So if, for example, the Registration submit button is hit:

         if(isset($_POST['registration']){ //or whatever the name of the relevant input/submit value 
    
              if(!$this->form_validation->run()) {
                  $vars['registration_errors'] = validation_errors();
              }else{
                 //success stuff
              }
          }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧