$params did not work as proposed :
private $params = [];
i post below the code that worked for me :
<?php
use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\TextArea;
use Phalcon\Forms\Element\Select;
use Phalcon\Forms\Element\Submit;
use Phalcon\Validation\Validator\StringLength;
use Phalcon\Validation\Validator\PresenceOf;
class BaseForm extends Form
{
public function initialize($entity = null, $options = null, $params)
{
if (isset($params['tag']))
{
$tag = new Text('tag', array('placeholder' => 'Tag', 'class' => 'form-control'));
$tag->setLabel('Tag');
$tag->addValidators
(
array
(
new StringLength(array('min' => 2,'messageMinimum' => 'Tag is too short. Minimum 2 characters')),
new StringLength(array('max' => 8,'messageMaximum' => 'Tag is too long. Maximum 8 characters')),
new PresenceOf(array('message' => 'The tag is required'))
)
);
$this->add($tag);
}
if (isset($params['name']))
{
$name = new Text('name', array('placeholder' => 'Name', 'class' => 'form-control'));
$name->setLabel('Name');
$name->addValidators
(
array
(
new StringLength(array('min' => 2,'messageMinimum' => 'Name is too short. Minimum 2 characters')),
new StringLength(array('max' => 64,'messageMaximum' => 'Name is too long. Maximum 64 characters')),
new PresenceOf(array('message' => 'The Name is required'))
)
);
$this->add($name);
}
if (isset($params['description']))
{
$description = new TextArea('description', array('placeholder' => 'Description, up to 512 characters', 'class' => 'form-control'));
$description->setLabel('Description');
$description->addValidators
(
array
(
new StringLength(array('max' => 512,'messageMaximum' => 'Description is too long. Maximum 512 characters')),
)
);
$this->add($description);
}
if (isset($params['status']))
{
$status = new Select('status', array('1' => 'Active', '0' => 'Inactive'), array('placeholder' => 'Status', 'class' => 'form-control'));
$status->setLabel('Status');
$status->setDefault(1);
$this->add($status);
}
}
and in dependent Form(s) :
<?php
class DepartmentForm extends BaseForm
{
public function initialize($entity = null, $options = null, $params)
{
$params['tag'] = true;
$params['name'] = true;
parent::initialize($entity, $options, $params);
}
}
<?php
class ProjectForm extends BaseForm
{
public function initialize($entity = null, $options = null, $params)
{
$params['name'] = true;
$params['description'] = true;
$params['status'] = true;
parent::initialize($entity, $options, $params);
}
}