Skip to content

Commit f984e7f

Browse files
docs: Document custom plugin forms
1 parent fdbd7d9 commit f984e7f

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

content/en/v1/plugins/forms.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
title: Custom Forms
3+
toc: true
4+
menu:
5+
main:
6+
parent: Developing Plugins
7+
---
8+
9+
Adding modal forms in your plugin
10+
---------------------------------
11+
12+
### Opening a modal form from a template
13+
14+
Using the `Kanboard\Helper\ModalHelper` class, you can open contextual menus (modals) to perform additional actions then redirect to a specific. For example from your template, for a `myform` action in your plugin's controller:
15+
16+
```phtml
17+
<?= $this->modal->small('font-awesome-icon', 'My Form text', 'MyController', 'myform', array('plugin' => 'myplugin')) ?>
18+
```
19+
20+
If you'd like your modal to redirect to the current page after submission, don't forget to give it additional params to generate the redirection. For example, adding the task's ID to redirect to the task view:
21+
22+
```phtml
23+
<?= $this->modal->small('font-awesome-icon', 'My Form text', 'MyController', 'myform', array('plugin' => 'myplugin', 'task_id' => $task['id'])) ?>
24+
```
25+
26+
### Displaying the form
27+
28+
Using the `Kanboard\Helper\FormHelper` class, you can generate your form, automatically handling submitted values and validation errors:
29+
30+
```phtml
31+
<form method="post" action="<?= $this->url->href('MyController', 'myform', array('plugin' => 'myplugin', 'task_id' => $task_id)) ?>" autocomplete="off">
32+
<?= $this->form->csrf() ?>
33+
<?= $this->form->label('Single line field', 'first_field') ?>
34+
<?= $this->form->text('first_field', $values, $errors) ?>
35+
<br>
36+
<?= $this->form->label('Text area field', 'second_field') ?>
37+
<?= $this->form->textarea('second_field', $values, $errors) ?>
38+
<?= $this->modal->submitButtons() ?>
39+
</form>
40+
```
41+
42+
Do not add a second form in the same modal. Only [one form per modal is supported](https://github.com/kanboard/kanboard/issues/5695). If you need to handle several forms from your modal, simply open a new modal from the current modal, like so:
43+
44+
```phtml
45+
<h2>Select a project:</h2>
46+
<form method="post" action="<?= $this->url->href('MyController', 'myform', array('plugin' => 'myplugin', 'task_id' => $task_id)) ?>" autocomplete="off">
47+
<?= $this->form->csrf() ?>
48+
<?= $this->form->select('project_id', $projects, $values, $errors) ?>
49+
<?= $this->modal->submitButtons() ?>
50+
</form>
51+
Or alternatively, create a new one:
52+
<?= $this->modal->small('plus', 'Create project', 'MyController', 'createproject', array('plugin' => 'myplugin', 'task_id' => $task_id)) ?></b>
53+
```
54+
55+
Clicking the `Create project` link will close the current modal and open a new one with the `createproject` action, avoiding confusion between forms.
56+
57+
### Validating the form
58+
59+
To check if the request is a fresh form, or contains submitted values, you can use `$this->request->isPost()`. After validating the submitted values in `$this->request->getValues()`, you should indicate success and redirect somewhere:
60+
61+
```php
62+
$this->flash->success('My form was successful');
63+
$this->response->redirect($this->helper->url->to('TaskViewController', 'show', array('task_id' => $this->request->getStringParam('task_id'))), true);
64+
```
65+
66+
Or upon error, redisplay the current form with the `$errors` and `$values` arrays in the template context:
67+
68+
```php
69+
$this->flash->failure('My form failed');
70+
$this->response->html($this->template->render('myplugin:myform', array(
71+
'task_id' => $task_id,
72+
'errors' => $errors,
73+
'values' => $values,
74+
)));
75+
```
76+
77+
Whether you're performing manual form validation, or using custom validators, the form helpers will display the errors alongside the erroneous submitted fields. For this purpose, make sure `errors` has one entry for each errored field, containing an array of error messages. For example:
78+
79+
```php
80+
$errors = [ "age" => [ "Age cannot be a negative number" ] ];
81+
```

0 commit comments

Comments
 (0)