我已經安裝了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 –