简体   繁体   English

PHP Codeigniter:将动态表单数据传递到视图,而无需在验证错误时从数据库重新获取

[英]PHP Codeigniter: Passing dynamic form data to a view without having to refetch from the database upon validation errors

I am making a basic signup page using PHP Codeigniter. 我正在使用PHP Codeigniter制作基本的注册页面。

On the signup page, I ask the user to select from a selection of categories (via a <select> html element). 在注册页面上,我要求用户从多个类别中进行选择(通过<select> html元素)。 These categories are stored in a list in my MySQL Database. 这些类别存储在我的MySQL数据库的列表中。

My current method is to fetch this list from the DB when the user calls the function to load the page and then display it to them. 我当前的方法是,当用户调用该函数加载页面并将其显示给用户时,从数据库中获取此列表。 However, if the user enter's incorrect data and the page must be reloaded with the validation errors, the variable that holds the data in the list seems to be cleared, and I must refetch the list data from the database before redisplaying the page. 但是,如果用户输入的数据不正确,并且页面必须重新加载验证错误,则似乎清除了将数据保存在列表中的变量,并且在重新显示页面之前,我必须从数据库中重新获取列表数据。

I believe there's something in the documentation about how to set this variable to be permanently available but upon looking again, I had no luck in finding it. 我相信文档中有一些关于如何将此变量设置为永久可用的信息,但是再次查看时,我发现没有运气。

Could anyone possibly point me in the right direction? 有人能指出我正确的方向吗? It seems silly to me to have to need to refetch this data every time (I know that people won't be putting in wrong info often, but this will come in handy in a few situations). 对我来说,每次都需要重新获取这些数据似乎很愚蠢(我知道人们不会经常输入错误的信息,但这在某些情况下会派上用场)。

NOTE: This is not an issue regarding remembering user selections. 注意:这不是有关记住用户选择的问题。

You just need to set it as the default value, for example 您只需要将其设置为默认值,例如

<input type="text" name="username" value="<?php isset($_POST['username']) echo $username;?>" />

That way, $_POST['username'] will always be available. 这样, $_POST['username']将始终可用。

this example is for a select drop down list using ci form helper (i'm modifying this from another form so hopefully its all correct) 此示例适用于使用ci form helper的选择下拉列表(我正在从另一种表单修改此列表,因此希望它是正确的)

the Array of select values is: $categoryarray
the drop down field name is 'category'
the default value is $defaultcategory
a css class to style (bootstrap etc): $dropclass = 'class="input-medium"';

the line of code is: 代码行是:

  form_dropdown('category',$categoryarray,set_value('category',$defaultcategory),$dropclass).form_error('category');

the form_error('category'); form_error('category'); at the end is for showing a validation error message and even though there is a default value - if the form fails validation from another field in the form - this will 'remember' what the user selected. 最后是显示验证错误消息的方法,即使有默认值-如果表单无法通过表单中的其他字段进行验证-这也会“记住”用户选择的内容。

EDIT ! 编辑!

ok there is good and bad news. 好,有好消息和坏消息。 bad news - if the categories are coming from a database then you need to get them again. 坏消息-如果类别来自数据库,则需要再次获取它们。 good news - CI remembers what category the user selected from the drop down list. 好消息-CI会记住用户从下拉列表中选择的类别。

and the bad news actually isn't that big a deal - if you create the category array in your model. 坏消息实际上没什么大不了的-如果您在模型中创建类别数组。 then its just one line of code to add to the validation. 然后只需将一行代码添加到验证中。

// In the Model 

    function makedropdown() {

// get your category list 
$cats = $this->getAllCategories() ;

$categoryarray = array();

   // make the array 
 foreach ( $cats->result() as $row ) {

 $categoryarray[$row->category] =  $row->category ; }

return $categoryarray ;

}

someone has filled out the form, we run validation, validation fails. 有人填写了表格,我们运行验证,验证失败。 in the controller: 在控制器中:

if ( $this->form_validation->run($formrules) == FALSE ) {

// get the categoryarray
$data['categoryarray'] = $this->categorymodel->makedropdown() ; 

// load form again
$this->load->view( 'myform', $data ); }

So even though we are getting the categories again from the db to dynamically populate the select list - CI still remembers the users choice from the first time they did the form. 因此,即使我们再次从数据库获取类别以动态填充选择列表,CI仍会记住用户在第一次填写表格时的选择。

And what about a default category for the drop down? 下拉菜单的默认类别又如何呢? if its not going to change then it can be set as a config. 如果它不会改变,那么可以将其设置为配置。 if the category values are coming from a database and they can change - then the default category could be created in the model. 如果类别值来自数据库并且可以更改,则可以在模型中创建默认类别。

EDIT 2 编辑2

gosh i always do this anyway so why didnt i think of it for this. 天哪,我总是这样做,所以为什么我没有想到这个呢。 so yeah this is yet another reason to make a specific method for showing your view 是的,这是制作显示您的观点的特定方法的另一个原因

 function _showCategoryForm(){

 // get the categoryarray
$data['categoryarray'] = $this->categorymodel->makedropdown() ; 

// anything else thats needed for the view 

// load form view
$this->load->view( 'myform', $data ); }

NOW we dont have any repeated code, and its easy to customize the validation failure with an error message if needed. 现在,我们没有任何重复的代码,并且如果需要,可以轻松地通过错误消息来自定义验证失败。

// since i'm grinding on this - the validation should happen in a model 
// and that method returns true or false
if ( $this->somemodel->validateCategoryForm() == FALSE ) {

// custom obnoxious error message 
$this->formerrormessage = "What part of required is eluding you?" ;

$this->_showCategoryForm() ;  }

This is much better because if the needs of your form changes - the change is only in one place. 这样会更好,因为如果表单的需求发生了变化-更改只在一个地方进行。 Also i added an underscore to remind us all that private methods are a good practice. 我还加了一个下划线,以提醒我们所有人私有方法都是一种好习惯。 And the form validation should be separate in a model, that is called by the controller. 表单验证应该在控制器调用的模型中是分开的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM