dongzhi9032 2018-04-12 03:43
浏览 54
已采纳

使用`form_validation.php`时如何使用CodeIgniter的`set_value()`函数?

Problem:

I've got a form I'm trying to re-populate via the CodeIgniter docs, and I'm trying to use the set_value() function in my view file, but I receive an error: Message: Call to undefined function set_value().

I saw elsewhere on StackOverflow solutions to a similar problem when using the set_rules() function to define validations on a field-by-field basis.

However, I've got all of my validations together in config/form_validation.php, as sets of rules. Because all of my validations are defined within the rule set, it seems a little different than when defining field-by-field.

My rule set, controller, model and view methods are below.

Any idea why my re-population is not working?

Thanks for any insight anyone can offer, I'm new to CodeIgniter and I might be misunderstanding how things are working.


Validation Rules via config/form-validation.php:

$config = array(
  'create' => array (
    array(
      'field' => 'name',
      'label' => 'product name',
      'rules' => 'trim|required|min_length[5]',
    ),
    array(
      'field' => 'description',
      'label' => 'product description',
      'rules' => 'trim|required|min_length[5]',
    ),
    array(
      'field' => 'price',
      'label' => 'product price',
      'rules' => 'trim|required|min_length[1]|less_than[100000]|decimal',
    ),
  ),
);

In my Controller:

  public function create()
    {
    // XSS FILTER $POST OBJECT
    $new_product = $this->input->post(null, true);

    // SEND TO MODEL
    $this->load->model("Product_model");
    $product = $this->Product_model->add_product($new_product);

    // IF VALIDATIONS RETURN FALSE, STORE ERRORS IN FLASH SESSION
    if ($product[0] === FALSE)
    {
      $this->session->set_flashdata('errors', $product[1]);
      redirect("/products/new");
    }

    // IF VALIDATION PASSES, SEND HOME
    redirect("/");
  }

In my Model:

 public function add_product($product)
  {

    $this->load->library("form_validation");

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

      // STORE ERRORS & SHIP BACK TO CONTROLLER:
      return array(FALSE, validation_errors());
    } 
    else 
    {
      // Success, Escape strings and insert to DB and return TRUE (or item)
    }
  }

In my form View:

(The page which displays returned errors above the form)

 <form action="./create" method="POST">
  <input type="text" name="name" id="name" value="<?php echo set_value('name'); ?>">
  <textarea name="description" id="description" cols="30" rows="10" value="<?php echo set_value('description'); ?>"></textarea>
  <input type="number" name="price" id="price" value="<?php echo set_value('price'); ?>">
  <input type="submit" value="Create">
</form>
  • 写回答

1条回答 默认 最新

  • doudong8713 2018-04-12 05:34
    关注

    Hope this will help you :

    add form_helper in your controller or in autoload.php

    In controller :

         public function __construct()
         {
            parent::__construct();
            $this->load->helper('form');
         }
    

    Or in autoload.php :

        $autoload['helper'] = array('form','url');
    

    For more : https://www.codeigniter.com/userguide3/libraries/form_validation.html#re-populating-the-form

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图