Milk Admin

Introduction to form creation

Quick Form Creation with FormBuilder

This is a manual, artisanal system for creating forms. If you need to create forms quickly from your Models, we recommend using the FormBuilder which can generate complete forms in minutes:

→ Getting Started - Forms with FormBuilder

The following documents show how to print various form elements through two systems: the FORM class and template plugins.
However, there is a faster system to print a form starting from the structure of the class that extends AbstractObjects.

Form creation

<div class="card-body">
   <?php 
       echo ObjectToForm::start($page, $action_save);
       ?>
       <div class="form-group col-xl-6">
       <?php
           // extract all form fields with edit = true
           foreach ($data->getRules('edit', true) as $key => $rule) {
               // Automatically generates the form field based on the rule
               echo ObjectToForm::row($rule, $data->$key);
           } 
       ?>
       </div>
       <?php
       echo ObjectToForm::submit();
       echo ObjectToForm::end();
       ?>
   </div>

$data->getRules('edit', true) extracts all class fields that have the edit property set to true. Considering that custom properties can be added within the rules, this allows creating automatic field groups to extract various fields. ObjectToForm::row($rule, $data->$key) converts the various fields into form elements.

This way it's possible to create form parts or entire forms more quickly

Data saving

Always if an Object class extending AbstractObject and a model class extending AbstractModel have been set up, during the saving phase you can take advantage of a series of facilitations.

$obj = $this->model->getEmpty($_REQUEST);

if ($obj->validate()) {
   if ($obj->save()) {
       ...
   }
}

$this->model->getEmpty($_REQUEST) returns a Model instance with data compiled from $_REQUEST.

$obj->validate() validates the internal data based on the Object class rules.

$obj->save() saves the internal data to the database.

Obviously all this is optional and should be used only if it facilitates the work.

Loading...