I realise this request goes against the example provided in the CI documentation (which advises a separate 'success' page view), but I would like to reutilise a given form view after a form has been successfully submitted - displaying a success message then displaying a blank form. I've tried a few ways unsuccessfully to clear the validation set values (unsetting $_POST
, setting rules / fields to an empty array and rerunning validation).
我意识到这个请求违背了CI文档中提供的示例(建议单独的'成功'页面视图),但我希望在成功提交表单后重新使用给定的表单视图 - 显示成功消息然后显示空白表格。我尝试了几种方法来清除验证设置值(取消设置$ _POST,将规则/字段设置为空数组并重新运行验证)。
I could redirect to the same page, but then I'd have to set a session variable to display a success message - which is a messy approach.
我可以重定向到同一页面,但是我必须设置一个会话变量来显示成功消息 - 这是一个混乱的方法。
Any ideas how to best achieve the above?
任何想法如何最好地实现上述目标?
8 个解决方案
#1
34
Redirect to itself. This way, no submissions have been run... This also gives you a way to show the flash_data.
重定向到自己。这样,没有提交任何提交...这也为您提供了一种显示flash_data的方法。
$this->load->library('form_validation');
$this->form_validation->set_rules('firstname', 'First Name', 'required');
$this->form_validation->set_rules('surname', 'Sur Name', 'required');
if ($this->form_validation->run() === TRUE)
{
// save data
$this->session->set_flashdata('message', 'New Contact has been added');
redirect(current_url());
}
$this->load->view('contacts/add', $this->data);
#2
12
Another solution, extend the library CI_Form_validation
. The property $_field_data
is protected, so we can acces it:
另一种解决方案是扩展库CI_Form_validation。 $ _field_data属性受到保护,因此我们可以访问它:
class MY_Form_validation extends CI_Form_validation {
public function __construct()
{
parent::__construct();
}
public function clear_field_data() {
$this->_field_data = array();
return $this;
}
}
And call the new method. This way, you can pass data without storing data in session.
并调用新方法。这样,您可以在不在会话中存储数据的情况下传递数据。
class Item extends Controller
{
function Item()
{
parent::Controller();
}
function add()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'name', 'required');
$success = false;
if ($this->form_validation->run())
{
$success = true;
$this->form_validation->clear_field_data();
}
$this->load->view('item/add', array('success' => $success));
}
}
#3
7
Pass a TRUE/FALSE variable to your views that conditionally sets the values of the form.
将TRUE / FALSE变量传递给有条件地设置表单值的视图。
The Controller
控制器
if($this->form_validation->run())
{
$data['reset'] = TRUE;
}
else
{
$data['reset'] = FALSE:
}
$this->load->view("form", $data);
The View:
风景:
<input type="text" name="email" value="<?php echo ($reset) ? "" : set_value('email'); ?>" />
<input type="text" name="first_name" value="<?php echo ($reset) ? "" : set_value('first_name'); ?>" />
#4
4
The set_value function fetches its value from the Form_validation object and not from the $_POST array. The Form_validation object stores its own copy of the posted values in a variable called $_field_data.
set_value函数从Form_validation对象中获取其值,而不是从$ _POST数组中获取。 Form_validation对象将其自己的已发布值副本存储在名为$ _field_data的变量中。
Its a hack, but you could clear this variable after handling a successful submission :
它是一个黑客,但您可以在处理成功提交后清除此变量:
class Item extends Controller
{
function Item()
{
parent::Controller();
$this->load->model('item_model');
}
function add()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'name', 'required');
$success = false;
if ($this->form_validation->run())
{
$this->item_model->add_item($this->input->post('name'));
$success = true;
// Look away now. Hack coming up!
// Clear the form validation field data
$this->form_validation->_field_data = array();
}
$this->load->view('item/add', array('success' => $success));
}
}
#5
0
Hope this would be helpful. Finally I understand the whole concept of the extending the library. All you need to do is
Step 1: In this directory "application/libraries/" create a file named "MY_Form_validation.php" with the following php code
希望这会有所帮助。最后,我理解了扩展库的整个概念。您需要做的就是步骤1:在此目录“application / libraries /”中使用以下php代码创建名为“MY_Form_validation.php”的文件
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');
class MY_Form_validation extends CI_Form_validation {
public function MY_Form_validation() {
parent::__construct();
}
public function unset_field_data()
{
unset($this->_field_data);
}
}
Step 2: Then use the function "unset_field_data()
" in you controller. for example below:
步骤2:然后在控制器中使用“unset_field_data()”功能。例如下面:
if ($this->form_validation->run())
{
$this->item_model->add_item($this->input->post('name'));
$success = true;
$this->form_validation->unset_field_data();
}
#6
0
I've found that where:
我发现在哪里:
- there's more than one CI view-partials which make up the page and their controller methods perform validation
- 有多个CI视图 - 部分组成页面,其控制器方法执行验证
- one of the validations failed and produced an error since the last page-refresh, say with an incorrectly formatted or typed input field value
- 其中一个验证失败并自上次页面刷新后产生错误,例如格式不正确或键入的输入字段值
it isn't enough to clear only the validation-rules array, you need to clear the validation-error array too.
仅清除validation-rules数组是不够的,还需要清除validation-error数组。
To this end, I've added a method to system/libraries/Form_Validation.php, as follows:
为此,我在system / libraries / Form_Validation.php中添加了一个方法,如下所示:
public function clear_rules()
{
$this->_error_array = array();
$this->_field_data = array();
return $this;
}
Returning $this is important if you want to chain your form-validation methods.
如果您想链接表单验证方法,那么返回$这很重要。
#7
0
In newer version 3.X, to clear set_value , $_POST=array()
and $this->_field_data = array();
in MY_Form_validation.php
library. try
在较新的版本3.X中,要清除set_value,$ _POST = array()和$ this - > _ field_data = array();在MY_Form_validation.php库中。尝试
Clear form data after success codeigniter using php not jquery
成功codeigniter后使用php而不是jquery清除表单数据
#8
0
The answer from d5avard is wrong, the CI form validation should have the rules array parsed to it: If you don't do this you can use form validation with posted data, but not with over-ride data.
来自d5avard的答案是错误的,CI表单验证应该将规则数组解析为它:如果不这样做,您可以使用表单验证和发布的数据,但不能使用覆盖数据。
save this file as Libraries/MY_Form_validation.php
将此文件另存为Libraries / MY_Form_validation.php
/**
* Class MY_Form_validation
* @description extension of the CI_Form_validation
* @property CI_DB_query_builder db database driver
* @property CI_Benchmark benchmark
* @property CI_Input input
* @property CI_Output output
*/
class MY_Form_validation extends CI_Form_validation
{
/**
* MY_Form_validation constructor.
* @param array $rules
*/
function __construct($rules = array())
{
parent::__construct($rules);
$this->_error_prefix = '<div class=""><p>';
$this->_error_suffix = '</p></div>';
}
/**
* Resets the form validation class for multiple runs.
* @param array $rules
*/
public function initialise($rules = array())
{
if (count($rules) == 0 )
{
require (APPPATH.'config'.DIRECTORY_SEPARATOR.'form_validation.php');
$this->_config_rules = $config;
}
else
{
$this->_config_rules = $rules;
}
$this->_field_data = array();
$this->_error_array = array();
$this->_error_messages = array();
$this->_error_prefix = '<div class=""><p>';
$this->_error_suffix = '</p></div>';
$this->error_string = '';
}
}
#1
34
Redirect to itself. This way, no submissions have been run... This also gives you a way to show the flash_data.
重定向到自己。这样,没有提交任何提交...这也为您提供了一种显示flash_data的方法。
$this->load->library('form_validation');
$this->form_validation->set_rules('firstname', 'First Name', 'required');
$this->form_validation->set_rules('surname', 'Sur Name', 'required');
if ($this->form_validation->run() === TRUE)
{
// save data
$this->session->set_flashdata('message', 'New Contact has been added');
redirect(current_url());
}
$this->load->view('contacts/add', $this->data);
#2
12
Another solution, extend the library CI_Form_validation
. The property $_field_data
is protected, so we can acces it:
另一种解决方案是扩展库CI_Form_validation。 $ _field_data属性受到保护,因此我们可以访问它:
class MY_Form_validation extends CI_Form_validation {
public function __construct()
{
parent::__construct();
}
public function clear_field_data() {
$this->_field_data = array();
return $this;
}
}
And call the new method. This way, you can pass data without storing data in session.
并调用新方法。这样,您可以在不在会话中存储数据的情况下传递数据。
class Item extends Controller
{
function Item()
{
parent::Controller();
}
function add()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'name', 'required');
$success = false;
if ($this->form_validation->run())
{
$success = true;
$this->form_validation->clear_field_data();
}
$this->load->view('item/add', array('success' => $success));
}
}
#3
7
Pass a TRUE/FALSE variable to your views that conditionally sets the values of the form.
将TRUE / FALSE变量传递给有条件地设置表单值的视图。
The Controller
控制器
if($this->form_validation->run())
{
$data['reset'] = TRUE;
}
else
{
$data['reset'] = FALSE:
}
$this->load->view("form", $data);
The View:
风景:
<input type="text" name="email" value="<?php echo ($reset) ? "" : set_value('email'); ?>" />
<input type="text" name="first_name" value="<?php echo ($reset) ? "" : set_value('first_name'); ?>" />
#4
4
The set_value function fetches its value from the Form_validation object and not from the $_POST array. The Form_validation object stores its own copy of the posted values in a variable called $_field_data.
set_value函数从Form_validation对象中获取其值,而不是从$ _POST数组中获取。 Form_validation对象将其自己的已发布值副本存储在名为$ _field_data的变量中。
Its a hack, but you could clear this variable after handling a successful submission :
它是一个黑客,但您可以在处理成功提交后清除此变量:
class Item extends Controller
{
function Item()
{
parent::Controller();
$this->load->model('item_model');
}
function add()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'name', 'required');
$success = false;
if ($this->form_validation->run())
{
$this->item_model->add_item($this->input->post('name'));
$success = true;
// Look away now. Hack coming up!
// Clear the form validation field data
$this->form_validation->_field_data = array();
}
$this->load->view('item/add', array('success' => $success));
}
}
#5
0
Hope this would be helpful. Finally I understand the whole concept of the extending the library. All you need to do is
Step 1: In this directory "application/libraries/" create a file named "MY_Form_validation.php" with the following php code
希望这会有所帮助。最后,我理解了扩展库的整个概念。您需要做的就是步骤1:在此目录“application / libraries /”中使用以下php代码创建名为“MY_Form_validation.php”的文件
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');
class MY_Form_validation extends CI_Form_validation {
public function MY_Form_validation() {
parent::__construct();
}
public function unset_field_data()
{
unset($this->_field_data);
}
}
Step 2: Then use the function "unset_field_data()
" in you controller. for example below:
步骤2:然后在控制器中使用“unset_field_data()”功能。例如下面:
if ($this->form_validation->run())
{
$this->item_model->add_item($this->input->post('name'));
$success = true;
$this->form_validation->unset_field_data();
}
#6
0
I've found that where:
我发现在哪里:
- there's more than one CI view-partials which make up the page and their controller methods perform validation
- 有多个CI视图 - 部分组成页面,其控制器方法执行验证
- one of the validations failed and produced an error since the last page-refresh, say with an incorrectly formatted or typed input field value
- 其中一个验证失败并自上次页面刷新后产生错误,例如格式不正确或键入的输入字段值
it isn't enough to clear only the validation-rules array, you need to clear the validation-error array too.
仅清除validation-rules数组是不够的,还需要清除validation-error数组。
To this end, I've added a method to system/libraries/Form_Validation.php, as follows:
为此,我在system / libraries / Form_Validation.php中添加了一个方法,如下所示:
public function clear_rules()
{
$this->_error_array = array();
$this->_field_data = array();
return $this;
}
Returning $this is important if you want to chain your form-validation methods.
如果您想链接表单验证方法,那么返回$这很重要。
#7
0
In newer version 3.X, to clear set_value , $_POST=array()
and $this->_field_data = array();
in MY_Form_validation.php
library. try
在较新的版本3.X中,要清除set_value,$ _POST = array()和$ this - > _ field_data = array();在MY_Form_validation.php库中。尝试
Clear form data after success codeigniter using php not jquery
成功codeigniter后使用php而不是jquery清除表单数据
#8
0
The answer from d5avard is wrong, the CI form validation should have the rules array parsed to it: If you don't do this you can use form validation with posted data, but not with over-ride data.
来自d5avard的答案是错误的,CI表单验证应该将规则数组解析为它:如果不这样做,您可以使用表单验证和发布的数据,但不能使用覆盖数据。
save this file as Libraries/MY_Form_validation.php
将此文件另存为Libraries / MY_Form_validation.php
/**
* Class MY_Form_validation
* @description extension of the CI_Form_validation
* @property CI_DB_query_builder db database driver
* @property CI_Benchmark benchmark
* @property CI_Input input
* @property CI_Output output
*/
class MY_Form_validation extends CI_Form_validation
{
/**
* MY_Form_validation constructor.
* @param array $rules
*/
function __construct($rules = array())
{
parent::__construct($rules);
$this->_error_prefix = '<div class=""><p>';
$this->_error_suffix = '</p></div>';
}
/**
* Resets the form validation class for multiple runs.
* @param array $rules
*/
public function initialise($rules = array())
{
if (count($rules) == 0 )
{
require (APPPATH.'config'.DIRECTORY_SEPARATOR.'form_validation.php');
$this->_config_rules = $config;
}
else
{
$this->_config_rules = $rules;
}
$this->_field_data = array();
$this->_error_array = array();
$this->_error_messages = array();
$this->_error_prefix = '<div class=""><p>';
$this->_error_suffix = '</p></div>';
$this->error_string = '';
}
}