1

我想知道以下 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]。

非常感谢。

马文 M

4

1 回答 1

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

于 2012-04-26T05:10:22.817 回答