Symbi0nt's Model Validation (examples) | |
| Type: Sample Code (HOWTO) |
Category: Other |
| License: GNU General Public License |
Language: PHP |
| Description: This are the examples for my app_model.php These are three examples of Models, thus you should see how to use the validation. Below two expamples from the view. NOTW: if you start to add your own validation methods like seen in example 2, use $args = func_num_args(); print_r($args) to see what the function gives you. | |
|
Download a raw-text version of this code by clicking on "Download Version"
<?php
// First simple Example
class Event extends AppModel
{
var $name = 'Event';
var $validationErrorMessages = INVALIDATE_VERBOSE_STEP;
var $validate = array(
'date' => array(
array(VALID_IF_NOT_EMPTY, '... your Date is empty'),
array(VALID_IF_EXISTING_DATE, '%Y-%m-%d', '... your Date is not valid')
),
'head' => array(
array(VALID_IF_NOT_EMPTY, '... the heading is empty'),
array(VALID_IF_MINIMUM_LENGTH_OF, 5, '... the heading should be at least 5 chars long')
),
'description' => array(
array(VALID_IF_NOT_EMPTY, '... the description is empty'),
array(VALID_IF_MINIMUM_LENGTH_OF, 10, '... the description should be at least 5 chars long')
)
);
}
?>
<?php
// Second Example with your own check method right here
class Affiliate extends AppModel
{
var $name = 'Affiliate';
var $validationErrorMessages = INVALIDATE_VERBOSE_STEP;
var $validate = array(
'slug' => array(
array(VALID_IF_NOT_EMPTY, '... the slug is empty'),
array(VALID_IF_MINIMUM_LENGTH_OF, 2, '... the slug should be at least 2 chars long'),
array(VALID_IF_MAXIMUM_LENGTH_OF, 20, '... the slug should be not longer than 20 chars'),
array('_chkIfMatchesRegEx', '/^[a-z_]*$/', '... the slug should only contain lower letters and underscores')
)
);
function _chkIfMatchesRegEx()
{
$args = func_get_args();
return $this->_chkByRegEx($args[0], $args[1][2][0]);
}
}
?>
<?php
// Third Example -> Advanced with conditions
class Abo extends AppModel
{
var $name = 'Abo';
var $useTable = false;
var $validationErrorMessages = INVALIDATE_VERBOSE_STEP;
var $validate = array(
'forename' => array(
array(VALID_IF_NOT_EMPTY, '... der Vorname ist leer'),
array(VALID_IF_MINIMUM_LENGTH_OF, 2, '... der Vorname muss aus mindestens 2 Zeichen bestehen'),
array(VALID_IF_ONLY_NAME_CHARS, '... der Vorname enthält untypische Zeichen')
),
'lastname' => array(
array(VALID_IF_NOT_EMPTY, '... der Nachname ist leer'),
array(VALID_IF_MINIMUM_LENGTH_OF, 2, '... der Nachname muss aus mindestens 2 Zeichen bestehen')
// VALID_IF_ONLY_NAME_CHARS
),
'city' => array(
array(VALID_IF_NOT_EMPTY, '... your town is empty'),
array(VALID_IF_MINIMUM_LENGTH_OF, 2, '... your town should be at least 2 chars long')
),
'birthday' => array(
array(VALID_IF_NOT_EMPTY, '... the brithdate is empty'),
array(VALID_IF_EXISTING_DATE, '%Y-%m-%d', '... the birthdate is not valid')
),
'email' => array(
array(VALID_IF_NOT_EMPTY, '... the e-mail is empty'),
array(VALID_IF_MINIMUM_LENGTH_OF, 6, '... the e-mail should contain at least six chars'),
array(VALID_IF_EMAIL, '... the e-mail syntax is wrong')
)
);
function beforeValidate() {
// if the fone number is optional, but should be in a certain syntax you can use
// beforeValidate to add the validation for these fields
if (!empty($this->data['Abo']['fon_pre']) || !empty($this->data['Abo']['fon_number'])) {
$add = array(
'fon_pre' => array(
array(VALID_IF_ADDITIONAL_FIELDS_ARE_NOT_EMPTY, 'fon_number', '... the 2nd field of you phone number is empty'),
array(VALID_IF_NUMBER, '... the phone number should only contain numbers')
),
'fon_number' => array(
array(VALID_IF_ADDITIONAL_FIELDS_ARE_NOT_EMPTY, 'fon_pre', '... the area code of your phone number is empty'),
array(VALID_IF_NUMBER, '... the phone number should only contain numbers')
)
);
$this->validate = array_merge($this->validate, $add);
}
// HACK: I use an hidden input field to determine what kind of subscription form I use
// There are some, sp I check for that value and can add another bunch of validation stuff as well
if ($this->data['Abo']['type'] == '10' ) {
$add = array(
'bank_account' => array(
array(VALID_IF_NOT_EMPTY, '... '), // Your text here
array(VALID_IF_NUMBER, '... '),
array(VALID_IF_MINIMUM_LENGTH_OF, 4, '... ')
),
'bank_name' => array(
array(VALID_IF_NOT_EMPTY, '... '),
array(VALID_IF_MINIMUM_LENGTH_OF, 2, '... ')
),
'bank_user' => array(
array(VALID_IF_NOT_EMPTY, '... '),
array(VALID_IF_MINIMUM_LENGTH_OF, 4, '... ')
),
'bank_blz' => array(
array(VALID_IF_NOT_EMPTY, '... '),
array(VALID_IF_NUMBER, '... '),
array(VALID_IF_MINIMUM_LENGTH_OF, 4, '... ')
),
);
$this->validate = array_merge($this->validate, $add);
}
// German zip codes are always only numbers and 5 digits long
if ($this->data['Abo']['country_id'] == 'DE' ) {
$add = array(
'plz' => array(
array(VALID_IF_NOT_EMPTY, '... your zip is empty'),
array(VALID_IF_NUMBER, '... your zip is not a number'),
array(VALID_IF_LENGTH_OF, 5, '... your zip should be 5 digits long')
)
);
$this->validate = array_merge($this->validate, $add);
}
return true;
}
// This here is commentet in the app_model.php as well, it checks creditcards for syntax
function _chkIfCreditCardNumber()
{
$args = func_get_args();
$type = array('1' => 'Visa', '2' => 'MasterCard', '3' => 'American Express');
return $this->CCValidationSolution($args[0], $type[$args[1][3][$args[1][2][0]]]);
}
// CreditCard Check Code //////////////////////////////////////////////////////////////////////////////
function CCValidationSolution($number, $card = null)
{
$name = $this->CCNameSolution($number);
if(!$name) {
return false;
} else {
$CardName = $name['CardName'];
$ShouldLength = $name['ShouldLength'];
}
if($card != null) {
if($card != $CardName) {
return false;
}
}
$numberLength = strlen($number);
# 3) Is the number the right length?
if ($numberLength != $ShouldLength)
return false;
# 4) Does the number pass the Mod 10 Algorithm Checksum?
if ($this->Mod10Solution($number) == true)
return true;
else
return false;
}
function CCNameSolution($number)
{
$numberLeft = substr($number, 0, 4);
if ($numberLeft >= 3000 and $numberLeft <= 3059)
{
$CardName = 'Diners Club';
$ShouldLength = 14;
}
elseif ($numberLeft >= 3600 and $numberLeft <= 3699)
{
$CardName = 'Diners Club';
$ShouldLength = 14;
}
elseif ($numberLeft >= 3800 and $numberLeft <= 3889)
{
$CardName = 'Diners Club';
$ShouldLength = 14;
}
elseif ($numberLeft >= 3400 and $numberLeft <= 3499)
{
$CardName = 'American Express';
$ShouldLength = 15;
}
elseif ($numberLeft >= 3700 and $numberLeft <= 3799)
{
$CardName = 'American Express';
$ShouldLength = 15;
}
elseif ($numberLeft >= 3528 and $numberLeft <= 3589)
{
$CardName = 'JCB';
$ShouldLength = 16;
}
elseif ($numberLeft >= 3890 and $numberLeft <= 3899)
{
$CardName = 'Carte Blache';
$ShouldLength = 14;
}
elseif ($numberLeft >= 4000 and $numberLeft <= 4999)
{
$CardName = 'Visa';
$ShouldLength = 16;
}
elseif ($numberLeft >= 5100 and $numberLeft <= 5599)
{
$CardName = 'MasterCard';
$ShouldLength = 16;
}
elseif ($numberLeft == 5610)
{
$CardName = 'Australian BankCard';
$ShouldLength = 16;
}
elseif ($numberLeft == 6011)
{
$CardName = 'Discover/Novus';
$ShouldLength = 16;
}
if(isset($CardName) && isset($ShouldLength))
{
$return_array['CardName'] = $CardName;
$return_array['ShouldLength'] = $ShouldLength;
return $return_array;
}
else
return false;
}
function Mod10Solution($number)
{
$numberLength = strlen($number);
$Checksum = 0;
# Add even digits in even length strings
# or odd digits in odd length strings.
for ($Location = 1 - ($numberLength % 2); $Location < $numberLength; $Location += 2)
{
$Checksum += substr($number, $Location, 1);
}
# Analyze odd digits in even length strings
# or even digits in odd length strings.
for ($Location = ($numberLength % 2); $Location < $numberLength; $Location += 2)
{
$Digit = substr($number, $Location, 1) * 2;
if ($Digit < 10)
$Checksum += $Digit;
else
$Checksum += $Digit - 9;
}
# Is the checksum divisible by ten?
return ($Checksum % 10 == 0);
}
}
?>
// I use this here in /views/elements/show_errors.thtml
<?php if(isset($this->controller->{$this->controller->modelNames[0]}->validationErrorMessage)) { ?>
<table class="form_errors"><tr><td>
The following errors occured:<br />
<?php
foreach($this->controller->{$this->controller->modelNames[0]}->validationErrorMessage as $elem) {
echo '<div class="error_message">'.$elem.'</div>'."\n";
}
?>
</table>
<?php
}
?>
// use it in your view as follow
<?php echo $this->renderElement('show_errors'); ?>
You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..