0

我已经定义了一个使用Zend\Form. 在那里,有一个单选按钮。

    $this->add(array(
        'name' => 'nationality_radio',
        'type' => 'Radio',
        'options' => array(
            'value_options' => array(
                'local' => 'Local',
                'expatriate' => 'Expatriate',
            ),
        )
    ));

它的值不直接与数据库列绑定。但它应该填充从 DB 中获取的正确值并保存用户输入。(例如 - 如果表列的值nationalitylocal它应该local在单选按钮中选择)

渲染时应显示为..

在此处输入图像描述

在表单加载时,它将选择考虑 column 的选项nationality。它将包含值localexpatriate

        <?php
        if ($candidate->nationality == 'local'){
            $local = 'checked';
        } else if ($candidate->nationality == 'local'){
            $expatriate = 'checked';
        }
        ?>

在纯 HTML 中,我可以按如下方式进行,

    <div class="profile_item list-group-item">
        <span class="item_title">Local: </span>
        <span class="item_content"><input type="radio" name="nationality" value="local" <?php echo $local ?>></span><br>
        <span class="item_title">Expatriate: </span>
        <span class="item_content"><input type="radio" name="nationality" value="expatriate" <?php echo $expatriate ?>></span>
    </div>

但是由于 Zend 表单使用 <?php echo $this->formRow($form->get('nationality_radio')); ?>我无法做到。它只是显示如下。

在此处输入图像描述

我怎样才能达到我的要求?

4

2 回答 2

1

对于设置值,在控制器方法中:

$form = new YourFormClass();
//are you using fieldsets? let's say no...
//if accepted value for $candidate->nationality could be only 'local' and 'expatriate',
//check the scope with your defined value_options or use an if
$form->get('nationality_radio')->setValue($candidate->nationality);

在您的视图中使用formRadio助手:

<?php echo $this->formRadio($form->get('nationality_radio'),\Zend\Form\View\Helper\FormRadio::LABEL_PREPEND);?>

查看文档: http: //framework.zend.com/manual/2.2/en/modules/zend.form.elements.html#radio

或检查课程:

\Zend\Form\View\Helper\FormRadio

\Zend\Form\View\Helper\FormMultiCheckbox

接受的值为$labelPosition

const LABEL_APPEND  = 'append';
const LABEL_PREPEND = 'prepend';
于 2013-09-05T09:49:10.643 回答
0

Try this

 <?php
            if ($candidate->nationality == 'local'){
                $LocalChecked="checked";
            } 
           else {
                $expatriateChecked ="checked";
            }
     ?> 



<div class="profile_item list-group-item">
        <span class="item_title">Local: </span>
        <span class="item_content"><input type="radio" name="nationality" value="local" <?php echo $LocalChecked ?>></span><br>
        <span class="item_title">Expatriate: </span>
        <span class="item_content"><input type="radio" name="nationality" value="expatriate" <?php echo $expatriateChecked ?>></span>
    </div>
于 2013-09-05T06:58:04.797 回答