Contao forms in modules

Always wondered how to generate forms in modules so that I could use contao’s form generator – Instead of doing that I had to write the forms myself. Now I know how to do it.

If you know the id of your form you can select it using:

$obj = new \stdClass();
$obj->form = $id;

If you do not know the id of your form, you can get the id using the alias (so yeah, you have to know the alias or the id):

$model = \FormModel::findByAlias($alias);
$id  = $model->id;

$obj = new \stdClass();
$obj->form = $id;

Now that you’ve got the id, you can create the form:

$form = new \Form($obj);

and finally render it:

$formRendered = $form->generate();

echo it or assign it to your Template.

No Comments

Post a Comment