0

目前,我使用这个:

<?php echo $this->form()->openTag($form); ?>
        <?php echo $this->formHidden($form->get('page_id')); ?>
        <fieldset>
            <?php echo $this->formRow($form->get('title')); ?>
        </fieldset>
        <fieldset>
            <?php echo $this->formRow($form->get('content')); ?>
        </fieldset>
        <fieldset>
            <?php echo $this->formRow($form->get('url_slug')); ?>
        </fieldset>
        <fieldset>
            <?php echo $this->formSubmit($form->get('submit')); ?>
        </fieldset>
        <?php echo $this->form()->closeTag(); ?>

在视图脚本中显示我的表单,但我想添加一些额外的样式、html 属性等,所以最好用纯 html 编写表单。到目前为止一切顺利,但是否可以在不使用上述方法的情况下在 html 视图中显示表单错误?在 ZF1 中,我使用了:

 <ul>
    <?php foreach ($this->form->class->getMessages() as $key => $value): ?>
        <li><?php echo $value; ?></li>
    <?php endforeach; ?>

    <?php foreach ($this->form->letter->getMessages() as $key => $value): ?>
        <li><?php echo $value; ?></li>
    <?php endforeach; ?>

    <?php foreach ($this->form->type->getMessages() as $key => $value): ?>
        <li><?php echo $value; ?></li>
    <?php endforeach; ?>
</ul>
//the html of the form

任何帮助深表感谢。提前致谢。:)

4

1 回答 1

0

您可以使用 formElementErrors 视图助手来获取每个元素的错误消息,如下所示:

<ul>

    <li>
         <?php echo $this->formLabel($form->get($form->get('title')) ?>
         <?php echo $this->formElement($form->get($form->get('title'))) ?>
         <?php echo $this->formElementErrors($form->get($form->get('title')), array('class' => 'errors')) ?>
    </li>
    <li>
         <?php echo $this->formLabel($form->get($form->get('content')) ?>
         <?php echo $this->formElement($form->get($form->get('content'))) ?>
         <?php echo $this->formElementErrors($form->get($form->get('content')), array('class' => 'errors')) ?>
    </li>

</ul>

您还可以像这样对 HTML 进行更多控制:

<li>
   <?php echo $form->get('content')->getLabel() ?>
    <input class="some-class" type="text" 
       name="<?php echo $form->get('content')->getName() ?>"
       value="<?php echo $form->get('content')->getValue() ?>"
    />
</li>

如果您想获取所有表单错误:

<?php foreach($form->getMessages() as $errors): ?>
    <?php var_dump($errors) // Array for messages for each input ?>
<?php endforeach ?>
于 2013-02-14T09:20:44.790 回答