diff --git a/Formbuilder/Formbuilder.php b/Formbuilder/Formbuilder.php index c7202b0..c9634e2 100644 --- a/Formbuilder/Formbuilder.php +++ b/Formbuilder/Formbuilder.php @@ -28,28 +28,10 @@ class Formbuilder { /** - * @var array Contains the form_hash and serialized form_structure from an external source (db) + * @var array Holds the form id and array form structure * @access protected */ - protected $_container; - - /** - * Form ID, useful for storing in a database, etc - * @var type - */ - protected $_form_id; - - /** - * @var array Holds the form source in raw array form - * @access protected - */ - protected $_structure; - - /** - * @var array Holds the form source in serialized form - * @access protected - */ - protected $_structure_ser; + protected $_form_array; /** @@ -64,67 +46,27 @@ public function __construct($form = false){ // Set the serialized structure if it's provided // otherwise, store the source if(array_key_exists('form_structure', $form)){ - - $this->_container = $form; // set the form as the container - $this->_structure_ser = $form['form_structure']; // pull the encoded form - $this->_structure = $this->retrieve(); // unserialize the form as the raw structure - + $form['form_structure'] = json_decode($form['form_structure']); + $this->_form_array = $form; } else if(array_key_exists('frmb', $form)){ - - $this->_form_id = ($form['form_id'] == "undefined" ? false : $form['form_id']); - $this->_structure = $form['frmb']; // since the form is from POST, set it as the raw array - $this->_structure_ser = $this->store(); // encode it - $this->rebuild_container(); // rebuild a new container - + $_form = array(); + $_form['form_id'] = ($form['form_id'] == "undefined" ? false : $form['form_id']); + $_form['form_structure'] = $form['frmb']; // since the form is from POST, set it as the raw array + $this->_form_array = $_form; } return true; } /** - * Wipes and re-saves the structure and hash to the containing array. - * - * @access protected - * @return boolean - */ - protected function rebuild_container(){ - $this->_container = array(); - $this->_container['form_structure'] = $this->_structure_ser; - return true; - } - - - /** - * Takes an array containing the form admin information - * and serializes it for storage in the database. Provides a hash - * that can will be used later during rendering. - * - * The array provided is typically from $_POST generated by the jquery - * plugin. + * Returns the form array with the structure encoded, for saving to a database or other store * * @access public * @return array */ - public function store(){ - $this->_structure_ser = json_encode($this->_structure); - return array('form_id'=>$this->_form_id,'form_structure'=>$this->_structure_ser); - } - - - /** - * Returns a serialized form back into it's original array, for use - * with rendering. - * - * @param string $form_array - * @access public - * @return boolean - */ - public function retrieve(){ - if(is_array($this->_container) && array_key_exists('form_structure', $this->_container)){ - return json_decode($this->_container['form_structure']); - } - return false; + public function get_encoded_form_array(){ + return array('form_id'=>$this->_form_array['form_id'],'form_structure'=>json_encode($this->_form_array['form_structure'])); } @@ -132,21 +74,10 @@ public function retrieve(){ * Prints out the generated json file with a content-type of application/json * * @access public - * @uses generate_json */ public function render_json(){ header("Content-Type: application/json"); - print $this->_structure_ser; - } - - - /** - * Builds a json object that the jquery plugin will parse - * - * @access public - */ - public function generate_json(){ - return json_encode( $this->_structure ); + print json_encode( $this->_form_array ); } @@ -175,12 +106,12 @@ public function generate_html($form_action = false){ $form_action = $form_action ? $form_action : $_SERVER['PHP_SELF']; - if(is_array($this->_structure)){ + if(is_array($this->_form_array['form_structure'])){ $html .= '
' . "\n"; $html .= '
    '."\n"; - foreach($this->_structure as $field){ + foreach($this->_form_array['form_structure'] as $field){ $html .= $this->loadField($field); } @@ -208,8 +139,8 @@ public function process(){ $results = array(); // Put together an array of all expected indices - if(is_array($this->_structure)){ - foreach($this->_structure as $field){ + if(is_array($this->_form_array['form_structure'])){ + foreach($this->_form_array['form_structure'] as $field){ $field['required'] = $field['required'] == 'checked' ? ' required' : false; diff --git a/Formbuilder/Formbuilder_pdo.php b/Formbuilder/Formbuilder_pdo.php index 74e7f26..c0cea89 100644 --- a/Formbuilder/Formbuilder_pdo.php +++ b/Formbuilder/Formbuilder_pdo.php @@ -57,7 +57,7 @@ public function connect($url = "mysql:host=127.0.0.1;dbname=formbuilder", $user * Save the data to the database, but still returns the $for_db array. */ public function save_form(){ - $for_db = parent::store(); + $for_db = parent::get_encoded_form_array(); if($for_db['form_id']){ $stmt = $this->_db->prepare("UPDATE fb_savedforms SET form_structure = :struct WHERE id = :id"); $stmt->bindParam(':id', $for_db['form_id'], PDO::PARAM_INT); diff --git a/README b/README index eace48a..40c1a26 100644 --- a/README +++ b/README @@ -23,6 +23,8 @@ Version 0.4 - 20111215 - Removing old XML-based form loading system, moving to pure JSON - Removing internal serialization system in favor of json formatting - Adding example mysql storage methods to the load, save process (PDO-based so other databases may be used) +- Incorporated a required attribute bug fix: https://snowy-evening.com/botsko/jquery-form-builder/8/ +- Dumped a ton of unnecessary complexity of encoding/decoding and array vars Version 0.3.1 - 20110722 - Corrected issue with reserved words breaking support for Safari, Opera, IE diff --git a/docs/DOCUMENTATION b/docs/DOCUMENTATION index 8d02e03..99d09d0 100644 --- a/docs/DOCUMENTATION +++ b/docs/DOCUMENTATION @@ -60,7 +60,7 @@ To save the POST request from the jquery object, just invoke the following: $form = new Formbuilder($form_data); // Return the array for saving to a database - $for_db = $form->store(); + $for_db = $form->get_encoded_form_array(); The $for_db variable is an array and will contain a hash value, and the serialized form value. Save these two values to your database. They both need diff --git a/example-html.php b/example-html.php index 828baf5..2ce7c14 100644 --- a/example-html.php +++ b/example-html.php @@ -13,7 +13,7 @@ // At this stage, we simulate getting an array of the // form_structure and hash from our database. This is // how the form data would have been saved using -// the $form->store() method. +// the $form->get_encoded_form_array() method. include('fake-form-db-vals.php'); $form = new Formbuilder($fake_db_vals); diff --git a/example-json.php b/example-json.php index 673cc28..4572696 100644 --- a/example-json.php +++ b/example-json.php @@ -5,7 +5,7 @@ // At this stage, we simulate getting an array of the // form_structure and hash from our database. This is // how the form data would have been saved using -// the $form->store() method. +// the $form->get_encoded_form_array() method. include('fake-form-db-vals.php'); //$form = new Formbuilder($fake_db_vals); diff --git a/example-save.php b/example-save.php index 7d57a19..556268d 100644 --- a/example-save.php +++ b/example-save.php @@ -7,14 +7,14 @@ // pass POST directly, but DO NOT use POST without // proper security in place. -// The store() method returns an array that should be +// The get_encoded_form_array() method returns an array that should be // used to store the values in the database. This array // is also what's passed to the class when rendering // or editing the form. -//$form_data = isset($_POST['frmb']) ? $_POST : false; -//$form = new Formbuilder($form_data); -//$for_db = $form->store(); +$form_data = isset($_POST['frmb']) ? $_POST : false; +$form = new Formbuilder($form_data); +$for_db = $form->get_encoded_form_array(); //------------------------------------------------------------------------------ @@ -32,11 +32,13 @@ //------------------------------------------------------------------------------ -// Here's the example output of store() +// Here's the example output of get_encoded_form_array() -//print_r($for_db); -//Array( -// [form_structure] => [{"cssClass":"input_text","required":"undefined","values":"First Name"},{"cssClass":"checkbox","required":"undefined","title":"Favorite programming language?","values":{"2":{"value":"PHP","baseline":"checked"},"3":{"value":"Ruby","baseline":"undefined"},"4":{"value":"Java","baseline":"undefined"}}}] +print_r($for_db); +//Array +//( +// [form_id] => 1 +// [form_structure] => [{"cssClass":"input_text","required":"undefined","values":"First Name"},{"cssClass":"input_text","required":"undefined","values":"Last Name"},{"cssClass":"textarea","required":"undefined","values":"Bio"},{"cssClass":"checkbox","required":"undefined","title":"What's on your pizza?","values":{"2":{"value":"Extra Cheese","baseline":"undefined"},"3":{"value":"Pepperoni","baseline":"undefined"},"4":{"value":"Beef","baseline":"undefined"}}}] //) ?> \ No newline at end of file diff --git a/example-submit.php b/example-submit.php index ac4d445..e7a9780 100644 --- a/example-submit.php +++ b/example-submit.php @@ -13,7 +13,7 @@ // At this stage, we simulate getting an array of the // form_structure and hash from our database. This is // how the form data would have been saved using -// the $form->store() method. +// the $form->get_encoded_form_array() method. include('fake-form-db-vals.php'); $form = new Formbuilder($fake_db_vals); diff --git a/fake-form-db-vals.php b/fake-form-db-vals.php index bc86d28..8b2d745 100644 --- a/fake-form-db-vals.php +++ b/fake-form-db-vals.php @@ -3,8 +3,6 @@ // This is an arry of fake form values that should be coming from your // database, or data storage system. This is simply defined here for // example purposes only. -$fake_db_vals = array( - 'form_structure'=>'[{"cssClass":"input_text","required":"undefined","values":"First Name"},{"cssClass":"checkbox","required":"undefined","title":"Favorite programming language?","values":{"2":{"value":"PHP","baseline":"checked"},"3":{"value":"Ruby","baseline":"undefined"},"4":{"value":"Java","baseline":"undefined"}}}]'); - +$fake_db_vals = Array( 'form_structure' => '[{"cssClass":"input_text","required":"undefined","values":"First Name"},{"cssClass":"input_text","required":"undefined","values":"Last Name"},{"cssClass":"textarea","required":"undefined","values":"Bio"},{"cssClass":"checkbox","required":"undefined","title":"What\'s on your pizza?","values":{"2":{"value":"Extra Cheese","baseline":"undefined"},"3":{"value":"Pepperoni","baseline":"undefined"},"4":{"value":"Beef","baseline":"undefined"}}}]'); ?> \ No newline at end of file