Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- *
- * @(#) $Id: form_custom_validation.php,v 1.3 2008/02/12 02:02:30 mlemos Exp $
- *
- */
- function seDay($begin, $end, $format, $sep)
- {
- $pos1 = strpos($format, 'd');
- $pos2 = strpos($format, 'm');
- $pos3 = strpos($format, 'Y');
- $begin = explode($sep, $begin);
- $end = explode($sep, $end);
- $first = GregorianToJD($end[$pos2], $end[$pos1], $end[$pos3]);
- $second = GregorianToJD($begin[$pos2], $begin[$pos1], $begin[$pos3]);
- if ($first > $second)
- return $first - $second;
- else
- return $second - $first;
- }
- if(!defined("PHP_LIBRARY_CUSTOM_VALIDATION_FORMS"))
- {
- define("PHP_LIBRARY_CUSTOM_VALIDATION_FORMS",1);
- class form_custom_validation_class extends form_custom_class
- {
- /*
- * Tell the forms class that this custom input will perform both server
- * side and client side validation.
- */
- var $client_validate=1;
- var $server_validate=1;
- var $anreisetag='';
- var $anreisemonat='';
- var $anreisejahr='';
- var $abreisetag='';
- var $abreisemonat='';
- var $abreisejahr='';
- var $minstay='';
- var $minstay_validation_error_message='Minstay nicht korrekt.';
- var $first='';
- var $second='';
- var $first_validation_error_message='The first input value is contained in the second.';
- var $second_validation_error_message='The second input value is contained in the first.';
- Function AddInput(&$form, $arguments)
- {
- /*
- * Get the identifiers of the inputs to validate
- */
- if(!IsSet($arguments['FirstInput'])
- || strlen($this->first=$arguments['FirstInput'])==0)
- return('It was not specified a valid first input identifier');
- if(!IsSet($arguments['SecondInput'])
- || strlen($this->second=$arguments['SecondInput'])==0)
- return('It was not specified a valid second input identifier');
- if(!IsSet($arguments['AnreisetagInput'])
- || strlen($this->anreisetag=$arguments['AnreisetagInput'])==0)
- return('It was not specified a valid AnreisetagInput input identifier');
- if(!IsSet($arguments['AnreisemonatInput'])
- || strlen($this->anreisemonat=$arguments['AnreisemonatInput'])==0)
- return('It was not specified a valid AnreisemonatInput input identifier');
- if(!IsSet($arguments['AnreisejahrInput'])
- || strlen($this->anreisejahr=$arguments['AnreisejahrInput'])==0)
- return('It was not specified a valid AnreisejahrInput input identifier');
- if(!IsSet($arguments['AbreisetagInput'])
- || strlen($this->abreisetag=$arguments['AbreisetagInput'])==0)
- return('It was not specified a valid AbreisetagInput input identifier');
- if(!IsSet($arguments['AbreisemonatInput'])
- || strlen($this->abreisemonat=$arguments['AbreisemonatInput'])==0)
- return('It was not specified a valid AbreisemonatInput input identifier');
- if(!IsSet($arguments['AbreisejahrInput'])
- || strlen($this->abreisejahr=$arguments['AbreisejahrInput'])==0)
- return('It was not specified a valid AbreisejahrInput input identifier');
- if(!IsSet($arguments['MinstayInput'])
- || strlen($this->minstay=$arguments['MinstayInput'])==0)
- return('It was not specified a valid MinstayInput input identifier');
- /*
- * Get the error messages to assign when the inputs are invalid.
- */
- if(IsSet($arguments['SecondValidationErrorMessage']))
- {
- if(strlen($arguments['SecondValidationErrorMessage'])==0)
- return('It was not specified a valid second validation error message');
- $this->second_validation_error_message=$arguments['SecondValidationErrorMessage'];
- }
- if(IsSet($arguments['MinstayValidationErrorMessage']))
- {
- if(strlen($arguments['MinstayValidationErrorMessage'])==0)
- return('It was not specified a valid Minstay validation error message');
- $this->minstay_validation_error_message=$arguments['MinstayValidationErrorMessage'];
- }
- if(IsSet($arguments['FirstValidationErrorMessage']))
- {
- if(strlen($arguments['FirstValidationErrorMessage'])==0)
- return('It was not specified a valid first validation error message');
- $this->first_validation_error_message=$arguments['FirstValidationErrorMessage'];
- }
- return('');
- }
- Function ValidateInput(&$form)
- {
- /*
- * Perform server side validation by checking whether one of the
- * input values contains the other.
- *
- * This function is called after all validations were performed on
- * all basic inputs.
- */
- $first=$form->GetInputValue($this->first);
- $second=$form->GetInputValue($this->second);
- $anreisetag=$form->GetInputValue($this->anreisetag);
- $anreisemonat=$form->GetInputValue($this->anreisemonat);
- $anreisejahr=$form->GetInputValue($this->anreisejahr);
- $abreisetag=$form->GetInputValue($this->abreisetag);
- $abreisemonat=$form->GetInputValue($this->abreisemonat);
- $abreisejahr=$form->GetInputValue($this->abreisejahr);
- $minstay=$form->GetInputValue($this->minstay);
- $datum1 = $anreisetag.".".$anreisemonat.".".$anreisejahr;
- $datum2 = $abreisetag.".".$abreisemonat.".".$abreisejahr;
- $stay = seDay($datum1,$datum2,"dmY",".");
- if($minstay > $stay)
- {
- $form->FlagInvalidInput($this->minstay, $this->minstay_validation_error_message);
- return('');
- }
- if(strlen($first)
- && strstr($second, $first))
- {
- $form->FlagInvalidInput($this->first, $this->first_validation_error_message);
- return('');
- }
- if(strlen($second)
- && strstr($first, $second))
- {
- $form->FlagInvalidInput($this->second, $this->second_validation_error_message);
- return('');
- }
- return('');
- }
- Function GetJavascriptValidations(&$form, $form_object, &$validations)
- {
- /*
- * Generate all the necessary Javascript to perform server side
- * validation.
- */
- if(strlen($first=$form->GetJavascriptInputValue($form_object,$this->first))==0)
- return('it was not possible to retrieve the first input Javascript value');
- if(strlen($second=$form->GetJavascriptInputValue($form_object,$this->second))==0)
- return('it was not possible to retrieve the second input Javascript value');
- if(strlen($anreisetag=$form->GetJavascriptInputValue($form_object,$this->anreisetag))==0)
- return('it was not possible to retrieve the anreisetag input Javascript value');
- if(strlen($anreisemonat=$form->GetJavascriptInputValue($form_object,$this->anreisemonat))==0)
- return('it was not possible to retrieve the anreisemonat input Javascript value');
- if(strlen($anreisejahr=$form->GetJavascriptInputValue($form_object,$this->anreisejahr))==0)
- return('it was not possible to retrieve the anreisejahr input Javascript value');
- if(strlen($abreisetag=$form->GetJavascriptInputValue($form_object,$this->abreisetag))==0)
- return('it was not possible to retrieve the abreisetag input Javascript value');
- if(strlen($abreisemonat=$form->GetJavascriptInputValue($form_object,$this->abreisemonat))==0)
- return('it was not possible to retrieve the abreisemonat input Javascript value');
- if(strlen($abreisejahr=$form->GetJavascriptInputValue($form_object,$this->abreisejahr))==0)
- return('it was not possible to retrieve the abreisejahr input Javascript value');
- if(strlen($minstay=$form->GetJavascriptInputValue($form_object,$this->minstay))==0)
- return('it was not possible to retrieve the minstay input Javascript value');
- $first=$form->GetInputValue($this->first);
- $second=$form->GetInputValue($this->second);
- $anreisetag=$form->GetInputValue($this->anreisetag);
- $anreisemonat=$form->GetInputValue($this->anreisemonat);
- $anreisejahr=$form->GetInputValue($this->anreisejahr);
- $abreisetag=$form->GetInputValue($this->abreisetag);
- $abreisemonat=$form->GetInputValue($this->abreisemonat);
- $abreisejahr=$form->GetInputValue($this->abreisejahr);
- $minstay=$form->GetInputValue($this->minstay);
- $datum1 = $anreisetag.".".$anreisemonat.".".$anreisejahr;
- $datum2 = $abreisetag.".".$abreisemonat.".".$abreisejahr;
- $stay = seDay($datum1,$datum2,"dmY",".");
- /*
- * Return an array with a list of all validation checks to be
- * performed.
- */
- $validations=array();
- $validations[]=array(
- /*
- * Each.validation check may be preceed by a list of Javascript
- * commands that are executed before each validation is performed.
- */
- 'Commands'=>array(
- 'first='.$first,
- 'second='.$second,
- ),
- /*
- * The condition is a boolean Javascript expression that is true
- * when the input is invalid.
- */
- 'Condition'=>'second.indexOf(first) != -1',
- /*
- * Error message associated to the invalid input
- */
- 'ErrorMessage'=>$this->first_validation_error_message,
- /*
- * Input that gets the user input focus so the user fixes its value
- * to make the input valid
- */
- 'Focus'=>$this->first
- );
- $validations[]=array(
- 'Commands'=>array(
- 'first='.$first,
- 'second='.$second,
- ),
- 'Condition'=>'first.indexOf(second) != -1',
- 'ErrorMessage'=>$this->second_validation_error_message,
- 'Focus'=>$this->second
- );
- $validations[]=array(
- 'Commands'=>array(
- 'minstay='.$minstay,
- 'stay='.$stay,
- ),
- 'Condition'=>'stay < minstay',
- 'ErrorMessage'=>$this->minstay_validation_error_message,
- );
- return('');
- }
- Function AddInputPart(&$form)
- {
- /*
- * Inputs that do not appear in the form must implement an empty
- * AddInputPart function.
- */
- return('');
- }
- };
- }
- ?>
Add Comment
Please, Sign In to add comment