Checking for keys in more than one array (form processing)

I recently added the following PHP to my contact form, to add an error class to affected inputs when the form is reloaded with error messages.

<?php if (array_key_exists('required', $errors) || array_key_exists('email', $errors)) { echo ' class="error" '; } ?>

Obviously, that snippet adds an ‘error’ class if the email is missing or wrong by checking my $errors array. Error messages appear above the form and the applicable fields are highlighted. This all works fine; but all required fields are going to be highlighted by this method, even if only one of them is empty. Any one or more missing required fields generates the ‘required’ key in $errors.

I only want the ‘error’ class to be echoed into the HTML if the ‘required’ key exists AND my example field of ‘email’ really is empty. I have an array called $inputs to populate the form fields on errors, which includes a key of ‘email’. So i figured that I could check for this key and I tried the following edit:

<?php if (!array_key_exists('email', $inputs) && array_key_exists('required', $errors) || array_key_exists('email', $errors)) { echo ' class="error" '; } ?>

Nada. The field isn’t highlighted, even though it is empty. Which is probably for a completely obvious reason, but I just don’t know why.

try this:

<?php if ((!array_key_exists('email', $inputs) && array_key_exists('required', $errors)) || array_key_exists('email', $errors) )) { echo ' class="error" ';  } ?>

This says:

IF “required error exists and no value for email” OR “other email error exists”

PS - A better solution would have a single object for each input that handles the requirements for that input, the posted data, the validation method, and the rendering method for it. Then wrap all your inputs into a collection for simpler processing.

For example, I use a framework that allows me to create forms and check them simply, like this:

$form = new Form("admin_mydoc_create", array(
	"method" => HttpRequestMethod::POST, 
	"validation" => ValidationType::BROWSER,
	"children" => array(
		new Fieldset("user", array(
			"legend" => "User Account Information",
			"children" => array(
				new DateInput("first_name", array("label" => "First Name", "required" => true)),
				new TextInput("last_name", array("label" => "Last Name", "required" => true)),
				new TextInput("email", array("label" => "Email", "required" => true, "format" => "Email")),
				new TextInput("password", array("label" => "Password", "required" => true, "value" => rand(11111111,99999999), "minlength" => 8)),
				new CheckboxInput("terms_accepted", array("label" => "Terms accepted by fax?", "required" => false ))
		new Button("submit", array("type" => ButtonType::SUBMIT, "value" => "Submit"))

if ($form->posted()) {
	if ($form->validate()) {
} else {

Thanks for replying.

I’ve tried adding the extra brackets that you suggested, but it hasn’t made any difference. Maybe there is something wrong elsewhere.

Your second post makes almost no sense to me! Sorry.

print_r your $errors and $inputs and post it here so I know what you’re dealing with in terms of data.

Not sure what you mean. Still new at this.



Ok. For the inputs, I get this when the page loads:

Array ( [name] => [phone] => [email] => [domain] => [message] => )

If I miss a few required fields except e-mail, then submit it, I get this:

Array ( [name] => [phone] => [email] => [domain] => [message] => )

Array ( [required] => Check that you have included a message, your name, and your e-mail address )

How do I check that the part after the right chevron is not empty? That seems to be the key to this. e.g. “if [email] => has something in it, don’t echo the new class value”.