[eluser]obiron2[/eluser]
OK guys,
i have just about got to grips with the validation thing; setting rules and applying them to fields. I even got as far as callbacks on multiple fields (with some help from the board).
Now I want to create 'generic; form handling for my semi-static data.
The plan:
build a helper that renders the whole of the form - this extends form_open(), form_input() etc by building the whole of the form from an object who's data structure is like
Code:
$form->header->class
$form->header->formid
$form->header->method
...
$form->fields->field1->id
$form->fields->field1->name
$form->fields->field1->type
$form->fields->field1->size
$form->fields->field1->value
$form->fields->field1->label
$form->fields->field1->maxlength
$form->fields->field2->id
$form->fields->field2->name
...
$form->submit->value
I havn't decided yet whether fields should be an array of objects or an array of arrays.
I can get all of the fields from a table using
Code:
select full columns from $table
and returning the results set
you will see why 'full columns' in a bit.
I can then foreach through the results set and add the field details to the $form->fields array as necessary.
included in the render_form($form) function is a hidden field with a unique formID built from the $form->name and the timestamp. This helps prevent double posting.
also included is the if(isset($_POST[fieldname])) rules for redisplaying values on validation failure.
I have all of this working incidentally..
Several things occur to me here.
1) auto-increment fields will be included. Normally these would be internal record pointers and not meant for public consumption.
2) Valid data may rely on data in another table. A select drop down list will be most approriate in the form but I would need to populate the data
3) What labels do I give the form fields - field names may not be appropriate (think multi-lingual)
4) There is no way to build in validation rules (e.g. phone number, postcode) automatically
I am using XAMP on the dev and LAMP on live with php5 (full OOP protocols) and MySQL with phpMyAdmin for manual db maintenance.
By using Select FULL columns from $table you have access to MySQL's comments functionality of the columns structure. I am storing in there data relating to actions to be taken on the field when the table is displayed in a form.
e.g.
'donotshow' = don't create/add to the form->fields object
'textarea[10,2]' = use input type of textarea with 10 rows and 2 cols instead of the text type
'validate[minlength[3],maxlength[5],isnumeric]' = add the rules to the field validation.
'norepost' = if the form is re-displayed after failing validation, do not re-display the value (e.g. captcha)
This method will be limited to databases that support the comments values in columns, I don't know what SQlite, mSQL and MsSQL do.
This method is very clunky for multi-lingual labels and help text.
Would I do better to have another table that holds exceptional formatting and validation rules for table display and another one for labels and help text in different languages.