We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Form Generator

Hello all !

I just created a new tool to render a form from a database.

There is the repository : https://github.com/Zheness/phalconFormGenerator

It's not perfect, but it's working, and it can save time by generating form.

This is an example of what this tool do (read README file for configuration):

My SQL table:

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(65) NOT NULL,
  `lastname` varchar(65) NOT NULL,
  `email` varchar(105) NOT NULL,
  `password` varchar(255) NOT NULL,
  `isAdmin` tinyint(1) NOT NULL,
  `biography` text NOT NULL,
  `short_description` varchar(1000) NOT NULL,
  `user_status_id` int(11) DEFAULT NULL,
  `last_date_login` date DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user_status_id` (`user_status_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

The form generated:

class UsersForm {
    private function _Firstname() {
        $element = new \Phalcon\Forms\Element\Text("Firstname");
        $element->setLabel("Firstname");
        $element->addValidator(new \Phalcon\Validation\Validator\StringLength([
            "max" => 65
        ]));
        return $element;
    }
    private function _Lastname() {
        $element = new \Phalcon\Forms\Element\Text("Lastname");
        $element->setLabel("Lastname");
        $element->addValidator(new \Phalcon\Validation\Validator\StringLength([
            "max" => 65
        ]));
        return $element;
    }
    private function _Email() {
        $element = new \Phalcon\Forms\Element\Text("Email");
        $element->setLabel("Email");
        $element->addValidator(new \Phalcon\Validation\Validator\StringLength([
            "max" => 105
        ]));
        return $element;
    }
    private function _Password() {
        $element = new \Phalcon\Forms\Element\Password("Password");
        $element->setLabel("Password");
        return $element;
    }
    private function _Isadmin() {
        $element = new \Phalcon\Forms\Element\Check("Isadmin");
        $element->setLabel("Isadmin");
        return $element;
    }
    private function _Biography() {
        $element = new \Phalcon\Forms\Element\Textarea("Biography");
        $element->setLabel("Biography");
        return $element;
    }
    private function _ShortDescription() {
        $element = new \Phalcon\Forms\Element\Textarea("ShortDescription");
        $element->setLabel("ShortDescription");
        return $element;
    }
    private function _UserStatusId() {
        $element = new \Phalcon\Forms\Element\Select("UserStatusId");
        $element->setLabel("UserStatusId");
        $element->setOptions([]);
        return $element;
    }
    private function _LastDateLogin() {
        $element = new \Phalcon\Forms\Element\Date("LastDateLogin");
        $element->setLabel("LastDateLogin");
        return $element;
    }
    public function setFields() {
        $this->add($this->_Firstname());
        $this->add($this->_Lastname());
        $this->add($this->_Email());
        $this->add($this->_Password());
        $this->add($this->_Isadmin());
        $this->add($this->_Biography());
        $this->add($this->_ShortDescription());
        $this->add($this->_UserStatusId());
        $this->add($this->_LastDateLogin());
    }
}

The next step is to copy the files in your project, modify labels, add options, add validators/filters, remove fields, etc.

If someone has ideas, feel free to contribute !

I hope this tool will help you !

Merry Christmas to all and to the Phalcon Team !



12.2k
edited Dec '15

Sorry, but this is not good as it can be or should be. What if I want to use form somewhere else? I have to repeat this again and agagin in every controller?

In ideal world form's call have to be no more than 1 line.


$form = new FormBuilder(new \Model\User());

In result I can call this form builder everywhere in the porject.

fx. I did something similar but in model. If you are interested I can share an example unfortunately my solution can't build selectboces with related tables, and does not allow embedding forms to each other. It just renders input fields and adds validators.



12.2k

I do not see any benefits to try move that to the CLI. Honestly. None of the frameworks do not generate it using CLI. Correct me if I'm wrong.

below is chunk of my model which used for Form Building There are so many variations can be so there are any any sense to create CLI comand for this, from my point of view

    /**
     *
     * @Column(type="string", nullable=true, column="login")
     * @FormOptions(type=text, length=32, label="Login")
     * @FormValidator("Phalcon\Validation\Validator\PresenceOf":{'message': 'The Username field is required'})
     * @var string
     */
    protected $login;

    /**
     * @Column(type="string", nullable=false, column="email")
     * @FormOptions(type=email)
     * @FormValidator(
     *  "Phalcon\Validation\Validator\PresenceOf":{'message': 'The e-mail field is required'},
     *  "Phalcon\Validation\Validator\Email":{'message': 'E-mail is not valid'}
     * )
     * @var string
     */
    //"Phalcon\Validation\Validator\Db\Uniqueness":{'message': 'The E-mail is already taken', "table":"users", "column": "email"}
    protected $email;

    /**
     * @Column(type="string", nullable=true, column="password")
     * @FormOptions(type=password)
     * @FormValidator(
     *  "Phalcon\Validation\Validator\PresenceOf":{'message': 'The password is required'},
     *  "Phalcon\Validation\Validator\StringLength":{'messageMinimum': 'Password is too short. Minimum 8 characters', 'min':8},
     *  "Phalcon\Validation\Validator\Confirmation":{'message': 'Password doesn\'t match confirmation', 'with': 'confirmPassword'}
     * )
     * @var string
     */
    protected $password;

Hi !

Thank you for the feedback ! I was in a hurry when i developed this functionnality, and I choose a simple phalcon project. But yeah, I tought that a CLI tools could be better.

But with a CLI tools, we can't set a lot of options. Here the generation is very basic/simple. All options are not functionnal (Select linked with real tables, validations, etc.).

I pushed this tool because this version helps me a lot for the moment. I will probably add some features, start a CLI project, etc. But it's not my priority ^^ Again I choose an open source project, so every body can modify this tool :)

Thanks again.

PS: And I never done CLI program with phalcon, and I don't think phalocn-devtool has a command for this.