Forms creation
Form creation in MV has principles similar to model creation. You need to specify the list of fields, after that to call the methods to display form and make its data validation. There are 2 approaches for the form creation: display the form from a ready model or create a new form separately, using array of fields.
Form from model
Suppose we have a model of questions and answers, on the website you need to organize an option for users to leave questions, which will be moderated in admin panel before it's published on the site.
Sample
<? class Faq extends Model protected $name = "Questions and answers"; protected $model_elements = array( array("Activation", "bool", "active"), array("Name", "char", "name", array("required" => true)), array("Date", "date_time", "date"), array("Text of question", "text", "question", array("required" => true)), array("Text of answer", "text", "answer") ); public function display() { ... //Display a list of questions with answers on the website } } ?>
This way we have a ready model, part of the fields of which we need to display on the website as a form for visitors to ask new questions. The fields we need are: "Name" and "Text of question". Suppose, our form is displayed in a view "view-help.php".
<? //Define a page in a menu $content = $mv -> pages -> defineCurrentPage($mv -> router); $mv -> display404($content); //Create a form from a model $form = new Form("Faq"); ?> <form action="?ask" method="post"> <h3>Ask a question</h3> <table> <? //Display only 2 fields in required order echo $form -> display(array("name", "question")); ?> </table> <div class="form-submit"> <input type="submit" value="Send" /> </div> </form>
When the form was submitted, we need to get POST data to validate the form fields values. We have to check only 2 fields of form.
<? $form = new Form("Faq"); if(!empty($_POST)) { $form -> getDataFromPost() -> validate(array("name", "question")); ... } ?> <form class="regular simple" action="" method="post"> <h3>Ask a Questions</h3> <? echo $form -> displayErrors(); ?> ... </form>
After form fields validation we need to insert a new question into database and reload the page to reset POST data.
<? $form = new Form("Faq"); if(!empty($_POST)) { $form -> getDataFromPost() -> validate(array("name", "question")); if($form -> isValid()) { $record = $mv -> faq -> getEmptyRecord(); $record -> name = $form -> name; $record -> question = $form -> question; $record -> date = I18n :: getCurrentDateTime(); $record -> create(); $mv -> reload("?send"); } } ?> ... <? //The next code depends on your template if(!$form -> isValid()) echo $form -> displayErrors(); else if(isset($_GET['send'])) { echo "<p>Your question was successfully sent."; echo "In a short time the answer will be published in this section.</p>"; include $mv -> views_path."main-footer.php"; exit(); } if(empty($_GET)) echo $mv -> faq -> display(); ?>
The form can be also created based on specific record of the model. We may have such a need when editing a record on the frontend part of the website. So the id of required record from the model will be a second parameter when creating a form, in that case form fields will be filled with the values of that record.
$form = new Form("Clients", 52); $form -> loadRecord(); //You can pass an array of fields to be populated with record's data $form -> loadRecord(array("first_name", "last_name", "phone", "email"));
Form without binding with a model
To create a form that is separated from a model, you need to specify the list of fields. The methods of operating with such a form are similar to previous example.
<? $fields = array(array("Имя", "char", "name", array("required" => true)), array("Email", "email", "email"), array("Message", "text", "message", array("required" => true)) ); $form = new Form($fields); ?>
Attention! If the form contains file or image fields, then you need to add the following JavaScript (supposed you already have jQuery library included). This script allows to delete the selected file and return an input for uploading of a new file.
$(document).ready(function() { $("form .field-input span.delete").click(function() { $(this).parents("div.file-params").empty().next().show(); }); });
Previous section
Special methods