Saturn Tables provides an input or edit form structure based on hooks and arrays to easily construct custom input forms with validation, notices and processing. It is done in a waterfall passing successive variables to callbacks with return values similar to WordPress filters. Each callback takes in several values as parameters but only alters and returns one value. Saturn tables callbacks are more isolated the WordPress filters, they are based on WordPress administration menu structure which utilizes callbacks. Before going through the waterfall, there are also menu_title, header and footer definitions similar to Saturn Tables list tables.
The menu definition callback is here:
function my_cars_add_edit_car_func() {
$id = isset($_GET['id']) ? $_GET['id'] : 0;
$input_form_definitions['menu_title'] = $id > 0 ? '<h1 class="wp-heading-inline">Edit Car</h1> <strong>[ID:' . $id . ']</strong>' : '<h1>Add Car</h1>';
$input_form_definitions['header'] = $id > 0 ? '<p>Edit car below with this form.</p>' : '<p>Input new car with this form.</p>';
$input_form_definitions['form_definition'] = 'my_cars_input_form_definition';
$input_form_definitions['form_validation'] = 'my_cars_input_form_validation';
$input_form_definitions['run_query'] = 'my_cars_run_query';
$input_form_definitions['set_notices'] = 'my_cars_set_notices';
$input_form_definitions['process_post_vars'] = 'my_cars_process_post_vars';
$input_form_definitions['footer'] = '<p>Powered by <a href="https://saturntables.com/" target="_blank">Saturn Tables</a></p>';
return $input_form_definitions;
}
The menu_title, header and footer are outputted as HTML content and the top and bottom as expected. menu_title will override the default menu_title.
The waterfall is as follows. These variables are then set with initial values.
$validation = false;
$query_return = false;
$notices = false;
$post_vars = $_POST;
The form_definition callable is called returning $input_form.
function my_cars_input_form_definition() {
global $wpdb;
$countries = $wpdb->get_col( "SELECT DISTINCT country FROM saturn_tables_cars ORDER BY country" );
$input_form['action'] = array('type'=>"hidden", "value" => "postback" );
$input_form['make'] = array('label'=> "Make", "required_label" => "", 'type'=>"text", "required" => "" );
$input_form['model'] = array( 'label'=> "Model", "required_label" => "", 'type'=>"textarea", 'rows'=>"5", 'cols'=>"30", 'style'=>"max-width: 500px;", "required" => "");
$input_form['mpg'] = array ('label'=> "MPG", "required_label" => "", 'type' => "number", 'step'=>".1", "required" => "");
$input_form['cylinders'] = array ('label'=> "Cylinders", "required_label" => "", 'type' => "number", 'step'=>"1", "required" => "");
$input_form['weight'] = array ('label'=> "Weight", "required_label" => "", 'type' => "number", 'step'=>"1", "required" => "");
$input_form['model_year'] = array ('label'=> "Model Year", "required_label" => "", 'type' => "number", 'step'=>"1", "required" => "");
$input_form['country'] = array ('label'=> "Country", "required_label" => "", 'type' => "select", 'options' => $countries, "required" => "" , 'usekey' => false);
return $input_form;
}
Next the validation function is called returning validation. Not that the $input_form and $post_vars are set and passed in. The array values $validation[input_form_key][‘error’] and $validation[input_form_key][‘message’] as reserved for later use.
function my_cars_input_form_validation($input_form, $validation, $post_vars) {
return $validation;
}