User Tools

Site Tools


form_class

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
form_class [2019/10/22 19:35] – [Easy forms] sahlform_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>
 <h2>Please fill this form</h2> <h2>Please fill this form</h2>
 <text label="Type something" name="field_title"> <text label="Type something" name="field_title">
Line 61: Line 61:
  
 <code php> <code php>
-$form = new Form('form_id', 'testform.frm');+$form = new Form('form_id', 'testform.form');
 </code> </code>
  
Line 68: Line 68:
 Never use real html form elements, such as <form>, <input> and <select> as this will confuse the Platform form system. Never use real html form elements, such as <form>, <input> and <select> as this will confuse the Platform form system.
  
 +===== The HTML form element =====
 +The form is rendered as a form with an ID equal to the form id given under construction, so
  
 +<code php>
 +$form = new Form('testform');
 +</code>
 +
 +gives
 +
 +<code html>
 +<form id="testform">
 +</code>
 +
 +===== 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->getFieldByName('fullname')->getValue() == 'Hitler') {
 +    $form->getFieldByName('fullname')->triggerError('This name is not allowed here');
 +    return false;
 +  }
 +}
 +
 +$form->addValidationFunction('myValidationFunction');
 +</code>
 +
 +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('backend.php', form.serialize(), function(data) {
 +  if (data.status == 1) {
 +    // Everything is fine
 +  } else {
 +    // There was backend errors. Add them to the form.
 +    add_errors_to_form(form, data.errors);
 +  }
 +}, 'json');
 +</code>
 +
 +<code php backend.php>
 +if ($form->validate()) $result = array('status' => 1);
 +else $result = array('status' => 0, 'errors' => $form->getAllErrors());
 +echo json_encode($result);
 +</code>
form_class.1571772932.txt.gz · Last modified: 2019/10/22 19:35 by sahl

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki