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

使用`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

    打赏 评论

相关推荐 更多相似问题