HTML Forms Generation and Validation PHP Class 1.0 BETA
Tuesday, December 19, 2006HTML Forms Generation and Validation PHP Class, News
This is a PHP class for easy HTML forms generation and validation. If you're a web developer you know that designing HTML forms and writing the PHP scripts for their validation is a really dull task. And if you're like me you must've looked around the web to see what you can find in order to ease this task.
December 27, 2006 6:21 PM
Great class! Thanks for your hard work.
But I have just one question. Is it possible to specify my own stylesheet. I mean my own font color, font size etc...as according to the theme of my site. Unless I am missing something here, I cant seems to find documented way to do that.
Thanks!
December 27, 2006 11:25 PM
Sure you can! I made the class with templating in mind. If you look in the templates/default folder you'll find the stylesheet you're looking for change it!
December 28, 2006 12:40 PM
Thanks!
Another thing,.Probably you know this already. I tried your example in IE7 and the date picker popup in another tab as full window instead of small popup window. I tried the same example in Opera and it's ok in Opera but Firefox is not showing any popup calendar at all when click.
Here are the version I am using (if it help at all):
- IE 7
- Opera 9.02
- Firefox 2.0.0.1
January 01, 2007 8:21 AM
Hi! I tried loading your class to my local http server with a dir setup root/phpforms/.. and /root/datepicker. i modified the datepicker line to $this->datePickerPath="../datepicker"; in class.htmlform.php. when i opened it i get a blank screen. any ideas what can be wrong?
January 01, 2007 12:36 PM
You are right, there was a bug in the class. Please download the class again to get the fixed version.
January 01, 2007 9:23 PM
Thanks. Ok, now i downloaded the fixed version. i was able to run the example.php but when u click on the datepicker - it opens up but the parent window closes. Now, with class.htmlform.php - it still is the same blank screen, IE7 or Firefox. $this->datePickerPath="../datepicker";
January 01, 2007 9:35 PM
btw, the htmlform1.0beta zip pkg has an empty index.html in its root folder. even if this is renamed or disabling the index.html, calling class.htmlform.php still renders a blank screen.
January 02, 2007 2:38 AM
Looking at example.php, I think the concept is to include "class.htmlform.php" in all our html forms i.e. myform.php, mynextform.php, etc. All control layouts will be designed via the forms' template (xtpl) files i.e. myform.xtpl, mynextform.xtpl, etc. If this is true, then I was incorrect in assuming that opening class.htmlform.php by itself would produce a wysiwyg form to throw in controls for layout. So, the only issue or bug remains is in example.php where when clicking on the datepicker, the parent form closes or disappears. Interesting class and would like to test and validate it further. Thanks!
January 02, 2007 9:09 PM
yes, you were supposed to run example.php :) and yes, all the other suppositions are correct. as for the date picker closing the parent form: i'll take a look and let you know
January 02, 2007 9:15 PM
ok, had a look and it seems to work ok here. what browser are you using? are you on windows? what server are you using?
a lot of people have problems with the date picker but i just can't reproduce them. any help would be appreciated!
January 03, 2007 8:10 PM
Thanks for the feedback. Server: Apache 2.0.55 on LAMP/ubuntu. Client1: WinXP-SP2 IE7.0 - here, when u click on the datepicker icon, the datepicker window opens but the underlying parent window closes and throws us to the subdir listing while the datepicker remains open.
Client2: Firefox2.0 - here the page opens but the datepicker icon is not visible - if u hover over where the datepicker icon should be, the link that appears is "http://127.0.0.1/phpforms/examples/example.php"! which i think is incorrect. It appears that there is some issue with regards to the url construction.
DatePicker Url icon points to:
WinXP IE7.0 = "http://server/phpforms/examples/"
Firefox2.0 = "http://server/examples/example.php"
note: on Firefox2.0, datepicker icon is not visible but the link is there.
January 03, 2007 8:15 PM
btw, we open the Firefox browser on the LAMP server. IE7 is on a different machine running on WinXP.
January 03, 2007 10:44 PM
typo error above, should be:
DatePicker Url icon points to:
WinXP IE7.0 = "http://server/phpforms/examples/"
Firefox2.0 = "http://server/phpforms/examples/example.php"
January 03, 2007 10:55 PM
thanks! i found and fixed all the errors. you will have the new version available tomorow as right now i am unable to upload the files.
January 04, 2007 1:23 PM
ok, download the package again to get the fixed version
January 04, 2007 9:25 PM
Finally got it to work:) thanks for the quick turnaround. Form runs ok in IE7 & Firefox with the exception of this --div class="HTMLForm-calendarIcon"--/div .... is not being recognized in Firefox. Overall - looking good.
documentation error in:
Method: setRule
email
1. $obj->setRule("mandatory"=>array("Not a valid email address!", "errorBlock1"));
>> "mandatory" should be "email"?
January 04, 2007 11:41 PM
i got Firefox 2 and the div looks ok. I'll find a Firefox 1.x tomorow and have a look
thanks for noticing the bug in the documentation!
January 04, 2007 11:43 PM
on a second thought: i think i just forgot to put the fixed stylesheet file in the package...
you'll have it first thing in the morning - that is, morning in romania :) that's about 9 hours away :)
January 05, 2007 12:28 PM
fixed.download the package again
January 05, 2007 7:42 PM
Cool! It's all working now. Firefox shows the icon and the class works fine on both browsers (IE7.0 & Firefox2.x). Thanks!!
January 17, 2007 11:31 AM
Its functionality is great but I think it has some bug in it. I have tried adding almost all the controls on the form and it was successful but when I tried to add radio button or checkbox, they are not getting added onto the form. If I am directly printing the output of toHTML(), radio button and checkbox are getting rendered but when I am adding them to form, they are not visible on the form.
January 17, 2007 11:45 AM
Hello Sridhar,
Thanks for reporting. I will check and get back to you later on today
January 19, 2007 9:35 AM
Ok, I figured it out: As the manual states:
"Note that the name attribute of the control will have the value of $controlID, while the id attribute will be str_replace(" ", "_", $controlID . "_" . $value). So, if $controlID is "checkbox" and $value is "value 1", the control's actual ID will be "checkbox_value_1"
Which simply means that if you add a radio (or a checkbox) control to your form and you do it like:
$frm->add ("radio", "id", "value");
in the template file you'd have to refer to it like
{controls.id_value}
and not
{controls.id}!
January 20, 2007 11:00 AM
Thanks Stefan for your swift reply. I have noticed one more point. I am not able to set the rule mandatory for input type file.
January 20, 2007 11:21 AM
In the example file there are mandatory rules set to all of the form's elements and are all working. you must be doing something wrong. also check in template file how errors are handled.
January 22, 2007 7:31 AM
Hi Stefan. I am adding a input type file control to the form and setting its rule as mandatory but its not showing any error even if I am not selecting any file.
I have written the code like this
In PHP file
$obj=& $form->add("file", "uploadfile");
$obj->setRule(array("mandatory"=>array("e6", "Please select a file")));
and in template file:
{errors.e6}
but its not showing error. Please help me.
January 22, 2007 9:26 AM
in the template file you should have
<!-- BEGIN: e6 -->
{errors.e6}
<!-- END: e6 -->
and not just
{errors.e6}
January 25, 2007 6:27 AM
Hi!
How to set a predefined value to a select or checkbox/radio control?
January 25, 2007 9:49 AM
You got two options.
The first one is to assign the checked attribute to the checkbox control upon creation:
$frm->add("checkbox", "sex", "male", array("checked"=>"checked"));
The second one is to assign the checked attribute by using the setAttributes() method of the checkbox object:
$obj = & $frm->add("checkbox", "sex", "male");
$obj->setAttributes(array("checked"=>"checked"));
January 25, 2007 2:22 PM
But for a SELECT control, how to set a specific value?
I tried something like that
$obj = & $frm->add("select", "country", "USA");
// and then add options with the list of coountries
January 25, 2007 2:35 PM
that is correct. only that you have to have an option with the value "USA" in order for it to be selected. and also remember that the addOptions() method takes an associative array as parameter (see the manual)! something like:
$obj = & $frm->add("select", "country", "USA");
$obj->addOptions(array("USA"=>"United States", "RO"=>"Romania", "GB"=>"Great Britain"));
January 29, 2007 6:57 AM
I have 2 control suggestions: 1) a link control - to spit out a content link on the template and 2) content control - to spit out just text/content on the template. This would give it some dynamic content flexibility (e.g. content can be sourced from db or rss feed skipping validation for fast rendering). This idea came upon designing the login page layout template i.e. there was more than just input controls to render.
January 29, 2007 9:34 AM
yes, you are right. I realized that, too. I don't really understand about the link control, though - isn't that the same as the content control?
January 30, 2007 11:41 AM
Hi Stefan. This is Sridhar again. Can we set any custom regular expression for checking of value in your class as we do for mandatory,numeric,email etc.
January 30, 2007 11:57 AM
sure. you must use the "custom" rule - see the manual about that. if you don't find your way, tell me what you need done and i'll try and do it for you
January 30, 2007 8:44 PM
hello stefan, i am carlos from VENEZUELA. im triying to specify the action in the form to post the data but i cant find where do it. i want to submit the data in the form thougth another file that process it and store in a database. thanks
January 30, 2007 9:29 PM
hi carlos! i'm sorry but that's currently not possible without tweaking the class a little. but if you can wait a few days, BETA 3 is coming out soon fixing some things and adding some more functionalities and this one is amongst them
January 31, 2007 9:00 PM
hi stefan, i did it. i change the default template.xtpl and the class.htmlform.php file adding a property named formAction and voila. anyway, i am waiting the new version of your class. thankyou. gretings from VENEZUELA
February 01, 2007 8:24 AM
i try to use PHPDataGrid 1.0 beta.
and i found i cannot check about check box in html page when phpdatagrid was auto generate check box.(as example4.php)
So, i would u to tell me how to check what item that was checked by user.
Thank you for advanced. ;-)
February 01, 2007 11:36 AM
Hi Stefan.Thanks a lot for all your prompt replies. Right now, in the form generation class, validate() is called to check for the rules we set for controls and if everything is correct, a message comes as "Form is valid. Redirect or whatever...". Now how can I get all the control values once the form is properly validated and how can I pass these values to another php file?
February 01, 2007 12:28 PM
I guess there is a bug with select control. If I set the attribute size=something(more than 1) and don't set the attribute multiple=multiple and if I select a particular option, I am not able to deselect it with ctrl+mouseclick on that option. I mean setting the size without multiple option is vague but somebody may do that.Put the following in your example.php file and see the result.
$obj = & $form->add("select", "option[]", "", array("size"=>3, "style"=>"height:60px"));
$obj->addOptions(array("I like this PHP class", "I don't like this PHP class", "What's PHP?"));
February 01, 2007 12:34 PM
for your first problem: i don't know. why would you pass your form's data to another file? do whatever you want to do with the data in the current file and then redirect everything to a confirmation page with header("Location: confirmation_page.html");
February 01, 2007 12:37 PM
your second problem: it has nothing to do with my class. it's just how select controls work. (see the w3c specifications at http://www.w3.org/TR/1999/REC-html401-19991224/). if you don't specify the "multiple" attribute, you are not allowed to select more than one entry...
February 03, 2007 11:49 AM
Hi Stefan. I have some problems with checkbox control. If I am setting the attribute of a particular checkbox as checked either by add() or by setAttributes(), it is coming as checked st the time of page load. After the page load, if I am unchecking that checkbox and again hitting submit, the form is rendered with the checkbox as checked and its not doing the check/uncheck operation by taking submittted values. I hope I am able to mae you understand my problem. For example, if $form->add("checkbox","interest","movies",array("checked"=>"checked")); is there in the form then this control is coming as checked when rendered. Now when I am unchecking this option and submitting the form, its again coming as checked.
February 03, 2007 1:08 PM
yes, you might be right on that one. let me check and get back to you at 22 (GMT +2). and thanks for all your feedback, i really really REALLY appreciate it
February 03, 2007 11:08 PM
yes, you were right. i fixed the bug and mentioned your name in the changelog file ;). i've also fixed another issue, one that made upload of file impossible unless the user would tweake the template file and added both enctype and MAX_FILE_SIZE. i am unable to upload the new version of the class until monday as i don't have ftp access from where i am now. you will be able to download the new version on moday around 10 AM (GMT+2)
January 29, 2007 5:56 PM
If you implement the link feature in a "content control" per se, it might get complex. For instance, if dynamic content is read from db - the dbtext field would have to have add'l intelligence to embed the title & link in the field. Then we have to tell the content control via setAttributes, i guess. It's easier for the db to store the link & title, in separate fields so when rendering we just implement your "link control". This leaves the "content control" to just plain text or html if you would. A good example of a "link control" is placing a "Forgot my password" link in a login.xtpl. Imagine implementing forms & dynamic content with this class (eg. search, news or a blog page like this - that's slick!). I hope that makes sense.
January 30, 2007 7:15 AM
cont'd. - I was just reading a section on your Printer Friendly class gen mentioning "transforming links to a readable format". That said, you already have implemented such logic for handling embedded links, so I guess, a "content control" with a rich setAttrib or functionality should do the trick. As long as its well documented. Thanks for considering this feature.
January 30, 2007 8:48 PM
hello stefan, im trying to specify the action of the form, but i cant find where do it. i want to submit the data in the form thought another file that process and stores in a database. how can i do that? thanks
February 07, 2007 8:35 AM
Hi Stefan. Did you upload the final class file on your site after fixing the bug on checkbox control which I mentioned. Moreover I am getting errors with select controls especially when setting something as selected while creating the form . The following notice also came sometime but I didn't exactly locate for which error it came. Notice: Undefined offset: 1 in /data/smartchange/corp_lab/corp_lab/web/htdocs/allcorp/smarty/callform2.php on line 166
February 07, 2007 9:34 AM
yes i did.
your error is, exactly as the notice says: in callform2.php at line 166. that's not one of my files :)
the error is not really an error. it's a notice. but you see it because when including my class you get to see ALL PHP's errors and notices. if you want to suppress these messages, comment the line error_reporting(E_ALL) on line 19 of the file class.htmlform.php
February 12, 2007 1:11 AM
Hi, great class
I have one question
How do i set the mandatory rule for a textbox only if a particular option in a optionbox is selected???
I can't find this in the documentation
February 12, 2007 8:09 AM
here's how you can do it: you don't set the mandatory rule to the textarea and after form submission you check the state of your checkbox and textarea and if the checkbox is checked and the textarea is empty you call the addError() method of the form object.
February 12, 2007 3:21 PM
hmm, I don't get it, could you give me an example or maybe a tip to modify the 'compare' rule??
February 12, 2007 3:25 PM
my mistake - you have to use the "custom" rule.
you build a function that checks whatever you need to check:
function check_textarea($textarea_value){
if (
isset($_POST["checkbox"]) &&
$_POST["textarea"] == ""
) {
return false;
}
return true;
}
and now you add a custom rule to the textarea
$obj = & $form->add("textarea", "textareaname");
$obj->setRule(array("custom"=>array("check_textarea", "", "error_block", "If checkbox is selected than this textarea is mandatory!")));
Although I did't test the code, it should look pretty much like that. (notice that i left an empty "" in the rule - i'm not sure what happens if you take that out :)
February 12, 2007 6:35 PM
btw I just noticed that you forgot to implement anti - html-injection functions :D
February 12, 2007 6:47 PM
CAPTCHA coming soon :)
February 12, 2007 7:02 PM
try to input to a textfield this code
tmp" < INPUT type="text" value="inject
of course remove the space between < and INPUT
February 12, 2007 7:05 PM
ok, i see what you mean
February 12, 2007 7:41 PM
something like this should do the trick but i can't find where the class returns the value to the textbox after submitting the form :/
foreach ($_POST AS $key => $val)
{
$val=htmlspecialchars(stripslashes($val), ENT_QUOTES);
$_POST[$key]=$val;
}
February 12, 2007 7:47 PM
i already fixed it. thanks a lot for spotting it out. i'll upload the fixed version as soon as i can
February 14, 2007 7:19 AM
Hi Stefan, the checkbox problem which I reported the last time is still there.You told me last time that it got fixed. I have downloaded the latest version and checked. The problem is still persisting.
February 14, 2007 9:20 AM
i am not sure what happened. i think i accidentaly overwritten the zip files. anyways, i uploaded the latest version that i have but this i am not sure about how stable it is as i am working on some stuff right now and i might've broken things...
February 14, 2007 11:15 AM
Thanks for replying. ANother thing which I have noticed was that we can't give key-value pairs in select control. We can but in that case the mandatory rule doesn't work. If for example I give
$obj = & $form->add("select", "age");
$obj->addOptions(array(0=>"- select -", in=>"India", uk=>"UK", us=>"USA"));
As per your program, option at 0th index is always considered as not selected and mandatory rule works in that ways. But if I am giving $obj->addOptions(array("- select -", "Between 0 an 30", "Between 30 and 60", "Older than 60")); without specifying the keys then its perfectly working. I hope I am able to make you understand my problem. Can you give me solution for this??
February 14, 2007 11:46 AM
fixed it, just download the zip again. thanks man!
February 14, 2007 2:07 PM
Can you tell me in which file you made the changes. I have downloaded the stuff but couldn't get it.
February 14, 2007 4:32 PM
? but anyway: the changes were done in class.htmlform.php, lines 337 to 343 are new and line 355 was changed
February 16, 2007 10:10 AM
Hey Stefan. Can't upload files through the file upload control from your class. I guess as the action parameter can't be set, I am not getting values from $_FILES. Now the point is, how to set the action for the script or how to get the file uploading task done.
February 16, 2007 11:33 AM
yes you are right. it is because i didn't upload modified version of the template file. please download the class again but bear in mind that this is a work-in-progress version and may have bugs. i'll have the final version ready later on today or tomorrow the latest
February 16, 2007 11:40 AM
can u give me the URL of the zip file?
February 16, 2007 12:19 PM
it's the same: http://www.nivelzero.ro/htmlform1.0.zip
February 01, 2007 8:37 AM
i am from Thailand. i d/l your phpdatagrid v.1.0 beta and i try it in example4.php (using checkbox)i found i can't check which checkbox was checked by user ? have you any example to check it. and please tell me.
thank you for advanced. ;-)
February 16, 2007 3:04 PM
Hi Stefan, I have downloaded the version you specified. Noticed one thing. You can't specify mandatory rule for file as there is no provision for file in the case mandatory of switch case in class.htmlform.php. I tried to put it with as $attribute["type"] == "file") && !$control->submittedValue, then it gave me mandatory error message even if I am selecting the file
February 17, 2007 5:04 PM
Please confirm when you upload the final version on the site
February 17, 2007 5:15 PM
Hi!
I just uploaded the latest version, BETA 3. Read about it here and follow the link to download.
Thanks for all the testing and all the useful comments. I hope you can further test this script but also my other scripts.
February 18, 2007 10:07 AM
Thanks a lot Stefan. Actually I am working on a project and taking the help of your classes.
February 19, 2007 2:44 PM
Hi
Is captcha working or is it still in development. I didn't find anything in the documentation but there's a captcha class. Could you give an example how to use it??
February 19, 2007 3:04 PM
yes, captcha is working. see the "registration" example
February 19, 2007 3:33 PM
sorry to bother you but i downloaded the class from the address u gave - http://www.nivelzero.ro/htmlform1.0.zip and there's no registration example, the only example there is is the old one from the previous version as far as i know
February 19, 2007 3:56 PM
there were 2 new relases of this class since this post. check my blog (not the comments, not this post, but the home at http://stefangabos.blogspot.com) and then go to the downloads area
February 21, 2007 9:03 AM
Hi Stefan. Can't we set the selected property for option(s)in the select control if multiple selection is done.For example, $obj = & $form->add("select", "select","ro");
$obj->addOptions(array("- select -", "ro"=>"Romania", "us"=>"United States", "br"=>"Brazil", "de"=>"Germany")); will result in Romania as selected value. But If I do $obj = & $form->add("select", "option[]", "", array("multiple"=>"multiple", "size"=>3, "style"=>"height:60px"));
$obj->addOptions(array(I like this PHP class", "I don't like this PHP class", "What's PHP?"));, can't I set one or more options as selected for this select control. I hope I am able to explain my point.
February 21, 2007 9:29 AM
Hi Stefan, its working.Actually I need to pass the key of the element as the 3rd parameter in the add(). Now my question is, I can set only one option as selected in case of multiple, but hot to set more than one option as selected.
March 05, 2007 1:23 AM
A few questions for you (I'm a relative noob to php).
Is it possible to load select lists dynamically from a database. If so, how? If not, how>
When using the file class how do you check against a list of valid file extensions that you want to allow to upload?
What is an easy method of retrieving data from the POST command?
Many thanks for your help.
Jay
March 06, 2007 9:02 AM
Me again :-(
I have the following code in my webpage:
$f->add("label","label_userfile","ufile","Your File");
$obj = & $f->add("file", "ufile","");
$obj->setAttributes(array("maxlength"=>255,"size"=>50));
$obj->setRule(array("mandatory"=>array("e1", $isRequired)));
When I run this file I get the following error message:
Notice: Undefined index: ufile in /home2/guidedis/public_html/development/includes/htmlformValidator/class.htmlform.php on line 357
The development machine is running PHP 5.0.1
The web server is running PHP 4.4.4
What is need to do to solve this problem.
Regards
Jay
March 06, 2007 10:28 AM
if you have the latest version that just put a "@" on line 362 so that $_FILES[$attribute["name"]]["name"] becomes @$_FILES[$attribute["name"]]["name"] and that should fix the problem.
if that's not what you have on line 362 than just download the latest version.
thanks for reporting!
March 07, 2007 9:48 AM
Using the @ symbol, in this instance, is like saying "I'll cross my fingers and hope the problem will go away".
Well the problem did "go away", sort of. I no longer get an error message but the file does not upload.
Now the problem isn't with your code it is with my ISP. Somehow they have blocked the $_FILES variable.
May I suggest that you, and your users, remove the @symbol because I can see it creating more problems than it solves.
If people are having the same problem they might want to add within the $f->validete() { } area the following:
phpinfo(INFO_VARIABLES);
March 26, 2007 8:57 PM
Its functionality is great but I think it has some bug in it. I have tried adding almost all the controls on the form and it was successful but when I tried to add radio button or checkbox, they are not getting added onto the form. If I am directly printing the output of toHTML(), radio button and checkbox are getting rendered but when I am adding them to form, they are not visible on the form.
Reagards,
Nick
March 27, 2007 8:49 AM
that's because you have to add them as {controls.checkboxId_checkboxValue}. so if you have a checkbox named "agreement" having a value of "yes", you'll be adding it to the form as {controls.agreement_yes}
March 08, 2007 11:42 PM
Hi Stefan,
I'm working with the radio control. How do I set the checked parameter ?
March 29, 2007 9:39 PM
How can we validate multiple forms & templates for ex. is this allowed?
if ($form1->validate()) {
// form1 is valid, show next
$form2->render("second.xtpl");
if($form2-validate()) {
... process ...
}
}
$form1->render("first.xtpl");
February 22, 2007 6:50 AM
Hi Stefan, can't we give two options in a single select control with the same key through your class?
September 19, 2007 11:23 AM
Carlos,
I have put a form with several submit buttons (with 1 hidden input for the button choice, and a "onclick" attribute for esch submit button), but after the validation of the form, the "value" attribute of the hidden input is always empty.
How do you recognaize which submit button was used ?
thanks
September 19, 2007 11:55 AM
first of all, the way you implemented the thing is not quite ok.
in the way you have it, you must set the hidden controls "locked" attribute - see the documentation for the hidden controls.
my advice is to give names and id's to the submit buttons:
<input type='submit' name='b1' id='b1' vale='Button 1'>
<input type='submit' name='b2' id='b2' vale='Button 2'>
and then check what button was pressed like this:
if (isset($_GET['b1']) && $_GET['b1']=='Button 1') {
// do stuff for when button 1 is pressed
} elseif (isset($_GET['b2']) && $_GET['b2']=='Button 2') {
// do stuff for when button 1 is pressed
}
September 19, 2007 4:54 PM
Thanks Stefan,
Here is what I
$b_modify = & $form->add("SUBMIT", "modify", "Modifier");
$b_delete = & $form->add("SUBMIT", "delete", "Supprimer");
$b_modify->setAttributes(array("id"=>'modify'));
$b_delete->setAttributes(array("id"=>"delete"));
...
and after validation:
if (isset($_GET['modify']))
print "variable set! ";
but the variable never exists ...
September 19, 2007 5:50 PM
that's probably because the form's default submission method is POST. In order to have the form submitted using GET you must declare the form like this:
$form = new HTMLForm("form_name, "get");
May 30, 2008 6:25 AM
looks great, but im stupid - Where is the documentation? im looking everywhere, and searching google phpclasses.org ?