From 322df8b0210255951a28c9a798bf0f927f4efaef Mon Sep 17 00:00:00 2001 From: Andrew Willis Date: Thu, 9 Jan 2014 22:18:29 +0000 Subject: [PATCH] added docblock and readme docs for using the new filter --- README.md | 34 ++++++++++++++++++++++++++++++++++ src/Valitron/Validator.php | 9 +++++++++ 2 files changed, 43 insertions(+) diff --git a/README.md b/README.md index 62a0641..54cde9f 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,40 @@ V::lang('ar'); * `dateBefore` - Field is a valid date and is before the given date * `dateAfter` - Field is a valid date and is after the given date * `contains` - Field is a string and contains the given string + * `creditCard` - Field is a valid credit card number + + ## Credit Card Validation usage + + Credit card validation currently allows you to validate a Visa `visa`, + Mastercard `mastercard`, Dinersclub `dinersclub`, American Express `amex` + or Discovery `discovery` + + This will check the credit card against each card type +```php +$v->rule('creditCard', 'credit_card'); +``` + +To optionally filter card types, add the slug to an array as the next parameter: + +```php +$v->rule('creditCard', 'credit_card', ['visa', 'mastercard']); +``` + +If you only want to validate one type of card, put it as a string: + +```php +$v->rule('creditCard', 'credit_card', 'visa'); +``` + +If the card type information is coming from the client, you might also want to +still specify an array of valid card types: + +```php +$cardType = 'amex'; +$v->rule('creditCard', 'credit_card', $cardType, ['visa', 'mastercard']); +$v->validate(); // false +``` + ## Adding Custom Validation Rules diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index d95cbc6..cea357b 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -472,6 +472,15 @@ class Validator return (is_bool($value)) ? true : false; } + /** + * Validate that a field contains a valid credit card + * optionally filtered by an array + * + * @param string $field + * @param string $value + * @param array $params + * @return bool + */ protected function validateCreditCard($field, $value, $params) { /**