2012-04-26 99 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