Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The staff score is an array value. The form still submitted even the text field is not filled or 0.

What I have tried:

$data['staff_score'] = array();

switch ($_POST['mode']) {		
				
		case 'submit':
			
			$ValidProcess = true;
	
			//this method is for 'staff_score'//NOT VALID
			if (count($_POST['staff_score'])> 0){
				$data ['staff_score'] = $_POST['staff_score'];
			}
			
			$isValid = false;
			foreach ($data['staff_score'] as $key => $value){
				if ($value > 0){
					$isValid = true;
					break;
				}
			}
			if (!$isValid){
				$ValidProcess = false;
				$errorMsg[]= 'Please Insert Your Score.';
			}
			
					
			//database
			if($ValidProcess) {
				$ValidProcess = true;
				
			} 
     break;
}
Posted
Updated 6-Jun-22 0:03am
v2
Comments
Member 15627495 6-Jun-22 18:16pm    
hello Zatil Syammi !

error handling is hard times.

Choose the 'golden path' :
- write the code needed when your input is compliant and perfect.
and as you know failure case ( score = 0 , or not fill ) , add 2 or three line for handling those errors.

It's like 'testing' your code, but the benchmark is made firstly with perfect datas for the needs.

at the step after , add few errors in the input array. then add handling errors lines.


It's like dividing a functions by two smaller functions,
send firstly a compliant array , add all the score within a loop. return the score sum.

then introduce error in a new array, and handles the error the ways needed.

1 solution

I suspect you are checking the wrong way. In your check of the scores array, if any one value is greater than zero you set valid to True, and do not check the remaining entries. A better method would probably be:
PHP
$isValid = true;
foreach ($data['staff_score'] as $key => $value){
    if ($value == 0){
        $isValid = false; // no further checks needed if any value is zero
        break;
    }
}

You also have a redundant piece of code at:
PHP
if($ValidProcess) {
    $ValidProcess = true; // if it is already true then this line is not needed

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900