Codeigniter 2 表单在一页上,validation_errors 问题 [英] Codeigniter 2 forms on one page, validation_errors problem

查看:22
本文介绍了Codeigniter 2 表单在一页上,validation_errors 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个网站上,我在一页上有 2 个表单,我遇到了问题,validation_errors(); 基本上发生的事情是针对我正在检查错误的表单之一如果有任何错误,我正在做一些样式来将标签变成红色,另一个表单如何使用 echo validation_errors(); 显示错误.当我提交不显示错误的表单时,只是错误样式验证错误显示在表单中.我怎样才能阻止这种情况?

On one of my websites I have 2 forms on one page, I am having a problem, with validation_errors(); basically what is happening, is for one of the forms I am checking for errors and if there are any errors I am doing some styling to turn the labels red, how the other form just display errors using echo validation_errors();. When I submit the form that does not display errors just error styling the validation errors are show in the form. How can I stop this?

推荐答案

你的问题有点难读,但如果我理解正确 - 你在验证来自一个控制器的 2 个单独的表单时遇到问题,或者处理错误的问题使用 validation_errors() 的不同形式,afaik 打印所有错误:

Your question is a bit hard to read, but if I understand correctly - you're having trouble validating 2 separate forms from one controller, or issues dealing with errors from different forms using validation_errors() which afaik prints ALL errors:

在运行验证之前,检查是否存在隐藏字段、表单独有的字段,或者您可以检查特定提交按钮的值.

Before running validation, check for the existence of either a hidden field, a field that is unique to the form, or you can check the value of the particular submit button.

<form>
<input type="hidden" name="form1" value="whatever">
<input name="form1_email" />
<input type="submit" value="Submit Form 1" />
</form>

然后您可以使用其中任何一种方法来检查提交的是哪个表单(此示例检查是否提交了form1"):

Then you can use any of these methods to check which form was submitted (This example checks if "form1" was submitted):

<?php
// Choose one:
if ($this->input->post('form1')): // check the hidden input
if ($this->input->post('form1_email')): // OR check a unique value
if ($this->input->post('submit') == 'Submit Form 1'): // OR check the submit button value

    if ($this->form_validation->run()):

        // process form

    else:
            // Create a variable with errors assigned to form 1
            // Make sure to pass this to your view
            $data['form1_errors'] = validation_errors();
    endif;

endif;
// Do same for form 2

那么在您看来,您将使用:

Then in your view, instead of using validation_errors() you would use:

if (isset($form1_errors)) echo $form1_errors; // Print only form1's errors

如果这没有帮助,请告诉我,并通过发布您的代码来澄清您的问题.

If this doesn't help let me know, and clarify your question by posting your code.

这篇关于Codeigniter 2 表单在一页上,validation_errors 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆