PHP Classes

Mutualy exclusive validation

Recommend this page to a friend!

      PHP Forms Class with HTML Generator and JavaScript Validation  >  PHP Forms Class with HTML Generator and JavaScript Validation package blog  >  How to Show Google Ma...  >  All threads  >  Mutualy exclusive validation  >  (Un) Subscribe thread alerts  
Subject:Mutualy exclusive validation
Summary:Mutualy exclusive validation, 2 fields only one allowed value
Messages:7
Author:Nikos
Date:2008-06-03 18:16:42
Update:2008-06-20 16:23:39
 

  1. Mutualy exclusive validation   Reply   Report abuse  
Picture of Nikos Nikos - 2008-06-03 18:16:42
Hello again users of this class.

Is it possible to have mutually exclusive validation. That is if I have 2 inputs I must only allow one field to be filled in and the other must be empty.

Thanks for any advice

  2. Re: Mutualy exclusive validation   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2008-06-04 02:03:11 - In reply to message 1 from Nikos
That is a very specific type of validation. You need to create a custom validation plug-in. Take a look at the test_custom_validation.php example script to see how you can implement your special type of validation.

  3. Re: Mutualy exclusive validation   Reply   Report abuse  
Picture of Nikos Nikos - 2008-06-04 11:01:01 - In reply to message 2 from Manuel Lemos
Thanks for this, I will research it.

  4. Re: Mutualy exclusive validation   Reply   Report abuse  
Picture of Nikos Nikos - 2008-06-04 15:33:18 - In reply to message 3 from Nikos
In your the line 84 comment in form_custom_validation.php it should read:

/*
* Generate all the necessary Javascript to perform client side
* validation.
*/

instead of

/*
* Generate all the necessary Javascript to perform server side
* validation.
*/


  5. Re: Mutualy exclusive validation   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2008-06-04 20:56:00 - In reply to message 4 from Nikos
Good catch. I have fixed that now. Thank you for reporting.

  6. Re: Mutualy exclusive validation   Reply   Report abuse  
Picture of Nikos Nikos - 2008-06-20 15:20:30 - In reply to message 5 from Manuel Lemos
The following version of your form_custom_validation.php class for mutually exclusive validation was used with the test_custom_validation.php and it worked fine

<?php
/*
*
* @(#) $Id: form_custom_validation.php,v 1.2 2007/03/02 05:57:05 mlemos Exp $
*
*/

if(!defined("PHP_LIBRARY_CUSTOM_VALIDATION_FORMS"))
{
define("PHP_LIBRARY_CUSTOM_VALIDATION_FORMS",1);

class form_custom_validation 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 $first='';
var $second='';
var $first_validation_error_message='You may only enter a value in Effect Size Absolute Rate or in Effect Size Absolute Time, not both';




var $second_validation_error_message='You must enter a value in Effect Size Absolute Rate or in Effect Size Absolute Time, not both';

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');

/*
* Get the error messages to assign when the inputs are invalid.
*/
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'];
}
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'];
}

return('');
}

Function ValidateInput(&$form)
{
/*
* Perform server side validation by checking whether mutualy exlusive input values have been entered
*
* This function is called after all validations were performed on
* all basic inputs.
*/
$first=$form->GetInputValue($this->first);
$second=$form->GetInputValue($this->second);
if($first != "" && $second != "" ) // validation rule
{
$form->FlagInvalidInput($this->first, $this->first_validation_error_message);
return('');
}
if($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');

/*
* 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!='' && first!=''",

/*
* 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(),
'Condition'=>"second=='' && first==''",
'ErrorMessage'=>$this->second_validation_error_message,
'Focus'=>$this->second
);

return('');
}

Function AddInputPart(&$form)
{
/*
* Inputs that do not appear in the form must implement an empty
* AddInputPart function.
*/
return('');
}
};

}

?>

However when I try to use it in my own php page that uses your forms with the following code, the Javascript for this validation is not outputed to the page and no custome Javascript validation is done on these two inputs. Any ideas?

$form_reg->AddInput(array ("TYPE" => "text",
"NAME" => "EffectSizeAbsoluteRate",
"ID" => "EffectSizeAbsoluteRate",
"VALUE" => "$EffectSizeAbsoluteRate",
"ValidateOptionalValue"=>"",
"ValidateRegularExpression" => "^[0-9]+$",
"ValidateRegularExpressionErrorMessage" => "The Effect Size Absolute Rate must be an integer",
"LABEL" => "Effect Size Absolute Rate"
));

$form_reg->AddInput(array ("TYPE" => "text",
"NAME" => "EffectSizeAbsolutetime",
"ID" => "EffectSizeAbsolutetime",
"VALUE" => "$EffectSizeAbsolutetime",
"ValidateRegularExpression" => "^[0-9]{2}:[0-5][0-9]:[0-5][0-9].[0-9]?[0-9]$",
"ValidateRegularExpressionErrorMessage" => "The Effect Size Absolute time must be of the format (hh:mm:ss.mm)",
"ValidateOptionalValue"=>"",
"LABEL" => "Time Span (hh:mm:ss.mm)"

));


/*
* Add a custom input that will be used only for validation purposes
*/
$error=$form_reg->AddInput(array(
'TYPE'=>'custom',
'ID'=>'validation',

/*
* Specify the custom plug-in input class name.
*/
'CustomClass'=>'form_custom_validation_metaneva',

/*
* Specify some custom parameters specific of this plug-in input
*/
'FirstInput'=>'EffectSizeAbsoluteRate',
'FirstValidationErrorMessage'=>'You may only enter a value in Effect Size Absolute Rate or in Effect Size Absolute Time',
'FirstValidationErrorMessage'=>'You must enter a value in Effect Size Absolute Rate or in Effect Size Absolute Time',
'SecondInput'=>'EffectSizeAbsoluteTime'
//'SecondValidationErrorMessage'=>'The second name is contained in the first name.'
));

  7. Re: Mutualy exclusive validation   Reply   Report abuse  
Picture of Nikos Nikos - 2008-06-20 16:23:39 - In reply to message 6 from Nikos
Sorry I forgot to add the input

$form->AddInputPart('validation');

Please feel free to use my code for your own purposes, or let me know if you want me to code other custom validations