【问题标题】:$form->input to create a radio button using CakePHP$form->input 使用 CakePHP 创建一个单选按钮
【发布时间】:2013-12-23 13:08:30
【问题描述】:

我正在为我的应用程序使用 jQuery 和 CakePHP。

在我的应用程序中使用我保存在数据库中的数据,例如,如果字段类型列是文本,那么我将使用 $form->input(); 在我的代码中生成一个文本框

如果它是一个下拉框,我将使用以下方法生成它:

echo $form->input($r['Attribute']['label'],
    array('id'=>$r['Attribute']['id'],'name'=>$r['Attribute']['label'],
        'options' => array(1,2,3,4,5))); 

现在我有一个“单选按钮”类型的字段。我正在尝试在 CakePHP 中创建单选框。 有没有可能……如果可以怎么办?

【问题讨论】:

    标签: cakephp


    【解决方案1】:

    使用FormHelper::input(),您可以通过设置type 选项来指定所需的字段类型:

    echo $form->input($r['Attribute']['label'], array(
        'type' => 'radio',
        'id' => $r['Attribute']['id'],
        'name' => $r['Attribute']['label'],
        'options' => array(1, 2, 3, 4, 5),
    ));
    

    与直接调用FormHelper::radio()不同,会呈现输入的标签和验证错误。

    【讨论】:

    • 还有一个布尔收音机?其中输入数据库值是布尔类型,但蛋糕(或 PDO)显示“f”和“t”?我无法获得布尔值来设置正确的单选选项(在 edit.cpt 上)。
    【解决方案2】:

    例如:

    $options=array('M'=>'Male','F'=>'Female');
    $attributes=array('legend'=>false);
    echo $this->Form->radio('gender',$options,$attributes);
    

    试试你的属性

    【讨论】:

    • 还有一个布尔收音机?其中输入数据库值是布尔类型,但蛋糕(或 PDO)显示“f”和“t”?我无法获得布尔值来设置正确的单选选项(在 edit.cpt 上)。
    【解决方案3】:
        echo $form->input($r['Attribute']['label'],
        array('id'=>$r['Attribute']['id'],'name'=>$r['Attribute']['label'],
             'type'=>'radio',
             'options' => array(1=>'male',2=>'female',3=>'Others')
        ));
    

    【讨论】:

      猜你喜欢
      • 2016-12-31
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-28
      • 2017-09-20
      • 2010-11-02
      相关资源
      最近更新 更多