Skip to content
This repository has been archived by the owner on Jul 25, 2020. It is now read-only.

Commit

Permalink
Added more validators.
Browse files Browse the repository at this point in the history
  • Loading branch information
kayahr committed Aug 2, 2011
1 parent f9510bc commit 34ff690
Show file tree
Hide file tree
Showing 5 changed files with 354 additions and 3 deletions.
106 changes: 106 additions & 0 deletions src/PhoolKit/I18N.php
@@ -0,0 +1,106 @@
<?php
/*
* PhoolKit - A PHP toolkit.
* Copyright (C) 2011 Klaus Reimer <k@ailis.de>
*
* This library is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

namespace PhoolKit;

/**
* Internationalization support.
*
* @author Klaus Reimer (k@ailis.de)
*/
final class I18N
{
/**
* Map from message key to message
* (Pre-filled with default PhoolKit messages).
*/
private static $messages = array(
"phoolkit.validation.required" => "This field is required.",
"phoolkit.validation.minLength" => "Please enter at least %d characters",
"phoolkit.validation.maxLength" => "Please enter no more than %d characters",
"phoolkit.validation.mask" => "Please enter a valid value"
);

/**
* Private constructor to prevent instantiation.
*/
private function __construct()
{
// Nothing to do here
}

/**
* Returns the message for the given message key. If no message was found
* for the key then the key itself is returned surrounded with question
* marks as an indicator that this message is missing.
*
* @param string $key
* The message key.
* @param mixed $args___
* Variable number of optional arguments used by the message.
* @return The resolved message.
*/
public static function getMessage($key, $args___ = NULL)
{
if (!array_key_exists($key, self::$messages))
return "???$key???";
$message = self::$messages[$key];
if (func_num_args() == 1) return $message;
$args = func_get_args();
array_shift($args);
return vsprintf($message, $args);
}

/**
* Adds the given messages.
*
* @param array $messages
* Map from keys to messages.
*/
public static function addMessages($messages)
{
foreach ($messages as $key => $message)
self::$messages[$key] = $message;
}

/**
* Adds a single message.
*
* @param string $key
* The message key.
* @param string $message
* The message.
*/
public static function addMessage($key, $message)
{
self::$messages[$key] = $message;
}

/**
* Loads messages from a PHP file which returns an array (Map from keys
* to messages.)
*
* @param string $filename
* The filename of the PHP file to load the messages from.
*/
public static function loadMessages($filename)
{
self::addMessages(include($filename));
}
}
84 changes: 84 additions & 0 deletions src/PhoolKit/MaskValidator.php
@@ -0,0 +1,84 @@
<?php
/*
* PhoolKit - A PHP toolkit.
* Copyright (C) 2011 Klaus Reimer <k@ailis.de>
*
* This library is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

namespace PhoolKit;

/**
* Validates a form field with a regular expression mask.
*
* @author Klaus Reimer (k@ailis.de)
*/
final class MaskValidator implements Validator
{
/** The fields to validate. */
private $fields;

/** The mask as a regular expression. */
private $mask;

/**
* Constructor
*
* @param number $mask
* The mask as a regular expression. Must be enclosed with
* slashes and can use switches after the expression.
* Example: /[a-z]{5}/i
*
* @param string... $fields___
* The field names to validate.
*/
public function __construct($mask, $fields___)
{
$args = func_get_args();
$this->mask = array_shift($args);
$this->fields = $args;
}

/**
* @see Validator::validate()
*/
public function validate(Form $form)
{
foreach ($this->fields as $field)
{
if (!preg_match($this->mask, $form->readProperty($field)))
$form->addError($field, I18N::getMessage(
"phoolkit.validation.mask", $this->mask));
}
}

/**
* @see phable.Validator::getScript()
*/
public function getScript()
{
$mask = $this->mask;
$message = StringUtils::escapeJS(I18N::getMessage(
"phoolkit.validation.mask", $mask));
$script = "var m = '$message';\n";
$script .= "var v = $mask;\n";
foreach ($this->fields as $field)
{
$property = StringUtils::escapeJS($field);
$script .= "if (!this.get('$property').match(v)) " .
"this.error('$property', m);\n";
}
return $script;
}
}
80 changes: 80 additions & 0 deletions src/PhoolKit/MaxLengthValidator.php
@@ -0,0 +1,80 @@
<?php
/*
* PhoolKit - A PHP toolkit.
* Copyright (C) 2011 Klaus Reimer <k@ailis.de>
*
* This library is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

namespace PhoolKit;

/**
* Checks for minimum length of submitted data.
*
* @author Klaus Reimer (k@ailis.de)
*/
final class MaxLengthValidator implements Validator
{
/** The fields to validate. */
private $fields;

/** The maximum length to check for. */
private $maxLength;

/**
* Constructor
*
* @param number $maxLength
* The maximum length to check for.
* @param string... $fields___
* The field names to validate.
*/
public function __construct($maxLength, $fields___)
{
$args = func_get_args();
$this->maxLength = array_shift($args);
$this->fields = $args;
}

/**
* @see Validator::validate()
*/
public function validate(Form $form)
{
foreach ($this->fields as $field)
{
if (strlen($form->readProperty($field)) > $this->maxLength)
$form->addError($field, I18N::getMessage(
"phoolkit.validation.minLength", $this->maxLength));
}
}

/**
* @see phable.Validator::getScript()
*/
public function getScript()
{
$maxLength = $this->maxLength;
$message = StringUtils::escapeJS(I18N::getMessage(
"phoolkit.validation.maxLength", $maxLength));
$script = "var m = '$message';\n";
foreach ($this->fields as $field)
{
$property = StringUtils::escapeJS($field);
$script .= "if (this.get('$property').length > $maxLength) " .
"this.error('$property', m);\n";
}
return $script;
}
}
80 changes: 80 additions & 0 deletions src/PhoolKit/MinLengthValidator.php
@@ -0,0 +1,80 @@
<?php
/*
* PhoolKit - A PHP toolkit.
* Copyright (C) 2011 Klaus Reimer <k@ailis.de>
*
* This library is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

namespace PhoolKit;

/**
* Checks for maximum length of submitted data.
*
* @author Klaus Reimer (k@ailis.de)
*/
final class MinLengthValidator implements Validator
{
/** The fields to validate. */
private $fields;

/** The minimum length to check for. */
private $minLength;

/**
* Constructor
*
* @param number $minLength
* The minimum length to check for.
* @param string... $fields___
* The field names to validate.
*/
public function __construct($minLength, $fields___)
{
$args = func_get_args();
$this->minLength = array_shift($args);
$this->fields = $args;
}

/**
* @see Validator::validate()
*/
public function validate(Form $form)
{
foreach ($this->fields as $field)
{
if (strlen($form->readProperty($field)) < $this->minLength)
$form->addError($field, I18N::getMessage(
"phoolkit.validation.minLength", $this->minLength));
}
}

/**
* @see phable.Validator::getScript()
*/
public function getScript()
{
$minLength = $this->minLength;
$message = StringUtils::escapeJS(I18N::getMessage(
"phoolkit.validation.minLength", $minLength));
$script = "var m = '$message';\n";
foreach ($this->fields as $field)
{
$property = StringUtils::escapeJS($field);
$script .= "if (this.get('$property').length < $minLength) " .
"this.error('$property', m);\n";
}
return $script;
}
}
7 changes: 4 additions & 3 deletions src/PhoolKit/RequireValidator.php
Expand Up @@ -47,8 +47,8 @@ public function validate(Form $form)
{
foreach ($this->fields as $field)
{
if (!$form->readProperty($field))
$form->addError($field, "Dieses Feld muss ausgefüllt sein.");
if (!$form->readProperty($field)) $form->addError($field,
I18N::getMessage("phoolkit.validation.required"));
}
}

Expand All @@ -57,7 +57,8 @@ public function validate(Form $form)
*/
public function getScript()
{
$message = StringUtils::escapeJS("Dieses Feld muss ausgefüllt sein.");
$message = StringUtils::escapeJS(I18N::getMessage(
"phoolkit.validation.required"));
$script = "var m = '$message';\n";
foreach ($this->fields as $field)
{
Expand Down

0 comments on commit 34ff690

Please sign in to comment.