0

I am using this example Bootstrap3 example to render a form and was wondering if it was possible to add custom classnames to form input wrappers based on the control type. I tried the following but it applies to the form inputs and not the surrounding wrapper divs (form-group in this case):

foreach ($form->getControls() as $control) {
    $type = $control->getOption('type');
    $control->getControlPrototype()->addClass('form-' . $control->getControlPrototype()->type);
}
Niraj Pandey
  • 309
  • 1
  • 9
  • 21
  • 3
    Better is to use some renderer. See https://github.com/nextras/forms/blob/master/src/Rendering/Bs3FormRenderer.php. – hrach Feb 04 '16 at 12:23

1 Answers1

1

The input wrapper is not a concern of the Control (input) but of the Renderer. DefaultFormRenderer wraps the control in the renderPair method – other renderers do not even have to do any wrapping. For that reason you can not get the wrapper’s prototype.

You can use $control->setOption('class', …), though, and DefaultFormRenderer will use it as a class for the wrapper (as you can see in the source of DefaultFormRenderer::renderPair).

Rather than manipulate the the form directly, it is much cleaner to implement a custom IFormRenderer as @hrach mentions above. His Bs3FormRenderer is a nice example.

Jan Tojnar
  • 5,306
  • 3
  • 29
  • 49
  • 1
    Or you can call $control->setAttribute('class', 'your-class'); – John Mar 07 '16 at 21:18
  • 1
    @John That is not what OP was asking for. [`setAttribute`](https://api.nette.org/2.3.8/Nette.Forms.Controls.BaseControl.html#_setAttribute) refers to control itself (i.e. the input), not the wrapper. – Jan Tojnar Mar 08 '16 at 00:16