1

I'd like to know if the following php validation is properly coded. It works, but I'd like to know if there's a better way to do it.

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');
        }   
}

I need it like this because I have a hidden required field that is shown only to the customers who are applying for a corporate account. The field is shown after clicking a radio button. So I only need to validate that field [cnpj] when a customer clicks on the retailer account radio button with a value=2.

Thanks a lot.

Marvin M

6
  • 1
    What is the expected value of $this->request->post['cnpj']? int or string? Commented Apr 26, 2012 at 4:50
  • cnpj will be a string @LawrenceCherone Commented Apr 26, 2012 at 5:15
  • @Chandresh... and your comment is relevant to my question because... Commented Apr 26, 2012 at 5:18
  • .... because people won't feel inclined to help if you don't accept their answers. That's why. Commented Apr 26, 2012 at 5:31
  • 25% accept is unacceptable. People more likely to help if the poster gives them points. It's just a game to everyone. Commented Apr 26, 2012 at 6:00

1 Answer 1

1

You can do your code like below lines of code:

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

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

}else{

     return $this->error;

}

// function for form validation

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');
        }   

}

I think this would be a batter way to do all the form validation at server side. and also most of MVC structures follow this way.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.