Welcome Guest, Not a member yet? Register   Sign In
Form Helper - set_value()
#1

I have a form setup using the CI Form Helper, mainly the set_value() feature.

The same view file with the form is used for creating new "pages", as well as editing pages, ill use the page_title as an example input from my form.
PHP Code:
<input type="text" id="page_title" name="page_title" value="<?php echo set_value('page_title', @$settings->title); ?>" /> 

So initially, that works fine..
  • If editing a page, then $settings->title will be set and the value will be the page title
  • If creating a new page, it will be empty
  • If creating a new page, and the validation fails, the value will be the previously populated value
I also have it setup so if you select the Create another page checkbox, then it will load the form again, allowing you to re-populate the fields and create another page.

The question I have is, when you populate the values in the field, hit submit, and the page is successfully created, how can I have the form loaded, with the values then being empty?
Reply
#2

When someone checks the checkbox, and they get a new form to fill out, you want the fields all empty, not with the values that were entered in the previous instance of the form?

If yes, I did this. It involved extending the form validation library by creating a file called MY_form_validation.php in my application/libraries folder, and inserting this code.

PHP Code:
<?php
class MY_Form_validation extends CI_Form_validation {

   
 public function __construct()
 
   {
 
       parent::__construct();
   
 }

   
 public function clear_field_data()
 
   {
 
       $this->_field_data = array();
     
   return;
   
 }


So to clear out the fields, use $this->form_validation->clear_field_data()
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#3

(08-29-2015, 04:03 PM)RobertSF Wrote: When someone checks the checkbox, and they get a new form to fill out, you want the fields all empty, not with the values that were entered in the previous instance of the form?

If yes, I did this. It involved extending the form validation library by creating a file called MY_form_validation.php in my application/libraries folder, and inserting this code.


PHP Code:
<?php
class MY_Form_validation extends CI_Form_validation {

 
   public function __construct()
 
   {
 
       parent::__construct();
 
   }

 
   public function clear_field_data()
 
   {
 
       $this->_field_data = array();
 
       return;
 
   }


So to clear out the fields, use $this->form_validation->clear_field_data()

Answer: Yes

I was hoping there was a way to do it without extending the CI framework, (Trying to do as little of that as possible), but this way is definitely better than my way, thanks!
Reply
#4

(This post was last modified: 08-30-2015, 09:26 AM by jLinux.)

So, oddly enough, this doesnt work actually

Edit: Maybe were using diff CI versions? But the code for set_value doesnt even look for _field_data, just looks right at $this->post

form_helper.php:
PHP Code:
<?php
if ( ! function_exists('set_value'))
{
 
   /**
     * Form Value
     *
     * Grabs a value from the POST array for the specified field so you can
     * re-populate an input field or textarea. If Form Validation
     * is active it retrieves the info from the validation class
     *
     * @param    string    $field        Field name
     * @param    string    $default    Default value
     * @param    bool    $html_escape    Whether to escape HTML special characters or not
     * @return    string
     */
 
   function set_value($field$default ''$html_escape TRUE)
 
   {
 
       $CI =& get_instance();

 
       $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
 
           $CI->form_validation->set_value($field$default)
 
           $CI->input->post($fieldFALSE); // RIGHT HERE

 
       isset($value) OR $value $default;
 
       return ($html_escape) ? html_escape($value) : $value;
 
   }

Reply
#5

(This post was last modified: 08-30-2015, 09:43 AM by jLinux.)

So I couldnt find a good way to unset $_POST without causing more errors, like in Input::_fetch_from_array().

Just gonna set a variable $empty_values, then..
PHP Code:
<?php @$empty_values || print(set_value('menu_title', @$settings->menu_title)); ?>
Reply
#6

So sorry to have led you astray! Sad The solution I posted works under CI 2.x, and I guess version 3 changed the code in the set_value function. Here's the code for version 2.

PHP Code:
/**
 * Form Value
 *
 * Grabs a value from the POST array for the specified field so you can
 * re-populate an input field or textarea.  If Form Validation
 * is active it retrieves the info from the validation class
 *
 * @access    public
 * @param    string
 * @return    mixed
 */
if ( ! function_exists('set_value'))
{
    function 
set_value($field ''$default '')
    {
        if (
FALSE === ($OBJ =& _get_validation_object()))
        {
            if ( ! isset(
$_POST[$field]))
            {
                return 
$default;
            }

            return 
form_prep($_POST[$field], $field);
        }

        return 
form_prep($OBJ->set_value($field$default), $field);
    }

As you can see, here the function has two possible returns, one that uses $_POST and one that uses CI's internal storage.

Don't know how you feel about resorting to Javascript for this, but this function helps me when I want to prevent someone from returning to a previously filled and submitted form. They can return to it, but the form will be blank. It's extremely simple.
Code:
function formReset() {
    this.form.reset()
}
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#7

(08-30-2015, 09:49 AM)RobertSF Wrote: So sorry to have led you astray! Sad The solution I posted works under CI 2.x, and I guess version 3 changed the code in the set_value function. Here's the code for version 2.


PHP Code:
/**
 * Form Value
 *
 * Grabs a value from the POST array for the specified field so you can
 * re-populate an input field or textarea.  If Form Validation
 * is active it retrieves the info from the validation class
 *
 * @access public
 * @param string
 * @return mixed
 */
if ( ! function_exists('set_value'))
{
 function 
set_value($field ''$default '')
 {
 if (
FALSE === ($OBJ =& _get_validation_object()))
 {
 if ( ! isset(
$_POST[$field]))
 {
 return 
$default;
 }

 return 
form_prep($_POST[$field], $field);
 }

 return 
form_prep($OBJ->set_value($field$default), $field);
 }

As you can see, here the function has two possible returns, one that uses $_POST and one that uses CI's internal storage.

Don't know how you feel about resorting to Javascript for this, but this function helps me when I want to prevent someone from returning to a previously filled and submitted form. They can return to it, but the form will be blank. It's extremely simple.

Code:
function formReset() {
this.form.reset()
}

When you say "Here is the code for version 2", do you mean CI v2, or version 2 of your set_value? lol.

And I love JS, but not sure id like to use JS for this one.

My bandaid ..
PHP Code:
<?php @$empty_values || print(set_value('menu_title', @$settings->menu_title)); ?>

.. works ok for now.
On the top of CI form related stuff though... You can look at this other issue! lol
Reply
#8

Ha, ha, no, I meant here's the code as it is in CI v2.

I'll look at your other post, though a first glance tells me it's a bit out of my league.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB