Jump to content

Quicker Form building


chronister

Recommended Posts

I dig coding, but the one thing I don't like is creating forms and the validation for them. I have spent WAY TOO MUCH time just messing with forms and getting them to work and validate properly.

 

Is there a form class that you folks here use, or some method of creating forms quicker??

 

I have looked at CloneFish form class, and it looks alright. Is there something else that you guys and gals reccommend??

 

Nate

Link to comment
Share on other sites

everybody is gonna hate me for just giving you code, but you have a thousand posts, you could do this if you really wanted to, it doenst help with validation, but man does it make forms a whole lot easier

 

formhelpers.php

<?php
//print a text box
function input_text($element_name, $values){
print '<input type="text" name="' . $element_name .'" value="';
print htmlentities($values[$element_name]) . '"/>';
}

//print a title box, like text box but as wide as a text area
function input_title($element_name, $values){
print '<input type="text" size="76" name="' . $element_name .'" value="';
print htmlentities($values[$element_name]) . '"/>';
}

//print a password box
function input_password($field_name, $values) {
print '<input type="password" name="' . $field_name .'" value="';
print htmlentities($values[$field_name]) . '"/>';
}

//print a submit button
function input_submit($element_name, $label){
print '<input type="submit" name="' . $element_name .'" value="';
print htmlentities($label) . '"/>';
}

//print a textarea
function input_textarea($element_name, $values){
print '<textarea cols="75" rows="40" name="' . $element_name .'">';
print htmlentities($values[$element_name]) . '</textarea>';
}

//print a radio button or checkbox
function input_radiocheck($type, $element_name, $values, $element_value){
print '<input type="' . $type . '" name="' . $element_name . '" value="' . $element_value . '" ';
if ($element_value == $values[$element_name]){
	print ' checked="checked"';
}
print '/>';
}

//print a select menu

function input_select($element_name, $selected, $options, $multiple = false){
//print out the <select> tag
print '<select name="' . $element_name;
// if multiple choices are permitted, add the multiple attribute
// and add a [] to the end of the tag name
if ($multiple){ print '[]" multiple="multiple'; }
print '">';

//set up the list of things to be selected
$selected_options = array();
if ($multiple) {
	foreach ($selected[$element_name] as $val){
		$selected_options[$val] = true;
	}
} else {
	$selected_options[ $selected[$element_name] ] = true;
}

//print out the <option> tags
foreach ($options as $option=>$label) {
	print '<option value="' . htmlentities($option) . '"';
	if ($selected_options[$option]){
		print ' selected="selected"';
		}
	print '>' . htmlentities($label) . '</option>';
}
print '</select>';
}


?>

Link to comment
Share on other sites

Lodius2000  - Thanks for that code. I have thought of something like that but hesitated as that part of it is not too terribly bad. The creation of the actual form is not a terribly tedious part, it is the processing of it and handling the errors properly and re-filling the form that is a bitch.

 

I have a HUGE employment application form in the forseeable future for the company I work for. This is going to be a pain in the ass because it will be IN DEPTH validation... things like if field a is yes, then fields b, c, d have to be filled out if field a is no, skip fields b, c, d ...... etc..

 

But I may use something like the functions you gave to save some coding and time.

 

cooldude832 - I have thought about a generated form that uses a database to store the fields and such.... But again the validation is not accounted for... I am not real good with the whole OOP thing yet, working on it, but got a ways to go.

 

I may just break down soon and make my own form class. It will be a good OOP practice.

 

Anyone familiar with CloneFish?? It looks pretty good minus the price.

 

Thanks for the input here folks....

 

Nate

Link to comment
Share on other sites

Think of it this way

 

MySQL tables

 

1) Forms (PK = FormID, list the name of form last edit etc.)

2) Fields (PK FieldID, FK FormID list the fields, labels, type, default value etc.)

3) Validate (PK ValidateID, FK FieldID, FormID Contains validation snippette for forms)

 

so example is you build a system that lets you do madlib fill in with like 6 selects

 

if (This Field affected by These parameters ) IS  (> < == === !=)  (This Value/Field)  (Halt/Display/Halt Display)  (This Error Message)

 

So you can take a field and do a regex to it/strip tags/strlen  etc and verify it this way.   

 

Or you could even add on to the cookie cutter a way to add a completely unique error checking that runs it against a table for example to verify its not a duplicate name. 

 

Once you have the class/functions set up For error checking its super easy as you error check you just keep in mind that idea I present of Halt//Warn/Warn and Halt  and build a function called error_report in the class that looks like

function error_report($error,$method){
switch $method{
case 0:
#Halt only
break;
case 1:
#Warn Only
break;
default:
#Halt and warn
}
?>

 

very easy

 

 

Link to comment
Share on other sites

chronister,

 

look into that book i talked about earlier 'learning php 5' his method is really nice, makes functions for form processing... simple validation lines and all sorts of goodies.

 

heres the main page logic for all my form pages, just put this at the top and then build all the functions

 

<?php
if($_POST['_submit_check']){ //$_POST['_submit_check'] is a hidden field, makes redisplay possible
if($form_errors = validate_form()){
	show_form($form_errors);
} else {
	process_form();
}
} else {
show_form();
}

function show_form($errors = '') {

// make the form here
//start with
if ($errors){
print 'Please correct these errors: <ul><li>';
print implode('</li><li>', $errors);
print '</li></ul>';
}
}

function validate_form(){
//contains simple validation like
//check that username is entered
if (trim(strlen($username)) == 0) {
$errors[]= "You must enter a username.";
}
}

function process_form(){
//what do you want to do with the entered data, whatever it is, do it here
}
?>

 

 

that make sense?

Link to comment
Share on other sites

I feel your pain - I hate validating inputs. I actually created a few different classes to try to find something to make it easier, and while it did become easier, I still didn't come up with anything that was perfect.

 

However, a few months ago I started learning Drupal. Huge learning curve, but once you've got it, building forms is as easy as pie. You just declare an array to build the forms, and drupal has automatic validation built into it, with the ability to add extra validation not built into the core.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.