if语句中的php验证


Php Validation inside if statement

我想知道以下php验证是否正确编码。它有效,但我想知道是否有更好的方法

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]。

多谢。

马文·

您可以像下面的代码行一样执行代码:

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 结构都遵循这种方式。