Forms Creation

Form creation in MV has the same similar principles as model creation. You need to specify the list of fileds, after that to call the methods for diplay and form data validation. There are 2 approaches for the form creation: display the form from a ready model or create a new form separately.

Form from a 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.

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/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. The fields we need are: "Name" and "Text of question ". Suppose, our form is displayed in a template "view-help.php".

<?
//Define a page in a menu
$content = $mv -> pages -> findContent(array("url" => "help", "active" => 1));
$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>

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

}
?>

<form class="regular simple" action="?ask" 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())
    {
        $mv -> faq -> getDataFromArray($form -> getAllValues());
        $mv -> faq -> setValue("date", I18n :: getCurrentDateTime("with-seconds"));
        $mv -> faq -> create();

        $mv -> redirect("help/?send");
    }
}
?>

...

<?
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. Thus the id of required record from the model will be a second parameter when creating a form, in that case form fields will be replaced 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 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 (suppose you already have JQuery library included). This script allows to delete the selected file and to return a field for loading of a new file.

$(document).ready(function() 
{ 
    $("form .field-input span.delete").click(function() 
    { 
        $(this).parents("div.file-params").empty().next().show(); 
    }); 
});