【问题标题】:Model attributes from dropdown list not transferring下拉列表中的模型属性未传输
【发布时间】:2013-03-17 08:17:49
【问题描述】:

我有一个resume 表,其中有一个position_type_id 列,它是position_type 表的外键。

resume 创建表单上,position type 下拉列表的选项从position_type 表中正确拉出,并使用它们的 ID 作为选择的值进行设置。一切似乎都很顺利。

提交表单后,我可以验证值是否正在返回到 POST 变量中的控制器,但是它们没有通过属性填充传递到模型中:

$model->attributes=$_POST['Resume'];

这是控制器上的create 方法:

public function actionCreate()
{
    $model=new Resume;

    if(isset($_POST['Resume']))
    {
        $model->attributes=$_POST['Resume'];

        if($model->save())
        {
            $this->redirect(array('view','id'=>$model->id));
        }   
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}

这是从相关模型加载键的表单:

echo $form->activeDropDownList(
    $model,
    'position_type_id',
    CHtml::listData(PositionType::model()->findAll(),'id','name')
);

position_type_id 是简历模型上的键,它将外键传送到 PositionType 模型。它在表格和简历模型上的拼写相同。模型上的关系是:'positionType' => array(self::BELONGS_TO, 'PositionType', 'position_type_id'),

我想我可以从 POST 数组中获取每个值并手动设置它们,但看起来这应该“正常工作”。设置属性后$model 上的值用于手动输入的字段,而对于来自生成的下拉列表的所有字段,则为空白。

以下是实际生成的内容:

<select name="Resume[position_type_id]" id="Resume_position_type_id">
    <option value="1">English Teacher</option>
    <option value="2">School Administrator</option>
</select>

【问题讨论】:

  • 表格是否通过POST 发布? position_type_id 是否在模型的 rules() 函数中设置为安全的质量分配?

标签: php forms drop-down-menu model yii


【解决方案1】:

我可能是错的,但对我来说,大规模的任务不起作用。在 yii 中,如果我们需要对其执行大量赋值,则需要将属性声明为安全的。

所以在您的模型Resume 中,您是否有以下规则:

public function rules() {
    return array(
        //the other rules ...
        array('position_type_id, others_attributes', 'safe'),
        //the rule above indicate the attributes that can be massively assigned
    );
}

A link to the wiki about the 'safe' validation rule

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-05
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多