/** * The default controller * @return void */ public function index() { /** * Load the helper file */ TINA_MVC\include_helper('form'); /** * Create the new form */ $form = new TINA_MVC\form('my_form'); /** * Open a fieldset */ $fset_open = $form->fieldset_open('fset_name_pw', 'Name and password please'); /** * Adds a text field */ $f_name = $form->add_field('your_name', $type = 'TEXT'); $f_name->add_validation(array('required' => NULL)); /** * Adds a password field */ $f_password = $form->add_field('the_password', $type = 'PASSWORD'); /** * Close a fieldset * * This is optional. If you do not close a fieldset, it is closed whenever you open a new one or * when you render the form */ $fset_close = $form->fieldset_close(); /** * add_field() and all field methods return a field object so you can use method chaining */ $f_hidden = $form->add_field('a_hidden_field', 'hidden')->set_value('Hidden field value'); /** * Some sample data for the select type */ $options = array(array('1', 'dog'), array('21', 'cat'), array('1234', 'mouse'), array('3', 'cake'), array('4', 'house'), array('5', 'meh')); /** * Adds the select field */ $f_select = $form->add_field('select_field', 'SELECT')->set_options($options); /** * Adds the radio field using the same options as for the select field above */ $f_radio = $form->add_field('radio_field', 'RADIO')->set_options($options); /** * A checkbox field */ $f_check = $form->add_field('check_field', $type = 'CHECKBOX', $caption = 'This is the checkbox caption'); /** * A textarea field */ $f_textarea = $form->add_field('textarea_field', $type = 'TEXTAREA', $caption = 'This is the textarea caption'); $f_textblock = $form->add_text('textblock_field', 'This is a block of text included in the form using the "textblock" field type.'); /** * A Googlemap location field * * You must enter your API key into app_settings.php */ if (TINA_MVC\get_tina_mvc_setting('google_api_key_v3')) { $f_map = $form->add_field('map_field', 'GOOGLEMAP'); /** * Sets the height and width of the page in CSS units */ $f_map->set_map_height(200); $f_map->set_map_height(400); } else { $f_gmap_note = $form->add_text('gmap_note', 'You must enter your Google API key (v3) into app_settings.php to use the Google Map location field type.'); } /** * A reCaptcha field * * The recaptcha field type requires your API keys to be set in app_settings.php * * @see http://www.reCaptcha.net */ if (TINA_MVC\get_tina_mvc_setting('recaptcha_pub_key') and TINA_MVC\get_tina_mvc_setting('recaptcha_pri_key')) { $f_recaptcha = $form->add_field('recaptcha_field', $type = 'RECAPTCHA', $caption = 'Are you human?'); } else { $f_recaptcha_note = $form->add_text('recaptcha_note', 'You must enter your reCaptcha API keys into app_settings.php to use the reCaptcha field type.'); } /** * A file upload field */ $f_file_upload = $form->add_field('a_file_upload', 'file'); /** * A second file upload field */ $f_second_file_upload = $form->add_field('a_second_file_upload', 'file'); /** * A submit button */ $f_submit = $form->add_field('submit_field', $type = 'SUBMIT', $caption = 'This is the submit caption'); /** * A reset button */ $f_reset = $form->add_field('reset_field', $type = 'RESET', $caption = 'This is the reset caption'); /** * Some data to pre-load into the form. * * This could come from a database query for example. */ $data = array('name' => 'Joe Bloggs', 'textarea_field' => "Some multiline\r\ntext", 'radio_field' => 3); /** * Loads the data into the form, overriding any default values previously set * * You would use this function to load a recordset from (for example a database query) into * the form. First, any keys in the $data array will match field names. After this any 'db_field' * values matching keys in $data have their values set too. */ $form->load_data($data); /** * Returns an array of posted data, FALSE if the form has not been posted. */ if ($posted_data = $form->get_posted_data()) { $content = "Posted Data<br />"; $content .= "<pre>" . print_r($posted_data, TRUE) . "</pre>"; /** * Generally you process your form here and finish off with a wp_redirect() and exit() */ // wp_redirect( 'wherever' ); // exit(); } else { /** * Returns the HTML required to disply the form */ $content = $form->render(); } /** * Assign as template data */ $this->add_var('view_html', $content); /** * Set the post (page) title and contents. */ $this->set_post_title('Tina MVC New Form Helper Example'); $this->set_post_content($this->load_view('test_form')); }
/** * Edit the users personal data * * @todo do we need to implement the Wordpress 'password_reset' action hook */ public function edit() { $current_user = $this->get_current_user(); TINA_MVC\include_helper('form'); $f = new TINA_MVC\form('edit_data_form'); // $f_user_login = $f->add_field( 'user_login', 'text' ); $f_user_email = $f->add_field('user_email', 'text')->set_default_value($current_user->user_email); $f_first_name = $f->add_field('first_name', 'text')->set_default_value($current_user->user_firstname); $f_last_name = $f->add_field('last_name', 'text')->set_default_value($current_user->user_lastname); $f_display_name = $f->add_field('display_name', 'text')->set_default_value($current_user->display_name); $f_current_password = $f->add_field('current_password', 'password')->add_validation(array('required' => NULL)); $f_note = $f->add_text('f_note', "Leave 'New Password' blank to leave your password unchanged."); $f_new_password_1 = $f->add_field('new_password', 'password'); $f_new_password_2 = $f->add_field('new_password_again', 'password'); $f_submit = $f->add_field('submit', 'submit'); $f_cancel = $f->add_field('cancel', 'submit'); if ($f_cancel->get_posted_value()) { wp_redirect(TINA_MVC\get_controller_url('my-profile')); exit; } if ($data = $f->get_posted_data()) { $user_data = array(); $error = 0; // check the password for edits... if (is_wp_error(wp_authenticate($current_user->user_login, $data['current_password']))) { $f_current_password->add_validation_error('Incorrect password.'); $error++; } if ($data['user_email']) { if (!is_email($data['user_email'])) { $f_user_email->add_validation_error("'" . $f_user_email->get_caption . "' is not a valid email address."); $error++; } // make sure it isn't attached to another user... if ($test_user = get_user_by('email', $data['user_email'])) { if ($test_user->ID != $current_user->ID) { $f_user_email->add_validation_error('This email address belongs to another user.'); $error++; } } if (!$error) { $user_data['user_email'] = $data['user_email']; } } if ($data['first_name']) { $user_data['first_name'] = $data['first_name']; } if ($data['last_name']) { $user_data['last_name'] = $data['last_name']; } if ($data['display_name']) { $user_data['display_name'] = $data['display_name']; } if ($data['new_password'] or $data['new_password_again']) { if ($data['new_password'] != $data['new_password_again']) { $f_new_password_1->add_validation_error('New passwords must match.'); $error++; } else { $user_data['user_pass'] = $data['new_password']; } } if ($user_data and !$error) { $user_data['ID'] = $current_user->ID; wp_update_user($user_data); wp_redirect(TINA_MVC\get_controller_url('my-profile/index/saved')); exit; } } $this->add_var('edit_data_form', $f->render()); $this->set_post_title('Editing Profile for ' . $current_user->user_login); $this->set_post_content($this->load_view('my_profile')); }
/** * The default controller * @return void */ public function index() { /** * Load the helper file */ TINA_MVC\include_helper('form'); /** * Create the new form */ $form = new TINA_MVC\form('my_form'); /** * Adds a text field */ $text_field_1 = $form->add_field('text_field_1', $type = 'TEXT'); /** * It is possible to retrieve the $_POST value for a field without checking if the form has been posted * using the get_posted_value() method. */ $text_message = $form->add_text('posted_message', 'The $_POST value for \'Text Field 1\' is: "' . $text_field_1->get_posted_value() . '"'); /** * Adds a validation rule * * All validation rules are of the form array( 'rule_name' => parameters ). Check the form helper file * for classes called validate_* for rules, parameters and for how to add your own validation rules. */ $text_field_1->add_validation(array('required' => NULL, 'min_length' => 3, 'max_length' => 7)); /** * The same using method chaining... */ $text_field_2 = $form->add_field('text_field_2', $type = 'TEXT')->add_validation(array('required' => NULL)); /** * Adding a field showing all available parameters: * * $name - the field name. Used as a key in the array of posted data returned by $form->get_posted_data(). * $type - see the field_* classes in the form helper file for a complete list of fields (or see * the samples in 08_form_helper_intro) * $caption - The label. Default label is based on the $name parameter. For example 'first_name' => 'First Name' * $db_table - used to group fields. For example, posted fields grouped according to $db_table = 'my_table' can * be retrieved using $form->get_posted_db_data('my_table') (gets data with keys based on the $db_field value) * or $form->get_posted_data('my_table') (gets data with keys based on the field $name value) * $db_field - the database field name (if different from the $name value). Use with $form->get_posted_db_data() * $default_value * $extra_attribs - a string (for example 'attribute="value"') or array of strings */ $text_field_3 = $form->add_field('text_field_3', 'text', 'Please enter a value for this field', 'my_table', 'my_field', 'Default value', 'style="color: white; background: black;"'); /** * Similar to above, but using method chaining and passing several extra html attributes to the input */ $text_field_4 = $form->add_field('field_4', $type = 'TEXT')->set_caption('Field Four')->set_db_table('my_table')->set_db_field('field_four')->set_default_value('Another default')->set_extra_attribs('style="background: #ccc;"')->set_extra_attribs('title="Like a tooltip, touched for the very first time..."'); /** * Extra attributes can be passed as an array too */ $text_field_5 = $form->add_field('field_5', $type = 'TEXT')->set_db_table('my_table')->set_extra_attribs(array('style="font-style: italic;"', 'title="Another title attribute"')); /** * A submit button */ $f_submit = $form->add_field('submit_field', $type = 'SUBMIT', $caption = 'This is the submit caption'); /** * A reset button */ $f_reset = $form->add_field('reset_field', $type = 'RESET', $caption = 'This is the resetcaption'); /** * Returns an array of posted data, FALSE if the form has not been posted. */ if ($posted_data_1 = $form->get_posted_data()) { /** * $posted_data_1 is an array of posted values. Keys are the field names. Values are taken from fields with a blank 'db_table' value */ $content = 'Posted Data: retrieved with $form->get_posted_data()<br />'; $content .= "<pre>" . print_r($posted_data_1, TRUE) . "</pre><br />"; /** * Returns an array of posted values. Keys are the field names. Values are taken from fields where 'db_table' = 'my_table' */ $posted_data_2 = $form->get_posted_data('my_table'); $content .= "Posted Data: retrieved with \$form->get_posted_data('my_table')<br />"; $content .= "<pre>" . print_r($posted_data_2, TRUE) . "</pre><br />"; /** * Returns an array of posted values. Keys are taken from 'db_field' value. Values are taken from fields with a blank 'db_table' value */ $posted_data_3 = $form->get_posted_db_data(); $content .= "Posted Data: retrieved with \$form->get_posted_db_data()<br />"; $content .= "<pre>" . print_r($posted_data_3, TRUE) . "</pre><br />"; /** * Returns an array of posted values. Keys are taken from 'db_field' value. Values where fields 'db_table' = 'my_table' */ $posted_data_4 = $form->get_posted_db_data('my_table'); $content .= "Posted Data: retrieved with \$form->get_posted_db_data('my_table')<br />"; $content .= "<pre>" . print_r($posted_data_4, TRUE) . "</pre><br />"; /** * Generally you process your form here and finish off with a wp_redirect() and exit() */ // wp_redirect( 'wherever' ); // exit(); } else { /** * Returns the HTML required to disply the form */ $content = $form->render(); } /** * Assign as template data */ $this->add_var('view_html', $content); /** * Set the post (page) title and contents. */ $this->set_post_title('Tina MVC New Form Helper Example'); $this->set_post_content($this->load_view('test_form_2')); }