form_class
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
form_class [2019/10/22 19:35] – [Easy forms] sahl | form_class [2021/03/11 19:44] (current) – [Easy forms] Adjusted file name sahl | ||
---|---|---|---|
Line 52: | Line 52: | ||
In order to provide an easier way to design forms, one can prepare a special html file | In order to provide an easier way to design forms, one can prepare a special html file | ||
- | <code html testform.frm> | + | <code html testform.form> |
< | < | ||
<text label=" | <text label=" | ||
Line 61: | Line 61: | ||
<code php> | <code php> | ||
- | $form = new Form(' | + | $form = new Form(' |
</ | </ | ||
Line 68: | Line 68: | ||
Never use real html form elements, such as < | Never use real html form elements, such as < | ||
+ | ===== The HTML form element ===== | ||
+ | The form is rendered as a form with an ID equal to the form id given under construction, | ||
+ | <code php> | ||
+ | $form = new Form(' | ||
+ | </ | ||
+ | |||
+ | gives | ||
+ | |||
+ | <code html> | ||
+ | <form id=" | ||
+ | </ | ||
+ | |||
+ | ===== Additional validation ===== | ||
+ | The form will validate itself from some basic rules such as having required fields filled in, and valid input according to field types, but sometimes extended validation is necessary. For frontend validation basic javascript can be used on the form, but in the backend one needs to add a validation function to the form. | ||
+ | |||
+ | <code php> | ||
+ | function myValidationFunction($form) { | ||
+ | if ($form-> | ||
+ | $form-> | ||
+ | return false; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $form-> | ||
+ | </ | ||
+ | |||
+ | The validation function is passed the form, and should return true if everything is OK, otherwise it should trigger an error on the appropriate form field and return false. | ||
+ | |||
+ | ===== Handling server side errors, when posting with javascript ===== | ||
+ | |||
+ | When doing a normal post, server side errors are automatic added to the form, but when posting using javascript, you need to pass server side errors back to the form. Platform provides easy functions to do that. | ||
+ | |||
+ | <code javascript frontend.js> | ||
+ | $.post(' | ||
+ | if (data.status == 1) { | ||
+ | // Everything is fine | ||
+ | } else { | ||
+ | // There was backend errors. Add them to the form. | ||
+ | add_errors_to_form(form, | ||
+ | } | ||
+ | }, ' | ||
+ | </ | ||
+ | |||
+ | <code php backend.php> | ||
+ | if ($form-> | ||
+ | else $result = array(' | ||
+ | echo json_encode($result); | ||
+ | </ |
form_class.1571772932.txt.gz · Last modified: 2019/10/22 19:35 by sahl