我已经安装了yii-user扩展并在tbl_profile表中添加了一些列用于注册。注册类型有两种类型:个人和公司验证模型取决于所选单选按钮
以下是列说:
对于公司:COMPANY_NAME,comoany_type
对于个人:手机
对于这两种个人和公司:手机,全名,国家,州,邮政编码,地址1,地址2
我已经使用jquery根据单选按钮选择注册类型来隐藏和禁用表单的输入字段。
这两种注册类型的国家选择也一样。两个选项:例如美国和其他国家
我很困惑,我如何根据所选的注册类型验证属性。例如如果我选择个人,则禁用公司财产字段的验证。
有两种型号,它们的属性:
档案:全名,COMPANY_NAME,COMPANY_TYPE,手机,电话,firstaddress,secondaddress,国家,州,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,公司类型并为国家选择相同,然后显示验证错误。在这里,我想要的是取消选择个人或公司类型的单选按钮禁用模型验证。
什么是validateCompanyName,validateCompanyType,ValidatePersonalPhone。我们需要定义这个规则?或者它是如何指定的 – CodeManiac
这些是您的'Profile'模型中声明的可能[自定义验证方法](http://www.yiiframework.com/wiki/168/create-your-own-validation-rule/)的名称。如果它们适合你,你可以使用[预定义的验证规则](http://www.yiiframework.com/wiki/56/)。 – Ezze
我不会推荐使用这个场景,因为你不能再使用'update'和'insert'场景,这在整个Yii中都会用到很多。只需编写一个基于registrationType属性进行验证的自定义验证函数即可。 http://www.yiiframework.com/wiki/168/create-your-own-validation-rule/请务必查看http://www.yiiframework.com/doc/guide/1.1/en/form.model #secured-attribute-assignments –