ZF2 form formatting?

2019-07-06 22:26发布

问题:

I am using ZF2 and have a form which defines a bunch of elements, and then I render it in my phtml like this:

<?php 

$form = $this->form;
$form->prepare();

echo $this->form()->openTag($form);
echo $this->formlabel($form->get('description'));
echo $this->formRow($form->get('radioButton'));
echo $this->form()->closeTag();

?>

Which draws a label and a radio button. My question is how can I then format these elements to my liking? For example make the radio buttons displayed horizontally rather than vertically and maybe change the location of the label.

回答1:

There's nothing stopping you from formatting them as you have there, you could put the elements inside a list, or andd any additional markup you want to style as you wish.

<?php 
$form = $this->form;
$form->prepare();

echo $this->form()->openTag($form);
?>
<ul class="form-list">
    <li>
    <div class="form-control">
        <?php echo $this->formlabel($form->get('description')); ?>
        <?php echo $this->formElementErrors($form->get('description')) ?>
        <?php echo $this->formElement($form->get('description')); ?>
    </div>
    <div class="form-control">
        <?php echo $this->formlabel($form->get('radioButton')); ?>
        <?php echo $this->formElementErrors($form->get('radioButton')) ?>
        <?php echo $this->formElement($form->get('radioButton')); ?>
    </div>
    </li>
</ul>
<?php echo $this->form()->closeTag() ?>

If you wanted to have control over the actual elements/inputs themselves you could do something like this:

<label>
    <?php echo $form->get('radioButton')->getLabel() ?>
    <input class="bob" type="radio" 
           name="<?php echo $form->get('radioButton')->getName() ?>"
           value="<?php echo $form->get('radioButton')->getValue() ?>"
    />
</label>