Problem with value returned in list()

In my script, I am using a ForEach to loop through all values in my $_POST array, and validate the data that was submitted.

Inside the loop, I have this line of code…


	// Call Validation Function.
	list($responseOK, $errors[$articleSurveyQuestionID]) = validateSurveyResponse($dbc, $articleSurveyQuestionID, $surveyResponse);


	echo "<p>\\$responseOK = $responseOK, <br />\\$errors[$articleSurveyQuestionID] = $errors[$articleSurveyQuestionID]</p>";

Following this loop, I have…


	if (empty($errors)){
		// Valid Form Data.
		echo "<p>NO ERRORS!!</p>";
	}else{
		echo "<p>ERRORS!!</p>";
	}

And in the Function being called, I have this code sub-section…


	case 'OPEN':
		if (empty($surveyResponse)){
			// Response does Not Exist.
			return array(91, NULL);

		}else{
			// Response Exists.
			// more stuff here...
		}

	break;

I know the first branch in my Case is firing, and I would expect that there is NO ENTRY in my $errors array, but on my screen I am seeing this…

$responseOK = 91,
$errors[3] =

$responseOK = 91,
$errors[7] =

$responseOK = 91,
$errors[8] =

ERRORS!!

Each of those values represents a TextArea that was left blank when the Survey was submitted.

I do NOT want anything written into my $errors array if there is an entry that is either 1.) a NULL, or 2.) Less than 1,024 Characters, because either is a valid response. However, if an entry over 1,024 characters, then it is too big, and it should throw an error so I can tell the User.

What am I doing wrong??

Thanks,

Debbie

Looks like it’s this line:


list($responseOK, $errors[$articleSurveyQuestionID]) = validateSurveyResponse($dbc, $articleSurveyQuestionID, $surveyResponse);

No matter what, you’re setting a key in the $errors array. It doesn’t matter if the value for that key is NULL, an empty string, or anything else. If there is a key, empty($errors) will return false.

So how do I redesign things to get what I want???

Normally - when I have hard-coded Forms - I would use something like this…


	// ************************
	// Validate Form Data.	*
	// ************************

	// Validate First Name. (Required)
	$firstName = $trimmed['firstName'];

	if (empty($firstName)){
		// First Name does Not Exist.
		$errors['firstName'] = 'Enter your First Name.';

	}else{
		// First Name Exists.
		if (preg_match('#^[A-Z \\'.-]{2,30}$#i', $firstName)){
			// Valid First Name.
			// Continue processing...

		}else{
			// Invalid First Name.
			$errors['firstName'] = 'First Name must be 2-30 characters (A-Z \\' . -)';
		}
	}//End of VALIDATE FIRST NAME

And then after my validation block, I would have…


	// ******************************
	// Attempt to Change Details.		*
	// ******************************
	if (empty($errors)){
		// Valid Form Data.


	}else{
		// Invalid Form Data.
		// Drop through to display Errors.

	}//End of ATTEMPT TO CHANGE DETAILS

When I run my validateSurveyResponse() function, I need a way to keep track of which Question (i.e. $articleSurveyQuestionID) has a validation-error, and the actual Error-Message itself.

So - in greater detail - my Function has code like this…


	// OPEN-ENDED QUESTION.
	case 'OPEN':
		if (empty($surveyResponse)){
			// Response does Not Exist.
			return array(91, NULL);

		}else{
			// Response Exists.
			$responseLength = strlen($surveyResponse);

			if ($responseLength <= 10){
				// Valid Response.
				return array(92, NULL);

			}else{
				// Invalid Response.
				$errors[$articleSurveyQuestionID] = "Response cannot exceed 1,024 characters. ($articleSurveyQuestionID)";
				return array(93, $errors[$articleSurveyQuestionID]);
			}
		}

		break;

(The 91, 92, and 93 where actually “TRUE”, but I switched to numbers to track where my code was firing…)

Hope that makes sense?! (:

Thanks,

Debbie

kduv is right. The problem is you’re setting something in $errors regardless of the result.

list($responseOK, $errors[$articleSurveyQuestionID]) = validateSurveyResponse($dbc, $articleSurveyQuestionID, $surveyResponse); 

One work around would be to set a temp variable there then check the result and only set $errors[$articleSurveyQuestionID] if there’s something to set.

I’m not following your suggestion and am still stuck. :frowning:

Here is some updated code…


	// OPEN-ENDED QUESTION.
	case 'OPEN':
		if (empty($surveyResponse)){
			// Response does Not Exist.

		}else{
			// Response Exists.
			$responseLength = strlen($surveyResponse);

			if ($responseLength <= 10){
				// Valid Response.
				// Continue processing...

			}else{
				// Invalid Response.
				$errors[$articleSurveyQuestionID] = "Response cannot exceed 1,024 characters. ($articleSurveyQuestionID)";
				return array($errors[$articleSurveyQuestionID]);
			}
		}

		break;

In the code above, I only assign an Error Message to my $errors array if there is an error. So good, so far, right?!

Then in my calling script, I have…


	// Call Validation Function.
	list($errors[$articleSurveyQuestionID]) = validateSurveyResponse($dbc, $articleSurveyQuestionID, $surveyResponse);

I think this is where the problem is…

My first set of code doesn’t return anything (i.e. an Error Message) if the Form Data is okay, but it seems like list($errors[$articleSurveyQuestionID]) is creating an array entry just by being called?! :-/

All I want to do is return an Error Message if there is an error and then assign it to an array so I will later know which Error Message goes with which Question when I dynamically re-generate my Survey Form…

If there aren’t any Errors, then I want to know it so I can INSERT the Survey Responses into my database. But if there are any returned Error Messages from my function, then my script should determine that successfully, and re-generate the Survey Form with the original Questions + Responses + Error Messages.

Debbie

Why use list() at all? Your validation function can just return an associative array. Feed it the $_POST variable, and return whatever you need in an associative array. You can include an errors key with it, and if there are any errors in that key, you can do whatever you want with it.

Something along the lines of:


// Call Validation Function. 
$result = validateSurveyResponse($dbc, $articleSurveyQuestionID, $surveyResponse);
if (count($result['errors'])) {
    // Validation failed ... do whatever you want
}

I decided it would be more practical to return the Error Message in a single variable, AND THEN assign it to an array like this…


	// Retrieve Form Data.
	foreach($_POST['responseToQuestion'] as $articleSurveyQuestionID => $surveyResponse){
		// Clean Form Data.
		$articleSurveyQuestionID = (int)$articleSurveyQuestionID;

		$surveyResponseArray[$articleSurveyQuestionID] = trim($surveyResponse);


		// ************************
		// Validate Form Data.	*
		// ************************

		// Call Validation Function.
		$errorMsg = validateSurveyResponse($dbc, $articleSurveyQuestionID, $surveyResponse);

		if ($errorMsg){
			$errors[$articleSurveyQuestionID] = $errorMsg;
		}

Debbie