Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update documentation

  • Loading branch information...
commit e933bb16bd3fbc446ebcb56f6d48a647a8b6f581 1 parent 11bf130
@c9s authored
Showing with 131 additions and 12 deletions.
  1. +79 −1 README.md
  2. +52 −11 src/ValidationKit/Validator.php
View
80 README.md
@@ -24,11 +24,39 @@ The $msgs() is an associative array that contains:
msg_id => msg_str
-For different kind of validation message, validator
+For different kind of validation messages, every validator
provides its custom msgid for message mapping, you
can simply override the message dictionary to customize
your messages.
+The following sample code shows the format of message dictionary:
+
+```php
+<?php
+ $validtor = new PasswordValidator(array( /* options */ ), array(
+ 'require_digits_error' => 'Please enter digits in your password',
+ 'require_alpha_error' => 'Please enter alphabets in your password',
+ 'max_length_error' => 'The maximum length of password is 24 charactors.'
+ ))
+?>
+```
+
+The following list is the default message mapping:
+
+- valid
+- invalid
+
+And of course you can easily extend messages in your customized
+validator class.
+
+to return an invalid message in your validator:
+
+ return $this->invalid('require_digits_error');
+
+to return an valid message in your validator:
+
+ return $this->valid('require_digits_error');
+
### EmailValidator
```php
@@ -119,6 +147,56 @@ $validator = new ValidationKit\PasswordValidator(array(
$bool = $validator->validate( 10.0 );
```
+# Customize your validator
+
+To write your own validator, here is the basic structure of a validator class:
+
+```php
+<?php
+namespace YourApp;
+use ValidationKit\Validator;
+
+class YourValidator extends Validator
+{
+ public function validate($val)
+ {
+ return $this->valid();
+ }
+}
+```
+
+You can provide your default message dictinory by defining a
+`getDefaultMessages` method:
+
+```php
+<?php
+namespace YourApp;
+use ValidationKit\Validator;
+
+class YourValidator extends Validator
+{
+ public function getDefaultMessages()
+ {
+ return array(
+ 'valid' => 'Its a valid value.',
+ 'too_large' => 'Your value is too large',
+ 'too_small' => 'Your value is too small',
+ );
+ }
+
+
+ public function validate($val)
+ {
+ if( $val > 30 )
+ return $this->invalid('too_large');
+
+ if( $val < 10 )
+ return $this->invalid('too_small');
+ return $this->valid();
+ }
+}
+```
+
# Hacking
Install dependencies:
View
63 src/ValidationKit/Validator.php
@@ -36,10 +36,24 @@ public function __construct($options = array(), $msgstrs = array())
} elseif( ! $msgstrs ) {
$msgstrs = array();
}
- $this->msgstrs = array_merge(array(
- 'invalid' => 'Invalid',
- 'valid' => 'Valid',
- ),$msgstrs);
+ $this->msgstrs = array_merge($this->getBuiltinMessages(),$this->getDefaultMessages());
+ $this->msgstrs = array_merge($this->msgstrs, $msgstrs);
+ }
+
+ public function getBuiltinMessages()
+ {
+ return array(
+ 'invalid' => 'Invalid value',
+ 'valid' => 'Valid value',
+ );
+ }
+
+ /**
+ * This method is for subclass to override.
+ */
+ public function getDefaultMessages()
+ {
+ return array();
}
/**
@@ -75,30 +89,57 @@ public function addInvalidMessage($msgId)
$this->messages[ $msgId ] = ValidationMessage::createInvalid($msgId,$this->getMsgstr($msgId));
}
+
+ /**
+ * Returns result messages
+ *
+ * @return array result messages
+ */
public function getMessages()
{
return $this->messages;
}
+ /**
+ * Set result messages
+ *
+ * @param array $messages
+ */
public function setMessages($messages)
{
$this->messages = $messages;
}
- protected function invalid($msgId = null)
+ /**
+ * Return a valid message with message id,
+ * the message string parameter is optional (for default message)
+ *
+ * If the message is defined in message dictionary,
+ * it won't overwrite it.
+ *
+ * @param string $msgId
+ * @param string $msgStr
+ */
+ protected function valid($msgId = null,$msgStr = null)
+ {
+ $msgId = $msgId ?: 'valid';
+ $this->addValidMessage($msgId);
+ if( ! $this->hasMsgstr($msgId) )
+ $this->setMsgstr($msgId,$msgStr);
+ return $this->isValid = true;
+ }
+
+ protected function invalid($msgId = null, $msgStr = null)
{
$msgId = $msgId ?: 'invalid';
$this->addInvalidMessage($msgId);
+ if( ! $this->hasMsgstr($msgId) )
+ $this->setMsgstr($msgId,$msgStr);
$this->isValid = false;
return false;
}
- protected function valid($msgId = null)
- {
- $msgId = $msgId ?: 'valid';
- $this->addValidMessage($msgId);
- return $this->isValid = true;
- }
+
public function isInvalid()
{
Please sign in to comment.
Something went wrong with that request. Please try again.