Skip to content

Commit

Permalink
add support for email validation
Browse files Browse the repository at this point in the history
  • Loading branch information
cburyta committed May 5, 2012
1 parent 4a4b6c0 commit 61b46df
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
23 changes: 20 additions & 3 deletions lib/PHPContactor.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ protected function validateField($name, $value, $rules = false)
{
switch($type)
{
case 'email':
if($rule == true)
{
$filtered = filter_var($value, FILTER_VALIDATE_EMAIL);
if($filtered === false)
{
// if false, we have an invalid email
$this->errors[$name] = sprintf('The %s field is required to be a valid email.', $name);
}
}
break;
case 'required':
if($rule == true && empty($value))
{
Expand Down Expand Up @@ -182,7 +193,13 @@ protected function getValidationRules($name)
$requirements = $this->required[$name];

// ensure we have an array, === to true means just required
if($requirements === true || !is_array($requirements))
if($requirements == 'email')
{
$requirements = array(
'email' => true,
);
}
elseif($requirements === true || $requirements === 'required' || !is_array($requirements))
{
// this is the base requirement structure we'll want to keep consistant
// @todo: break out the setup so we can have other types of requirements
Expand Down Expand Up @@ -258,9 +275,9 @@ public function hasErrors()
*/
public function printErrorClass($name)
{
if($this->getErrorMessage($name))
if($error = $this->getErrorMessage($name))
{
return $this->settings['error_class'];
print $this->settings['error_class'];
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/PHPContactor.setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
// Required fields here
// key == name of the field (e.g. 'email' for the field with name="contactForm[name]")
$required = array(
'name' => true,
'email' => true,
'phone' => true,
'name' => 'required',
'email' => 'email',
'phone' => 'required',
);
}

Expand Down

0 comments on commit 61b46df

Please sign in to comment.