2

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

以下是添加的列:

对于公司:company_name,comoany_type

个人:电话

适用于个人和公司:手机、全名、国家、州、邮政编码、地址 1、地址 2

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

两种注册类型的国家选择也是如此。两种选择:例如美国和其他国家

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

有两个模型及其属性:

个人资料:全名、公司名称、公司类型、手机、电话、第一地址、第二地址、国家、州、邮政编码

注册表格:用户名、密码、邮箱

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

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

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 、公司类型和相同的国家/地区选择,然后显示验证错误。在这里,我想要的是根据选择个人或公司类型的单选按钮来禁用模型验证。

4

1 回答 1

2

我从未使用过扩展,但作为一种解决方案,我可以建议通过为模型设置不同的场景yii-user来限制公司和个人验证,具体取决于在将值分配给模型之前,例如:$profile$_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'];
于 2013-06-18T09:13:26.113 回答