Nette forms documentation lacks information about enhancing your forms while still using the default renderer. I offer here a list of solutions for the most common needs, using getControlPrototype(), getLabelPrototype(), getSeparatorPrototype() and setOption().
The code goes into your form factory method in the presenter. Using the default renderer means you can’t tweak the look in your template, you have to do it when you define the form.
Formatting
Very often it’s needed to add a css style or class to a field or its label. You can modify other elements too though. For instance, you can automatically place user’s text cursor into a particular field after the page loads.
-
span class=”st0″>’subject’, ‘Subject:’‘wideField’‘terms’, ‘Terms and Conditions:’)
-
->getLabelPrototype()->style(‘white-space: nowrap;’);
-
Radio buttons on a single line
Default renderer will place each radio button/label pair on a new line. What happens in terms of Nette is that it uses a line break as a separator. Here’s how to remove (or replace) the separator.
-
span class=”st0″>’one’ => "One", ‘two’ => "Two"‘type’, ‘Type:’
Read-only
Make a form field (or a set of form fields) read-only – aka disabled. This field’s value will then not appear among $form->getValues().
-
span class=”st0″>’type’
Description
Piece of text or html element printed after the form field.
-
span class=”st0″>’password’, ‘Password:’)
-
->setOption(‘description’, "Leave empty if you don’t want to change.");
Will be rendered as
-
<td><input type="text" … /> <small>Leave empty…</small></td>
What if you want the description to start on a new line? Or be something else than a plain text?
-
span class=”st0″>’password’, ‘Password:’)
-
->setOption(‘description’,
-
NHtml::el(‘div’)->setHtml(
-
NHtml::el(‘small’)->setText("Leave empty if you don’t want to change.")
-
)
-
);
Printing a description with form macros
When rendering your forms semi-manually with macros {form}, {label} and {input} you may also need to print the description. Nette doesn’t ship with any handy macro for that purpose but you can create your own. Best place would be your BasePresenter:
-
span class=”st0″>’description’,
-
‘$__desc = $form[%node.word]->getOption("description"); echo %escape($__desc);’
-
);
-
-
$template->registerFilter($latte);
-
}
Additional information on defining your own Latte macros. Don’t expect much though, it’s pretty brief.
Other things you can set with setOption()
(taken from API for Nette\Forms\ControlGroup)
- label
- String or Html object – Setting this option only makes sense for a group where it replaces a label defined previously with addGroup()
- container
- Html object – Again, only makes for a group. Good for wrapping it with a DIV for example.
- description
- String or Html object – Displayed after the form field, inside of a fieldset when talking about a group. Doesn’t work for buttons (addSubmit/addButton)
- visual
- Wraps a group with a fieldset when true.
- embedNext
- Understands the next group as a child when true, or as a sibling if false (default).
(Nette\Forms\Rendering\DefaultFormRenderer)
- class
- css class
- id
- id attribute
Tags: Nette Framework 2.0-beta (revision f38d86f released on 2011-08-24)
-
-
[…] cookbook and the built-in n:href macro. You can also take a peek an another post of mine on how to tweak your forms using macros. As always, hopefully it’ll save you some […]