I am trying something new in the way I develop. I am sudo-coding the code that will use the class before the class is even built. This form class allows you to see all your form elements as form elements in DW design view and I assume other editors would work too but havn't tried any others. I have a class already completed similar to this but its a little more messy and complicated to acheive the same goal as this new class would do. Let me know what you think.
<?php
# NOTEs:
# This class will assume all data passed into it is "clean"
// Name of the form and the method it will submit by
$form = new form_handle('myform',__POST);//('myform',__GET)
// Values for the dropdown list
$icecream_list = array('cho' => 'chocolate','van' => 'vanilla','straw' => 'strawberry');
$sports_list = array('1' => 'soccer','2' => 'baseball','3' => 'football','4' => 'basketball');
#===========================================================================================
# Validation
# NOTE: Adding a parameter to each validation of "JS" will create javascript code to validate
# client side before submitting in addition to the server side validation
# This will not work the the validation that queries the database
# --will think about a way to hook this in via ajax after most of everything else is done
#
// Must be at least X number of characters
$form->validate(__MIN_CHAR,array(
array('firstname','2','Lenght must be longer than 2 characters'),
array('element name','minimum lenght','error message')
));
// Must be not more then X number of characters
// Note: If you use "%var%" in the error message it will use the number sent,
// this prevents you from having to change the number in two place if you change
// the acceptable lenght at a later date
$form->validate(__MAX_CHAR,array(
array('firstname','15','Lenght must not be longer than %var% characters'),
array('element name','minimum lenght','error message')
));
// Requires a value to to inputted
$form->validate(__REQUIRED,array(
array('firstname','This value is required'),
array('firstname','This value is required',JS),//Validates using javascript ALSO
array('element name','error message')
);
// E-MAIL
$form->validate(__EMAIL,array(
array('emailaddy','This must be a valid email'),
array('element name','error message')
);
// Two values must match/be the same
$form->validate(__SAME,array(
array('password1','password2','Passwords did not match!'),
array('element name1','element name2','error message')
);
// Two values must not match/be the same
$form->validate(__OPPOSITE,array(
array('name1','name2','names can not match!'),
array('element name1','element name2','error message')
);
// Minimum value/amount numbers
$form->validate(__MIN_VALUE,array(
array('age',15,'Not old enough'),
array('element name','min amount','error message')
);
// Maximum value/amount numbers
$form->validate(__MAX_VALUE,array(
array('age',95,'Too old!!!'),
array('element name','max amount','error message')
);
// Custom regular expression
$form->validate(__REGEX,array(
array('somename','^[A-Z]$',__CASE_SENSITIVE,'Invalid value'),
array('somename','^[A-Z]$',__CASE_INSENSITIVE,'Invalid value'),
array('element name','regular expression',case sen/insen-itive,'error message')
);
//______________________________________________________________________________________
// Database match, only works if using my db class, or you can modify it to connect to whoevers
$form->validate(__DB_MATCH,array(
array('login',__BAD,"SELECT user_login FROM user WHERE login = '{$form->getValue['login']}' LIMIT 1",'Some1 already has that login name'),
array('state',__GOOD,"SELECT state_name FROM states WHERE state_name = '{$form->getValue['statename']}' LIMIT 1",'you choose a state that does not exsist'),
array('element name',__BAD=Match found fails validation, __GOOD=Match not found fails validation,'sql query','error message')
);
#===========================================================================================
#===========================================================================================
# Setting the value of elements
$theperson = $sql->fetchArray();
// The will automatically match up form elements to the database results assuming you named them the same
$form->addFormValues($theperson);
// If you have any more values you wish to add in just send them in as an single-dem array
// Say you want to redisplay the form with what was submitted
$form->addFormValues(cleanArray($_POST));
#===========================================================================================
#===========================================================================================
# Defaults:
# only appear if no other values for said element were added
# does not override any pre-exsisting values, LOWEST PRIORITY
# Examples will show how to do single and multiple values
//______________________________________________________________________________________
$form->addDefaultToList(// Single
array('icecream' => array('van'))
);
$form->addDefaultToList(// Multiple
array('icecream' => array('van')),
array('sports' => array('soccer','baseball'))
);
//______________________________________________________________________________________
$form->addDeaultToCheckBox(// Single
array('shirt')
);
$form->addDeaultToCheckBox(// Multiple
array(
'shirt',
'hat',
'pants',
'shoes')
);
//______________________________________________________________________________________
$form->addDeaultToTextArea(// Single
array('bio' => 'Enter some text here')
);
$form->addDeaultToTextArea(// Multiple
array('bio' => 'Enter some text here'),
array('moreinfo','blabalblab'
);
//______________________________________________________________________________________
$form->addDeaultToRadio(// Single
array('color' => 'red')
);
$form->addDeaultToRadio(// Multiple
array(
'color' => 'red',
'yesno' => 'yes')
);
//______________________________________________________________________________________
$form->addDeaultToText(// Single
array('firstname' => 'Tom')
);
$form->addDeaultToText(// Multiple
array
(
'firstname' => 'Tom',
'lastname' => 'Ginkens')
);
//______________________________________________________________________________________
#===========================================================================================
?>
<form action="" <?php echo $form->setForm();// method="POST" name="myform" id="myform" ?>>
<p>
<input type="text" <?php echo $form->setText('firstname');// name="firstname" id="firstname" value="Tom" ?>/>
<input type="text" <?php echo $form->setText('lastname');// name="firstname" id="firstname" value="Ginkens" ?>/>
<?php echo errorMsg('firstname'); //Displays error message if validation fails ?>
</p>
<p>
<input type="radio" <?php echo $form->setRadio('color','red'); //name="color" value="red" checked="checked" ?> />
<input type="radio" <?php echo $form->setRadio('color','blue'); //name="color" value="blue"?>/>
<input type="radio" <?php echo $form->setRadio('color','yellow'); //name="color" value="yellow"?>/>
</p>
<p>
<input type="radio" <?php echo $form->setRadio('yesno','yes'); //name="yesno" value="yes"?>/>
<input type="radio" <?php echo $form->setRadio('yesno','no'); //name="yesno" value="no"?>/>
</p>
<p>
<input type="checkbox" <?php echo $form->setCheckBox('hat','on'); ?> />
<input type="checkbox" <?php echo $form->setCheckBox('shirt','1'); ?> />
<input type="checkbox" <?php echo $form->setCheckBox('pants','yup');
// name="hat" id="hat" value="on"
// name="shirt" id="shirt" value="1" checked="checked"
// name="pants" id="pants" value="yup"
?>/>
<input type="checkbox" />
</p>
<p>
<select name="icecream" id="icecream">
<?php echo $form->setList('icecream',$icecream_list);
// <option value="cho">chocolate</option>
// <option value="van" selected="selected">vanilla</option>
// <option value="straw">strawberry</option>
?>
</select>
</p>
<p>
<select name="sports[]" id="sports" multiple="multiple">
<?php echo $form->setList('sports',$sports_list);
// <option value="1" selected="selected">soccer</option>
// <option value="2" selected="selected">baseball</option>
// <option value="3">football</option>
// <option value="4">basketball</option>
?>
</select>
</p>
<p>
<textarea name="bio"><?php echo $form->setTextArea('bio'); ?></textarea>
</p>
<p>
<input type="submit" <?php echo $form->setSubmit('Send','Press Me!!','Sending...',true);
// The last two parameters are optional
// The last parameter is to disable the button after being pressed or not, true is disable
//name="Send" id="Send" BEFORE CLICK -> value="Press Me!!"
//name="Send" id="Send" AFTER CLICK -> value="Sending..." disabled="disabled"
?> />
</p>
</form>
I cut out some html and comments so I could get the character count under limit.