简体   繁体   English

我应该使用set_value()在CodeIgniter中重新填充表单

[英]Should I use set_value() to repopulate a form in CodeIgniter

My question is whether I should use set_value() at all to re-populate a form. 我的问题是我是否应该使用set_value()来重新填充表单。 It might seem odd to say that, however I am creating a shared controller function and view which can be used to either add a new record or edit an existing one. 这可能看起来很奇怪,但是我正在创建一个共享控制器功能和视图,可用于添加新记录或编辑现有记录。 It seems to make sense to do this since the functionality is so incredibly alike. 这样做似乎很有意义,因为功能非常相似。

As such, if we call up an existing record to edit I do this: 因此,如果我们调用现有记录进行编辑,我会这样做:

$data['fields'] = $this->customer_model->get_customer($id);

If the form is submitted to save the record, or if we're adding a record for the first time, the form has the potential to reload if the user makes a mistake so I populate $data['fields'] this way instead: 如果表单被提交以保存记录,或者我们第一次添加记录,则表单有可能在用户出错时重新加载,因此我以这种方式填充$ data ['fields']:

$data['fields'] = array(
    'company' => $this->input->post('company') ?: '',
    'website' => $this->input->post('website') ?: '',
    'credit_limit' => $this->input->post('credit_limit') ?: ''
);

My form element looks like this: 我的表单元素如下所示:

<input type="text" name="company" value="<?php echo set_value('company', $fields['company']); ?>"  />

But I'm thinking it may as well look like this: 但我认为它可能看起来像这样:

<input type="text" name="company" value="<?php echo escape_html($fields['company']); ?>"  />

Since the form data could come from either user input (when adding or saving) or from the database (when retrieving a record to edit) I cannot rely entirely on post() or set_value() without the 2nd parameter. 由于表单数据可以来自用户输入(添加或保存时),也可以来自数据库(当检索要编辑的记录时),我不能完全依赖post()set_value()而不使用第二个参数。 Furthermore, the second parameter for set_value() will always exist ( $fields['company'] in this example) because it's initialized from the start, which is why I am thinking of just using it directly. 此外,set_value()的第二个参数将始终存在(在此示例中$fields['company'] ),因为它是从头开始初始化的,这就是我想直接使用它的原因。

Is there a problem with this approach? 这种方法有问题吗?

If you want to populate form fields on FALSE return of Form Validation or insert data for editing operations, I suggest you to use following helper: 如果要在FALSE返回表单验证时填写表单字段或插入数据以进行编辑操作,我建议您使用以下帮助程序:

Usage 用法

<input type="text" name="last_name" value="<?=vset_value('last_name','',$rs);?>">

Explanation 说明

$rs here is the $db data for record (if you are sending it to view). $rs这里是记录的$ db数据(如果你要发送它来查看)。 To stay at the safe side please include $this->data['rs'] = false; 为了保持安全,请包括$this->data['rs'] = false; at your controller. 在你的控制器。 If $rs is set and true, helper take results from it and display it. 如果$rs设置为true,则helper从中获取结果并显示它。 Otherwise it displays if the key exist in $_POST . 否则,如果密钥存在于$_POST则显示。 If both don't exists, it display default value. 如果两者都不存在,则显示默认值。

Helper 帮手

/**
 * vayes_helper::vset_value
 *
 * Retains state of INPUT text after Form Validation
 *
 * @access  public
 * @param string  name of INPUT tag
 * @param string  default value for INPUT tag
 * @param mixed   DB Result (array or object)
 * @return  string
 */
if(!function_exists('vset_value')) {
  function vset_value ($name_of_input,$default_state='',$db_result_array='') {
    $CI = &get_instance();
    $render_state = $default_state;
    if($CI->input->post()) {
      $render_state = $CI->input->post($name_of_input);
    } else {
      if(is_object($db_result_array) && isset($db_result_array->$name_of_input)) {
        $render_state = (isset($db_result_array->$name_of_input)) ? $db_result_array->$name_of_input : $default_state;
      } else if($db_result_array != '' && array_key_exists($name_of_input,$db_result_array)) {
        $render_state = (isset($db_result_array[$name_of_input])) ? $db_result_array[$name_of_input] : $default_state;
      }
    }
    return $render_state;
  }
}

If you like the way, let me know. 如果您喜欢这种方式,请告诉我。 I can supply for more form input type like select, checkbox, etc. 我可以提供更多表单输入类型,如选择,复选框等。

The approach is correct, as mentioned in the CodeIgniter docs. 这种方法是正确的,如CodeIgniter文档中所述。 In fact, you don't need to include the second parameter in set_value. 实际上,您不需要在set_value中包含第二个参数。

set_value definition: set_value定义:

string set_value(string $field = '', string $default = '')

//$field: If there is a submitted field with this name, its value is returned.
//$default: If there is no matching submitted field, this value is returned.

Yes,You should. 是的你应该。

 set_value() is used to re-populate a form has failed validation. 
  There is no additional filtering on it,  so it faster.

 But, I prefer some times to use $this->input->post() for the secure.

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

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