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?
在我的一个网站上,我在一个页面上有两个表单,我遇到了问题,有validation_errors();基本上发生了什么,是我检查错误的一种形式,如果有任何错误,我正在做一些样式来将标签变为红色,另一种形式如何使用echo validation_errors();显示错误。当我提交不显示错误的表单时,只是错误样式验证错误显示在表单中。我怎么能阻止这个?
2 个解决方案
#1
23
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:
您的问题有点难以阅读,但如果我理解正确 - 您无法从一个控制器验证2个单独的表单,或者使用validation_errors()处理来自不同表单的错误的问题,其中afaik打印所有错误:
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>
Then you can use any of these methods to check which form was submitted (This example checks if "form1" was submitted):
然后,您可以使用任何这些方法来检查提交的表单(此示例检查是否提交了“form1”):
<?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:
然后在您的视图中,您将使用:而不是使用validation_errors()。
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.
如果这没有帮助,请通过发布您的代码来澄清您的问题。
#2
0
What I did was divide both forms. The view would be like
我所做的是划分两种形式。视图就像
<?php echo validation_errors(); ?>
<?php echo form_open('form1'); ?>
<form id="form1" action="some_action">
//Inputs
</form>
<?php echo form_open('form2'); ?>
<form id="form2" action="other_action">
//Inputs
</form>
Now, in the controller you can have two different functions for each validation:
现在,在控制器中,每个验证可以有两个不同的函数:
//Controller
function some_action(){
//validate form and code
}
function other_action(){
//validate form2 and code
}
Now, all validation messages will appear in the same place but will only show the messages of each form. Hope that helps
现在,所有验证消息都将出现在同一位置,但仅显示每个表单的消息。希望有所帮助
#1
23
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:
您的问题有点难以阅读,但如果我理解正确 - 您无法从一个控制器验证2个单独的表单,或者使用validation_errors()处理来自不同表单的错误的问题,其中afaik打印所有错误:
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>
Then you can use any of these methods to check which form was submitted (This example checks if "form1" was submitted):
然后,您可以使用任何这些方法来检查提交的表单(此示例检查是否提交了“form1”):
<?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:
然后在您的视图中,您将使用:而不是使用validation_errors()。
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.
如果这没有帮助,请通过发布您的代码来澄清您的问题。
#2
0
What I did was divide both forms. The view would be like
我所做的是划分两种形式。视图就像
<?php echo validation_errors(); ?>
<?php echo form_open('form1'); ?>
<form id="form1" action="some_action">
//Inputs
</form>
<?php echo form_open('form2'); ?>
<form id="form2" action="other_action">
//Inputs
</form>
Now, in the controller you can have two different functions for each validation:
现在,在控制器中,每个验证可以有两个不同的函数:
//Controller
function some_action(){
//validate form and code
}
function other_action(){
//validate form2 and code
}
Now, all validation messages will appear in the same place but will only show the messages of each form. Hope that helps
现在,所有验证消息都将出现在同一位置,但仅显示每个表单的消息。希望有所帮助