Here is my code:
class FormValidator {
private $firstname;
private $lastname;
private $email;
private $fields_with_lengths = array('first' => 2, 'last' => 2);
private $color = '<span style="color:#FF0000">';
function checkFirst($firstname) {
$first_error = NULL;
if(strlen(trim($firstname)) < $this->fields_with_lengths['first']){
$first_error = $this->color . 'Please enter more than ' . $this->fields_with_lengths['first'] . ' characters.</span>';
}
return $first_error;
}
function checkLast($lastname) {
$last_error = NULL;
if(strlen(trim($lastname)) < $this->fields_with_lengths['last']){
$last_error = $this->color . 'Please enter more than ' . $this->fields_with_lengths['last'] . ' characters.</span>';
}
return $last_error;
}
function validateEmail($email){
return preg_match('/^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/', $email);
}
}
Here is where I call it:
$validator = new FormValidator();
$firstResult = $validator->checkFirst($_POST['firstname']);
$lastResult = $validator->checkLast($_POST['lastname']);
$emailResult = $validator->validateEmail($_POST['emailaddress1']);
if (is_null($firstResult) && is_null($lastResult) && $emailResult) {
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->AddAddress("s...@....com");
$mail->Subject = "test";
$mail->MsgHTML($messageHTML);
redirectULS('english/forms/thankyou.php');
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
//$bridge->pushLead($lead);
}
}
This only works if I remove $emailResult from the if statement. What am I doing wrong. I’ll take into account the earlier comments about functions that only return true or false later. Right now I need to fix this if statement. Thanks. (Some variables are set elsewhere. All I want to know here is how to get the if statement to work.)
1 Answer