globally change ZF2 form formatting

2019-03-01 07:01发布

问题:

I'm building forms from entities

$builder = new AnnotationBuilder( $entityManager);
$form = $builder->createForm( $entity );

This is a great feature (saves me a lot of work), but what I want is to globally change the layout of these forms. I want to use a table layout instead of

<label><span>Name:</span><input type="text" value="" name="name"></label>

I want something like

<tr><td>Name:</td><td><input type="text"></td></tr>

Is that possible?

回答1:

I actually was mistaken you do not need to actually extend or write a new viewHelper to achieve a diffrent kind of rendering.

In fact the formRow viewHelper has a partial parameter

public function __invoke(ElementInterface $element = null, $labelPosition = null, $renderErrors = null, $partial = null)

Once the viewHelper is called with that parameter set <?php echo $this->formRow($element, null, true, 'path/to/your/partial'); ?> it'll set following variables for your partial to use.

'element'           => $element,
'label'             => $label,
'labelAttributes'   => $this->labelAttributes,
'labelPosition'     => $this->labelPosition,
'renderErrors'      => $this->renderErrors,

Now you'll only have to write your own markup and you should be good to go.