Codeigniter表单验证...形式重新填充

时间:2022-10-06 16:09:36

I am in the process of creating an input form on a record specific page e.g. (property/rentals/rec_id). On submission, the data should be inserted, validated and return to the original page from which it was sent with any relevant messages.

我正在特定记录页面上创建输入表单,例如(属性/出租/·REC_ID)。提交时,应插入,验证数据并返回到发送数据的原始页面以及任何相关消息。

  • If the validation fails, reload the form page a re-populate the form
  • 如果验证失败,请重新加载表单页面以重新填充表单

  • Echo the specific validation error - e.g. invalid email address etc

    回应特定的验证错误 - 例如电子邮件地址无效等

  • If successful, insert data, reload the page and display success message.

    如果成功,请插入数据,重新加载页面并显示成功消息。

    function class() {

    function class(){

    $this->load->library('form_validation');    
    $this->form_validation->set_rules('name','name','required|trim');
    $this->form_validation->set_rules('email','email','required|trim|valid_email');
    
    $fields['name'] = 'Name';
    $fields['email'] = 'Email Address';
    
    $this->validation->set_fields($fields);
    
    if ($this->form_validation->run() == FALSE) 
    {
        //failed - reload page and re-populate fields with any error messages
    }
    else
    {
         // display form with success message
    }
    

    }

Normally this is simply done by 'loading a view' and passing the relevant data to repopulate the page, fields or display messages. But I have to use a 'redirect' back to the product specific page. Is there any good way to do this other than using session data to carry the validation errors..?

通常,这可以通过“加载视图”并传递相关数据来重新填充页面,字段或显示消息来完成。但我必须使用“重定向”回到产品特定页面。除了使用会话数据来传递验证错误之外,还有什么好方法吗?

Thanks in advance, Michael

迈克尔,提前谢谢

5 个解决方案

#1


Take a look at the CodeIgniter manual on Sessions and see if 'FlashData' is of any use.

查看Sessions上的CodeIgniter手册,看看'FlashData'是否有用。

#2


You may use the set_value('field_name') function to re-populate the form. Sample usage in view:

您可以使用set_value('field_name')函数重新填充表单。视图中的示例用法:

<input type="text name="name" value="<?php echo set_value('name'); ?>" />"

“/>”

To display the errors from the form_validation, you may use the validation_errors() function. Sample usage in view:

要显示form_validation中的错误,可以使用validation_errors()函数。视图中的示例用法:

<?php echo validation_errors(); ?>

Also, see link text for more info.

另外,请参阅链接文本以获取更多信息。

#3


   $insertData=array(
            'name'=>NULL,
            'email'=>NULL);
 //setting validation rules
    $this->form_validation->set_rules('name', 'Name', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required|email');

//here you will get all data which user entered in form 
$insertData = $this->input->post(NULL, TRUE);

if ($this->form_validation->run() == FALSE) 
{
    //failed - reload page and re-populate fields with any error messages
    $this->load->view('view_name',$insertData)    

}
else
{
     //set flash data for displaying success message in redirected page
     $this->session->set_flashdata('success','Success message')
     redirect('controller_name');

}

view page

<?php if ($this->session->flashdata('succes_message')) { echo    $this->session->flashdata('succes_message'); ?> } ?>

<form action='action page url' method='post'>
<input type='text' name='name' value='<?php echo $name ?>'/>
<input type='text' name='email' value='<?php echo $email ?>'/>
<input type='submit' value='Save'/>

#4


Session data would be the simplest and most accurate/efficient way to send the data back to the form. You could use cookies as well, but it would be less secure (since cookie data is sent with every http request).

会话数据是将数据发送回表单的最简单,最准确/最有效的方式。您也可以使用cookie,但它不太安全(因为cookie数据随每个http请求一起发送)。

Your other option would be to store the details in the database and fetch them again after the redirect, but I feel like that would be adding unneeded complexity to your application.

您的另一个选择是将详细信息存储在数据库中,并在重定向后再次获取它们,但我觉得这会为您的应用程序添加不必要的复杂性。

For a really simple solution you could even just dump your POST/GET data into a SESSION variable then call it up quickly on the page being redirected to, almost making the redirect transparent from a logic point of view.

对于一个非常简单的解决方案,您甚至可以将POST / GET数据转储到SESSION变量中,然后在重定向到的页面上快速调用它,从逻辑的角度来看几乎使重定向变得透明。

session_start();
$_SESSION['POST_DATA'] = $_POST;

#5


To alleviate the issue you could just do client side validation using Javascript/jQuery. So, basically, instead of immediately going to the new page, the Submit action would just get redirected to your javascript function, which would decide if the form is valid. Then you could use jQuery to make things look pretty for invalid elements.

为了缓解这个问题,您可以使用Javascript / jQuery进行客户端验证。因此,基本上,不是立即转到新页面,而是将提交操作重定向到您的javascript函数,这将决定表单是否有效。然后你可以使用jQuery使无效元素看起来很漂亮。

#1


Take a look at the CodeIgniter manual on Sessions and see if 'FlashData' is of any use.

查看Sessions上的CodeIgniter手册,看看'FlashData'是否有用。

#2


You may use the set_value('field_name') function to re-populate the form. Sample usage in view:

您可以使用set_value('field_name')函数重新填充表单。视图中的示例用法:

<input type="text name="name" value="<?php echo set_value('name'); ?>" />"

“/>”

To display the errors from the form_validation, you may use the validation_errors() function. Sample usage in view:

要显示form_validation中的错误,可以使用validation_errors()函数。视图中的示例用法:

<?php echo validation_errors(); ?>

Also, see link text for more info.

另外,请参阅链接文本以获取更多信息。

#3


   $insertData=array(
            'name'=>NULL,
            'email'=>NULL);
 //setting validation rules
    $this->form_validation->set_rules('name', 'Name', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required|email');

//here you will get all data which user entered in form 
$insertData = $this->input->post(NULL, TRUE);

if ($this->form_validation->run() == FALSE) 
{
    //failed - reload page and re-populate fields with any error messages
    $this->load->view('view_name',$insertData)    

}
else
{
     //set flash data for displaying success message in redirected page
     $this->session->set_flashdata('success','Success message')
     redirect('controller_name');

}

view page

<?php if ($this->session->flashdata('succes_message')) { echo    $this->session->flashdata('succes_message'); ?> } ?>

<form action='action page url' method='post'>
<input type='text' name='name' value='<?php echo $name ?>'/>
<input type='text' name='email' value='<?php echo $email ?>'/>
<input type='submit' value='Save'/>

#4


Session data would be the simplest and most accurate/efficient way to send the data back to the form. You could use cookies as well, but it would be less secure (since cookie data is sent with every http request).

会话数据是将数据发送回表单的最简单,最准确/最有效的方式。您也可以使用cookie,但它不太安全(因为cookie数据随每个http请求一起发送)。

Your other option would be to store the details in the database and fetch them again after the redirect, but I feel like that would be adding unneeded complexity to your application.

您的另一个选择是将详细信息存储在数据库中,并在重定向后再次获取它们,但我觉得这会为您的应用程序添加不必要的复杂性。

For a really simple solution you could even just dump your POST/GET data into a SESSION variable then call it up quickly on the page being redirected to, almost making the redirect transparent from a logic point of view.

对于一个非常简单的解决方案,您甚至可以将POST / GET数据转储到SESSION变量中,然后在重定向到的页面上快速调用它,从逻辑的角度来看几乎使重定向变得透明。

session_start();
$_SESSION['POST_DATA'] = $_POST;

#5


To alleviate the issue you could just do client side validation using Javascript/jQuery. So, basically, instead of immediately going to the new page, the Submit action would just get redirected to your javascript function, which would decide if the form is valid. Then you could use jQuery to make things look pretty for invalid elements.

为了缓解这个问题,您可以使用Javascript / jQuery进行客户端验证。因此,基本上,不是立即转到新页面,而是将提交操作重定向到您的javascript函数,这将决定表单是否有效。然后你可以使用jQuery使无效元素看起来很漂亮。