根据选择的单选按钮验证模型

时间:2013-06-18 07:04:27

标签: yii yii-extensions

我已经安装了yii-user扩展,并在tbl_profile表中添加了一些列进行注册。注册类型有两种类型:个人和公司

以下是添加的列:

对于公司:company_name,comoany_type

个人:电话

适用于个人和公司:移动,全名,国家,州,邮政编码,地址1,地址2

我根据注册类型的单选按钮选择使用jquery来隐藏和禁用表单的输入字段。

同样适用于两种注册类型的国家选择。两个选项:例如美国和其他国家

我很困惑,如何根据所选的注册类型验证属性。例如如果我选择个人,则禁用公司属性字段的验证。

有两种型号的属性:

个人资料:fullname,company_name,company_type,mobile,phone,firstaddress,secondaddress,country,states,postal_code

RegistrationForm :用户名,密码,电子邮件

我还在相应的模型上定义了这些属性的规则。

我已经尝试过验证这样的模型但不起作用:

if(isset($_POST['RegistrationForm'])) {
    if($_POST['Profile']['account_type'] == 'personal')
    {
        //for personal account
        $profile->account_type = $_POST['Profile']['account_type'];
        $model->username = $_POST['RegistrationForm']['username'];
        $model->password = $_POST['RegistrationForm']['password'];
        $model->verifyPassword = $_POST['RegistrationForm']['verifyPassword'];
        $model->email = $_POST['RegistrationForm']['email'];
        $model->verifyCode = $_POST['RegistrationForm']['verifyCode'];
        $model->accept = $_POST['RegistrationForm']['accept'];
        $profile->fullname = $_POST['Profile']['fullname'];
        $profile->phone = $_POST['Profile']['phone'];
        $profile->ext = $_POST['Profile']['ext'];
        $profile->mobile = $_POST['Profile']['mobile'];
        if($_POST['choose_country'] == 'other')
        {
            $profile->country = $_POST['choose_country'];
            $profile->states = $_POST['profile_states'];
            $profile->postalcode = $_POST['Profile']['postalcode'];
            $profile->firstaddress = $_POST['Profile']['firstaddress'];
            $profile->secondaddress = $_POST['Profile']['secondaddress'];
        }
        if($_POST['choose_country'] == 'Nepal')
        {
            $profile->country = $_POST['choose_country'];
            $profile->firstaddress = $_POST['Profile']['firstaddress'];
            $profile->secondaddress = $_POST['Profile']['secondaddress'];
        }
    }
    if($_POST['Profile']['account_type'] == 'company')
    {
        //for organization account
        $profile->account_type = $_POST['Profile']['account_type'];
        $model->username = $_POST['RegistrationForm']['username'];
        $model->password = $_POST['RegistrationForm']['password'];
        $model->verifyPassword = $_POST['RegistrationForm']['verifyPassword'];
        $model->email = $_POST['RegistrationForm']['email'];
        $model->verifyCode = $_POST['RegistrationForm']['verifyCode'];
        $model->accept = $_POST['RegistrationForm']['accept'];
        $profile->fullname = $_POST['Profile']['fullname'];
        $profile->ext = $_POST['profile']['ext'];
        $profile->mobile = $_POST['Profile']['mobile'];
        $profile->company_name = $_POST['Profile']['company_name'];
        $profile->company_type = $_POST['Profile']['company_type'];
        $profile->designation = $_POST['Profile']['designation'];

        if($_POST['choose_country'] == 'Nepal')
        {
            $profile->country = $_POST['choose_country'];
            $profile->states = $_POST['Profile']['states'];
            $profile->postalcode = $_POST['Profile']['postalcode'];
            $profile->firstaddress = $_POST['profile']['firstaddress'];
            $profile->secondaddress = $_POST['profile']['secondaddress'];
        }
        if($_POST['choose_country'] == 'others')
        {
            $profile->country = $_POST['profile']['country'];
            $profile->firstaddress = $_POST['profile']['firstaddress'];
            $profile->secondaddress = $_POST['profile']['secondaddress'];
        }
    }

    //$model->attributes=$_POST['RegistrationForm'];
    //$profile->attributes=((isset($_POST['Profile'])?$_POST['Profile']:array()));

    if($model->validate()&&$profile->validate())
    {
    }
}

问题:

如果我选择个人单选按钮并提交表单,它仍会验证company_name,公司类型和国家/地区选择相同,然后显示验证错误。这里我想要的是根据个人或公司类型的单选按钮的选择禁用模型验证。

1 个答案:

答案 0 :(得分:2)

我从未使用yii-user扩展程序,但作为解决方案,我可以建议通过为$profile模型设置不同的scenarios来限制公司和个人验证,具体取决于$_POST['Profile']['account_type']在将值分配给$_POST的模型之前,例如:

if ($_POST['Profile']['account_type'] === "personal")
    $profile->scenario = "personal";
else
    $profile->scenario = "company";

rules()模型的Profile方法中,您为每个帐户类型相关字段指定相应的方案:

public function rules() {
    return array(
        // ...general rules
        array("company_name", "validateCompanyName", 'on' => array("company")),
        array("company_type", "validateCompanyType", 'on' => array("company")),
        array("phone", "validatePersonalPhone", 'on' => array("personal"))
    )
}

我相信这样就可以为这样的模型赋值:

$model->attributes = $_POST['RegistrationForm'];
$profile->attributes = $_POST['Profile'];