a complete form
<?php
namespace Whatever\Frames\Forms;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Select;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\TextArea;
use Whatever\Models\{
FrameTypes, FrameColors
};
class Add extends \Phalcon\Forms\Form
{
public function initialize($entity = null, $options = [])
{
$name = new Text("name", [
"class" => "form-control"
]);
$this->add($name);
$text = new TextArea("text", [
"class" => "form-control"
]);
$this->add($text);
$hiddenField = new Hidden("id");
$this->add($hiddenField);
//this is your usual select
$primalColor = new Select("primal_color", FrameColors::find([
"conditions" => "shown != 0"
]), [
"class" => "form-control",
"using" => [
"id",
"name",
]
]);
if ($entity && $entity->getPrimalColor() !== 0) {
$primalColor->setDefault($entity->getPrimalColor());
}
$this->add($primalColor);
//this is select2 field with multiple options
$colors = new Select("alt_colors[]", [], [
"class" => "form-control frame-colors-input",
"multiple" => "multiple",
"using" => [
"id",
"name",
]
]);
if ($entity && false === empty($entity->getAltColors())) {
$str = $entity->getAltColors();
$pr = FrameColors::find([
"conditions" => "id in ({arr:array})",
"bind" => [
"arr" => explode(",", $str)
]
]);
$colors->setOptions($pr);
$colors->setDefault(explode(",", $str));
}
$this->add($colors);
}
}
now when you want to create a new product ( or whatever ) in your controller;
//create
$this->view->setVar("form", new Form());
//edit | update
$this->view->setVar("form, new Form( Model::findFirstById(1));
also as far as I remember, forms automatically was checking for post ( or get i am not 100% sure ), to automatically fill the data
honestly i write the radio buttons and checkboxes in the forntend and i handle "checked" there with ifs. but u can use the same logic as above
in volt is
this.view.getVar("form").render("-field-name-goes-here")