2012-04-26 98 views
1

我想知道,如果下面的PHP驗證正確編碼。它的工作原理,但我想知道是否有更好的方法來做到這一點。腓驗證內部if語句

if(isset($_POST['account_type']) && $_POST['account_type']==2){ 

if ((strlen(utf8_decode($this->request->post['cnpj'])) < 3) || (strlen(utf8_decode($this->request->post['cnpj'])) > 32)) { 
      $this->error['cnpj'] = $this->language->get('error_cnpj'); 
     } 
} 

我需要它喜歡這樣,因爲我有一個只會向誰申請的是企業帳戶的客戶隱藏必填字段。該字段在單擊單選按鈕後顯示。因此,當客戶點擊值爲2的零售商帳戶單選按鈕時,我只需驗證該字段[cnpj]。

非常感謝。

馬文中號

+1

什麼是'$這個 - >請求 - >後[ 'CNPJ']'的期望值?整數或字符串? – 2012-04-26 04:50:21

+0

CNPJ將是一個字符串@LawrenceCherone – codekmarv 2012-04-26 05:15:20

+0

@Chandresh ......和您的評論是有關我的,因爲... – codekmarv 2012-04-26 05:18:13

回答

1

你可以做你的代碼像下面的代碼行:

if($_POST['account_type']==2 && $this->formValidation()){ 

     // Do your next process what you want to do if all set 

}else{ 

    return $this->error; 

} 

//表單驗證功能

function formValidation(){ 

if ((strlen(utf8_decode($this->request->post['cnpj'])) < 3) || (strlen(utf8_decode($this->request->post['cnpj'])) > 32)) { 
      $this->error['cnpj'] = $this->language->get('error_cnpj'); 
     } 

} 

我認爲這將是一個連擊方式在服務器端進行所有表單驗證。 以及大多數MVC結構都遵循這種方式。

+0

感謝您的回覆,請關注一下。 – codekmarv 2012-04-26 05:16:30