Form Validation and Presentation Functions - Exercise

Contact Us or call 1-877-932-8228
Form Validation and Presentation Functions - Exercise

Form Validation and Presentation Functions

Duration: 45 to 60 minutes.

In this exercise, you will complete several functions needed for the application we have just seen.

  1. Open ReusingCode/Exercises/Includes/fnFormPresentation.php in your editor.
  2. Complete the radioEntry() and selectEntry() functions so that the return value contains correct form entries and, if appropriate, errors.
  3. Open ReusingCode/Exercises/Includes/fnFormValidation.php in your editor.
  4. Complete checkEmail() and checkPassword(), so that they return true if the form entry is valid and false otherwise.
  5. To test your solution, open ReusingCode/Exercises/AddEmployee.php in your browser and submit the form several times entering different data combinations each time.

Solution:

ReusingCode/Solutions/Includes/fnFormPresentation.php
<?php
/********* FORM PRESENTATION FUNCTIONS *********/
---- C O D E   O M I T T E D ----
/*
	Function Name: radioEntry
	Arguments: $display,$name,$entries,$errors,$values
	Returns:
		table rows as string
*/
function radioEntry($display,$name,$entries,$errors,$values)
{
	$returnVal = "
	<tr>
		<td>$display:</td>
		<td>";
		foreach ($values as $value)
		{
			if (array_key_exists($name,$entries) &&
					$entries[$name]==$value)
			{
				$returnVal .= "<input type='radio' name='$name'
							value='$value' checked> $value ";
			}
			else
			{
				$returnVal .= "<input type='radio' name='$name'
							value='$value'> $value ";
			}
		}
	$returnVal .= "</td>
			</tr>";

	if (array_key_exists($name,$errors))
	{
		$returnVal .= addErrorRow($name,$errors);
	}
	return $returnVal;
}

/*
	Function Name: selectEntry
	Arguments: $display,$name,$entries,$errors,$selected?
	Returns:
		table rows as string
*/
function selectEntry($display,$name,$options,$errors,$selected=0)
{
	$returnVal = "<tr>
		<td>$display:</td>
		<td>
			<select name='$name'>
			<option value='0'>Choose one...</option>";
			foreach ($options as $key=>$option)
			{
				if ($key == $selected)
				{
					$returnVal .= "<option value='$key' selected>
								$option</option>";
				}
				else
				{
					$returnVal .= "<option value='$key'>
								$option</option>";
				}
			}
			$returnVal .= "</select>
		</td>
		</tr>";

		if (array_key_exists($name,$errors))
		{
			$returnVal .= addErrorRow($name,$errors);
		}
	return $returnVal;
}
---- C O D E   O M I T T E D ----

?>

Solution:

ReusingCode/Solutions/Includes/fnFormValidation.php
<?php
/********* FORM VALIDATION FUNCTIONS *********/
---- C O D E   O M I T T E D ----
/*
	Function Name: checkEmail
	Arguments: $email
	Returns:
		false if $email has fewer than 6 characters
		false if $email does not contain @ symbol
		false if $email does not contain a period (.)
		false if the last @ symbol comes after the last period (.)
		true otherwise
*/
function checkEmail($email)
{
	$email = trim($email);
	if (!checkLength($email,6))
	{
		return false;
	}
	elseif (!strpos($email,'@'))
	{
		return false;
	}
	elseif (!strpos($email,'.'))
	{
		return false;
	}
	elseif (strrpos($email,'.') < strrpos($email,'@'))
	{
		return false;
	}
	return true;
}

/*
	Function Name: checkPassword
	Arguments: $pw1,$pw2
	Returns:
		false if $pw1 has fewer than 6 characters
		false if $pw1 has more than 12 characters
		false if $pw1 and $pw2 do not match
		true otherwise
*/
function checkPassword($pw1,$pw2)
{
	$pw1 = trim($pw1);
	$pw2 = trim($pw2);
	return checkLength($pw1,6,12) && strcmp($pw1,$pw2) == 0;
}
?>
Next