带有文本选项的单选按钮列表 蛋糕

radio button list with text option cakephp

本文关键字:单选按钮 列表 蛋糕 选项 文本      更新时间:2023-09-26

我是CakePHP的新手。我需要制作一个带有单选按钮的表单,最后一个是"其他",用户可以在其中键入文本框中的答案。

有什么方法可以做到这一点 FormHelper 内置的吗?

我要做的一种方法是创建一个单选列表和一个文本字段。当选择"其他"时,Javascript将显示文本字段。为此,我不明白如何使用数据库中字段以外的任何其他变量。如何从可以从视图访问的模型创建变量,并返回值进行处理?

对于我拥有的模型:

class User extends AppModel {
    /**
     * Display field
     *
     * @var string
     */
    public $displayField = 'title';
    var $sourceOther = ' ';
    var $passwordRepeat = ' ';

    public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
            $this->data[$this->alias]['password'] = sha1(
                    $this->data[$this->alias]['password']);
        }
//        $this->data[$this->alias]['created']= CakeTime::gmt();
//        $this->data[$this->alias]['updated']= CakeTime::gmt();
        $this->data[$this->alias]['username']= $this->data[$this->alias]['email'];
        return true;

在我的观点中

echo $this->Form->input('mobilePhone',array(
    'label'=>'Mobile or fixed phone with no spaces'));
echo $this->Form->input('alternatePhone');
echo $this->Form->input('leadSource', array(
    'options'=>array('Google'=>'Google','OnlineAd'=>'Online Ad',
            'Printed Media'=>'Printed Media','LetterDrop'=>'Letter Drop',
            'Other'=>'Other (specify text)'),
    'empty'=>'(choose one)')); 
echo $this->Form->input($this->sourceOther);

。但它不喜欢模型中的变量 sourceOther。如何将数据从视图(文本框)获取到用户模型中,以便在 Save 之前可以使用它执行某些操作?

谢谢。

谢谢。

阅读"蛋糕PHP应用程序开发"后整理出来。它适用于Cake 2.x和1.2(如书中使用)。

在您看来:

echo $this->Form->input('sourceOther');

然后在模型中,您可以使用以下命令访问变量:

$this->data['User']['sourceOther'];

例如,使用它在 beforesave 中保存"其他"文本字段:

public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = sha1(
                $this->data[$this->alias]['password']);
    }
    $this->data[$this->alias]['username'] = $this->data[$this->alias]['email'];
    $this->data[$this->alias]['activation'] = md5(uniqid(rand(), true));
    if ($this->data[$this->alias]['leadSource'] == 'Other') {
        $this->data[$this->alias]['leadSource'] =
                $this->data['User']['sourceOther'];
    }
    return true;
}