如何在yii中将按钮id放入模型中


How to get the button id into model in yii?

我有两个按钮,即next和submit。当我点击下一个按钮时,一些验证将起作用。当点击提交按钮时,下一步和提交按钮验证都会起作用。我该怎么办?

public function rules()
{
    return array(
        array('place', 'required', 'message' => 'Please select Working At'),
        array('empno', 'required', 'message' => 'Please fill Employee No'),
     );
}

假设您有以下按钮:

<INPUT TYPE="submit" NAME="action" VALUE=" next">
<INPUT TYPE="submit" NAME="action" VALUE=" submit">

然后在你的控制器中,你可以进行

$model = new Model;
if(isset($_POST['action'])) {
    if($_POST['action'] == "next") {
        $model->scenario = "next"
    } else {
        $model->scenario = "submit"
    }
}
//Assign the attributes
$model->validate();//or save or anything you want to do

在您建模的规则中,您可以设置一些仅适用于提交场景(第一个示例)或除下一个场景(第二个示例)之外的所有场景的规则

public function rules()
{
    return array(
        array('place', 'required', 'message' => 'Please select Working At', 'on' => 'submit'),
        array('empno', 'required', 'message' => 'Please fill Employee No', 'except' => 'next'),
     );
}