HTML Forms Generation and Validation PHP Class 1.0 BETA

Tuesday, December 19, 2006
,

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.

96 comments:

  1. Alex said...

    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!

  2. Stefan Gabos said...

    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!

  3. Anonymous said...

    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

  4. Anonymous said...

    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?

  5. Stefan Gabos said...

    You are right, there was a bug in the class. Please download the class again to get the fixed version.

  6. beta tester said...

    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";

  7. beta tester said...

    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.

  8. beta tester said...

    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!

  9. Stefan Gabos said...

    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

  10. Stefan Gabos said...

    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!

  11. beta tester said...

    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.

  12. beta tester said...

    btw, we open the Firefox browser on the LAMP server. IE7 is on a different machine running on WinXP.

  13. beta tester said...

    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"

  14. Stefan Gabos said...

    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.

  15. Stefan Gabos said...

    ok, download the package again to get the fixed version

  16. beta tester said...

    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"?

  17. Stefan Gabos said...

    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!

  18. Stefan Gabos said...

    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 :)

  19. Stefan Gabos said...

    fixed.download the package again

  20. beta tester said...

    Cool! It's all working now. Firefox shows the icon and the class works fine on both browsers (IE7.0 & Firefox2.x). Thanks!!

  21. Sridhar said...

    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.

  22. Stefan Gabos said...

    Hello Sridhar,

    Thanks for reporting. I will check and get back to you later on today

  23. Stefan Gabos said...

    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}!

  24. Sridhar said...

    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.

  25. Stefan Gabos said...

    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.

  26. Sridhar said...

    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.

  27. Stefan Gabos said...

    in the template file you should have

    <!-- BEGIN: e6 -->
    {errors.e6}
    <!-- END: e6 -->

    and not just

    {errors.e6}

  28. elffikk said...

    Hi!
    How to set a predefined value to a select or checkbox/radio control?

  29. Stefan Gabos said...

    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"));

  30. elffikk said...

    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

  31. Stefan Gabos said...

    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"));

  32. beta tester said...

    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.

  33. Stefan Gabos said...

    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?

  34. Sridhar said...

    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.

  35. Stefan Gabos said...

    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

  36. Carlos said...

    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

  37. Stefan Gabos said...

    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

  38. Carlos said...

    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

  39. Anonymous said...

    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. ;-)

  40. Sridhar said...

    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?

  41. Sridhar said...

    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?"));

  42. Stefan Gabos said...

    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");

  43. Stefan Gabos said...

    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...

  44. Sridhar said...

    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.

  45. Stefan Gabos said...

    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

  46. Stefan Gabos said...

    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)

  47. beta tester said...

    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.

  48. beta tester said...

    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.

  49. Carlos said...

    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

  50. Sridhar said...

    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

  51. Stefan Gabos said...

    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

  52. Bartosz said...

    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

  53. Stefan Gabos said...

    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.

  54. Bartosz said...

    hmm, I don't get it, could you give me an example or maybe a tip to modify the 'compare' rule??

  55. Stefan Gabos said...

    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 :)

  56. Bartosz said...

    btw I just noticed that you forgot to implement anti - html-injection functions :D

  57. Stefan Gabos said...

    CAPTCHA coming soon :)

  58. Bartosz said...

    try to input to a textfield this code
    tmp" < INPUT type="text" value="inject

    of course remove the space between < and INPUT

  59. Stefan Gabos said...

    ok, i see what you mean

  60. Bartosz said...

    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;
    }

  61. Stefan Gabos said...

    i already fixed it. thanks a lot for spotting it out. i'll upload the fixed version as soon as i can

  62. Sridhar said...

    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.

  63. Stefan Gabos said...

    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...

  64. Sridhar said...

    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??

  65. Stefan Gabos said...

    fixed it, just download the zip again. thanks man!

  66. Sridhar said...

    Can you tell me in which file you made the changes. I have downloaded the stuff but couldn't get it.

  67. Stefan Gabos said...

    ? but anyway: the changes were done in class.htmlform.php, lines 337 to 343 are new and line 355 was changed

  68. Sridhar said...

    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.

  69. Stefan Gabos said...

    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

  70. Sridhar said...

    can u give me the URL of the zip file?

  71. Stefan Gabos said...

    it's the same: http://www.nivelzero.ro/htmlform1.0.zip

  72. Anonymous said...

    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. ;-)

  73. Sridhar said...

    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

  74. Sridhar said...

    Please confirm when you upload the final version on the site

  75. Stefan Gabos said...

    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.

  76. Sridhar said...

    Thanks a lot Stefan. Actually I am working on a project and taking the help of your classes.

  77. Bartosz said...

    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??

  78. Stefan Gabos said...

    yes, captcha is working. see the "registration" example

  79. Bartosz said...

    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

  80. Stefan Gabos said...

    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

  81. Sridhar said...

    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.

  82. Sridhar said...

    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.

  83. Anonymous said...

    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

  84. Anonymous said...

    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

  85. Stefan Gabos said...

    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!

  86. Anonymous said...

    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);

  87. Anonymous said...

    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

  88. Stefan Gabos said...

    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}

  89. Bert said...

    Hi Stefan,
    I'm working with the radio control. How do I set the checked parameter ?

  90. beta tester said...

    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");

  91. Sridhar said...

    Hi Stefan, can't we give two options in a single select control with the same key through your class?

  92. David said...

    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

  93. Stefan Gabos said...

    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

    }

  94. David said...

    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 ...

  95. Stefan Gabos said...

    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");

  96. Anonymous said...

    looks great, but im stupid - Where is the documentation? im looking everywhere, and searching google phpclasses.org ?

Post a Comment

GlossyBlue Blogger by Black Quanta. Theme & Icons by N.Design Studio
Entries RSS Comments RSS