Exemple #1
0
 /**
  * Saves input from the $input parameter (array) into the form model's
  * field_data array if the key is present in the $fields array then 
  * serializes the field_data array to the session.
  * 
  * The $fields array is a simple array. Only the fields declared in 
  * the $field array will be stored.
  *
  * <code>
  *		// save form input data
  *		ExampleForm::save_input( array( 'first_name', 'last_name' ) );
  * </code>
  *
  * @param  array   $fields
  * @param  array   $input
  */
 public static function save_input($fields = null, $input = null)
 {
     // $fields must be an array
     if (!is_array($fields) && !is_null($fields)) {
         return false;
     }
     // by default we save all fields
     if (is_null($fields)) {
         $fields = array_keys(Input::all());
     }
     // by default we save all input, this can be overridden by passing
     // a second parameter to the save_input() method
     if (is_null($input)) {
         $input = Input::all();
     }
     // when storing input it's important to load the persistent form
     // data that may exist from previous requests, otherwise we will
     // overwrite them
     if (empty(static::$field_data)) {
         static::unserialize_from_session();
     }
     // ideally we'll have either a value for a field or an empty value
     // for a field. this isn't strictly necessary and may change in the
     // future given an appropriately convincing argument
     foreach ($fields as $field_name) {
         if (Input::has($field_name)) {
             static::$field_data[$field_name] = Input::get($field_name);
         } else {
             static::$field_data[$field_name] = '';
         }
     }
     // serialize the field data to session
     static::serialize_to_session();
 }