0

I would like to check many rows of the same 2 input fields in a form. The validation should fail if one is empty and the other is not.

I have created an associative array based on several input fields(e_me_id, e_md_number, e_md_id1,e_md_number1, p_me_id,p_md_number...) in a form.

$pattern='(md_number|me_id)';
foreach($_POST as $field => $value) {
    $success = preg_match($pattern, $field);
    if ($success) {

        $validate += [$field => $value];
        }   
}

 result of validate =(
[e_me_id] => 1 
[e_md_number] => 111 
[e_me_id2] => 2 
[e_md_number2] => 222 
[p_me_id] => 10 
[p_md_number] => 101010 
[f_me_id] => 16 
[f_md_number] => 161616 
[d_me_id] => 18 
[d_md_number] => 181818 )

I need some looping php to check that the first/second are both null or both filled... same for third/forth, fifth/sixth... etc etc.

I tried to use prev($validate) and next($validate) but could not get it to work.

Any ideas or a different approach.

Thanks in advance.

1
  • You can use the xor operator to compare. Commented Jun 5, 2020 at 16:13

1 Answer 1

2

You can do this to do the validation of pairs:

// assuming this is the array generated by your code ...
$tst= array("e_me_id" => 1,"e_md_number" => 111 ,
            "e_me_id2" => 2,"e_md_number2" => 222 ,
            "p_me_id" => 0,"p_md_number" => 101010 ,
            "f_me_id" => 16,"f_md_number" => 161616 ,
            "d_me_id" => 0,"d_md_number" => 0 );
// then this will do the validation of pairs:
$keys=array_keys($tst);
for ($i=0;$i<count($tst);$i+=2) 
  echo "$keys[$i] and "
    .$keys[$i+1]
    .(empty($tst[$keys[$i]]) == empty($tst[$keys[$i+1]])?'':' DO NOT')
    ." pass the validation.\n";

You can see a demo here: https://rextester.com/VEMUFJ14979
I changed a few of the numbers to demonstrate the different possible cases.

Using the == operator between the two empty()-tests is equivalent to using the negated xor operator as suggested by @Markus Zeller.

Edit:

A "shorthand-if statement" should look something like this:

$x = (empty($tst[$keys[$i]]) == empty($tst[$keys[$i+1]])?'':$fieldname);

Although to me it is not quite clear what you intend $x and $fieldname to be in your for-loop. This would set $x to '' if case both tested values were empty or both were "filled" and it would set it to $fieldname when only one of them was empty.

Your most recent comment led me to believe that you want to have an overall validation result in $x. In order to get this you will have to count all errors while looping over the pairs. Something like the following should do that:

for ($i=0,$x=0;$i<count($tst);$i+=2){
  if (empty($tst[$keys[$i]]) != empty($tst[$keys[$i+1]])) $x++;
  // echo $keys[$i].", error count so far: $x\n";
}

echo ("here ".($x?'validation error!':'OK'));

See the demo here: https://rextester.com/XTK88037

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

8 Comments

Your code worked perfectly... I am still getting my head round it.. big thanks.
Can I use the shorthand if statement to set a variable rather than echo a message, for example (empty($tst[$keys[$i]]) == empty($tst[$keys[$i+1]])?$x = '':$x=fieldname)
Yes you can @osieman (Sometimes it's faster to just test if it works :-))
I tried but it always chooses true. In this case it should have output '1'. rextester.com/DZIMG36085
@osieman, the $x in your snippet only returns the result of the last performed test. Please have a look at my modified answer (at the bottom).
|

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.