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>