Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Reuse form elements in many forms, is it possible ?

let me explain by example :
Models : field1, field2 ...
Employees : Name, Surname
Departments : Tag, Name, Description
Ranks : Tag, Class, Name, Description
Projects : Code, Name
Clients : Code, Name, Address1, ..

As you can see, in the required 5 forms there are many common tags such as "Name", "Tag", "Code" etc

I catch my self repeating "Name" form element in every form file i create for the respective models
thus I wonder if there is a way to declare "Name" and use it in many different form files ?



37.9k

HI

You to create form Employees and in other form You used to extends class Employees.



22.3k
Accepted
answer
edited Mar '14

You can create a base form where you set those elements and then extend your other forms from it.

For instance:

<?php

use \Phalcon\Forms\Form as PhForm;
use \Phalcon\Forms\Element\Text as PhText;

class BaseForm extends Form
{
    private $params = [];

    public function initialize()
    {
        if (isset($params['name'])) {
            $this->add(new PhText("name"));
        }

        if (isset($params['code'])) {
            $this->add(new PhText("code"));
        }
    }
}

and your DepartmentsForm becomes:

<?php

use \Phalcon\Forms\Element\Text as PhText;

class DepartmentsForm extends BaseForm
{
    public function initialize()
    {
        // Set the fields we need
        $this->params['name'] = true;

        parent::initialize();

        $this->add(new PhText("Tag"));
        $this->add(new PhText("Description"));
    }
}

your ProjectsForm

<?php

use \Phalcon\Forms\Element\Text as PhText;

class ProjectsForm extends BaseForm
{
    public function initialize()
    {
        // Set the fields we need
        $this->params['name'] = true;
        $this->params['code'] = true;

        parent::initialize();
    }
}

HTH



6.2k
edited Mar '14

thank you very much :D
now i have to reengineer some dozens of forms but it really worth the trouble !!



6.2k
edited Mar '14

$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);
    }
}

I'm new to Phalcon and want to learn about the form. My question is: where should I put the form classes? In Controller folder, or Model, or create a new folder called Form? Thank you very much.

@manhthang2504, You should create new folder Form, for example https://github.com/duythien/blog/tree/master/app/forms Thank you, Duy Thien.