1

I want to get better on not nesting if-satments so much and got some help from my brother about how to think. So the thougt here is to create a function that validates that all fields in an form is filled in and with allowed caracters.

But I'm an expert on nested if:s and I need some help to learn a better way of doing it. My question is how I can do the same check in the function instead? What can I use that is good practise? Can I do it with an array?

My nested if statment is here (I'm supposed to do this to a much bigger form later on):

// If the submit button has been clicked
if(isset($_POST["login"])){

    // Checks if username-field is empty and if allowed characters are used
    if(empty($_POST['user']) or !preg_match("/^[a-zA-Z0-9 ]*$/", $_POST['user'])){
        $error = "You can't leave a field empty and you can only use letters, numbers and space.";
    }else{
        // Checks if password-field is empty and if allowed characters are used
        if(empty($_POST['pass']) or !preg_match("/^[a-zA-Z0-9 ]*$/", $_POST['pass'])){
            $error = "You can't leave a field empty and you can only use letters, numbers and space.";
        }else{
            // Code here
        }
    }
}

And this is the base for what I want to try to do instead:

function validate(){
    // Code here
}

// If the submit button has been clicked
if(isset($_POST["save"])){  

    if(validate(/*vaibles for each of the fields in the form*/)){

    }else{

    }
}
8
  • put your if else condition in your function and pass $_POST array to that function as parameter. Commented Jun 3, 2015 at 9:47
  • I'm sorry, can you maybe explain that a bit more? What is the $_OST varible/array? Maybe show me an example would be nice. Commented Jun 3, 2015 at 9:51
  • You don't need to nest all individual validations, you can put them all on the same level and build an array with error messages instead. And note that not all fields will use the same rules so one function for all is probably not going to work. Commented Jun 3, 2015 at 9:52
  • Ah ok, got a bit more what you meant now Anant! Commented Jun 3, 2015 at 9:55
  • Im gonna build an array with errormessages, but this is the part I need more help with. I know I cant do the validate function for all the fields, but most fields are similar so for them I can. But when you mean on the same level, you think I should do all the if-statments after ech other in some way? Because that Im not sure how to actually check if them all are empty. Commented Jun 3, 2015 at 9:57

1 Answer 1

1

As you asked for an example, check this:-

<?php
if(isset($_POST["login"])){

   $error = validate($_POST);

    if(empty($error) {

    .....//your further processing code
    }else{
     // code to show your error at appropriate place.
    }
}
function validate($dataArray){
    $validateArray  = $dataArray; // either asign to a new variable or you can use directly $dataArray
    $errors = array();
    if(empty($_POST['user']) or !preg_match("/^[a-zA-Z0-9 ]*$/", $_POST['user'])){
        $errors['user'] = "You can't leave a field empty and you can only use letters, numbers and space.";
    }
    if(empty($_POST['pass']) or !preg_match("/^[a-zA-Z0-9 ]*$/", $_POST['pass'])){
            $errors['pass'] = "You can't leave a field empty and you can only use letters, numbers and space.";
     }
     //......so on

    return $errors;
}
?>

Note:- this is an example that how can you proceed. thanks.

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

8 Comments

That seems really good, got the code :). The only part that I'm not sure of is the: $validateArray = $dataArray; You dont seem to use the $validateArray anyway so why put the $dataArray in to that varible? Just so I get why you do so :)
Please check my edited answer and if it seems useful to you you can mark it as an answer and up-vote it if possible. it will help others .thanks.
I still dont get where you acctually use the $dataArray? either if its in the new varible or not? I will try this out and mark the answer later on when I know it works for me of course :)
$dataArray is to to tell that function will take one parameter . it will hold directly your $_POST values when you call function validate($_POST)
That part I get, I ment more in the code in the function. But i think I'm just thinking a bit crazy ^^. I kind of relized now that you dont really need to use it in any other way. Just forget my question ;). I will take a brake now for some lunch but I'll try this soon and mark the question if it works (wich I think it will, looks that way)!
|

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.