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

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘