PHP Classes

Multiple validations on 1 element

Recommend this page to a friend!

      Another Form generator  >  All threads  >  Multiple validations on 1 element  >  (Un) Subscribe thread alerts  
Subject:Multiple validations on 1 element
Summary:Multiple Validations
Messages:5
Author:Tim
Date:2010-07-06 12:17:24
Update:2010-07-07 17:09:30
 

 


  1. Multiple validations on 1 element   Reply   Report abuse  
Picture of Tim Tim - 2010-07-06 12:17:24
Is it possible to do multiple validations on one single element?

  2. Re: Multiple validations on 1 element   Reply   Report abuse  
Picture of Gergely Aradszki Gergely Aradszki - 2010-07-06 21:55:07 - In reply to message 1 from Tim
Haven't thought of this yet, so there is no easy way.
Although one can write a wrapper validator, that takes other validators list as arguments, then runs the value through all of them.
I'll try to implement multiple validators, but until then, here is the workaround:

//Assign the validator function with other validators name as arguments
$form->validator("username", "validatorWrapper", "lengthValidator", "strengthValidator");

//sample validator function
public function validatorWrapper($value, $args){
foreach ($args as $func){
$this->error=$this->${$func}($value);
if ($this->error){
break;
}
}
return $this->error;
}

//I haven't tested this, so might have bugs

I must admit, that this solution has its limitations:
-you can only retrieve the first error message (maybe you can modify the function to append messages)
-you lose the ability to pass parameters to validator functions.



  3. Re: Multiple validations on 1 element   Reply   Report abuse  
Picture of Tim Tim - 2010-07-07 10:45:00 - In reply to message 2 from Gergely Aradszki
Thx for the respones, i'm going to check it out.

If your intrested, i wrote a extention to your class, maybe it might be usefull for others. The functions compares to elements in a given function. I wrote it to compare to password field, but it might be usefull for other sources to.

*************************************************
EDIT IN validateFields()
*************************************************

protected function validateFields() {
if ($this->fields) {

foreach ($this->fields as $id =>$field) {
if (isset($field["mandatory"])) {
$mandatory=$field["mandatory"];
}else {
$mandatory=false;
}
if (isset($field["validator"])) {
$validatorFunc=$field["validator"];
}else {
$validatorFunc=false;
}
if (isset($field["args"])) {
$args=$field["args"];
}else {
$args=false;
}
if (isset($this->input[$this->getConfig("submitField")])) {
if ( (isset($this->input[$id])) && ($this->input[$id]==="") && ($mandatory) ) {
$this->error($id);
}
if ($validatorFunc) {
if (isset($this->input[$id])) {
$value=$this->input[$id];
}else {
$value=false;
}
$this->error($id, $this->validator->{$validatorFunc}($value, array_slice($args, 2)),$this->input);

}

}
}

if (isset($this->fields["vergelijk1"]["validatorVergelijk"])) {
$validatorVergelijkFunc= $this->fields["vergelijk1"]["validatorVergelijk"];


}else {
$validatorVergelijkFunc=false;
}

if (isset($this->input[$this->getConfig("submitField")])) {

if ( (isset($this->input[$this->fields["vergelijk1"]["field"]])) && ($this->input[$this->fields["vergelijk1"]["field"]]==="")) {
$this->error($this->fields["vergelijk1"]["field"]);
}

if ($validatorVergelijkFunc) {
if (isset($this->input[$this->fields["vergelijk1"]["field"]]) ) {
$value1 = $this->input[$this->fields["vergelijk1"]["field"]];
$value2 = $this->input[$this->fields["vergelijk2"]["field"]];
}else {
$value1=false;
$value2=false;
}
$this->error($this->fields["vergelijk1"]["field"], $this->validatorVergelijk->{$validatorVergelijkFunc}($value1, $value2),$this->input[$this->fields["vergelijk1"]["field"]]);

}
}

}
}

*************************************************
Compare()
*************************************************

public function Compare($field1,$field2, $function) {
try {
if ($this->validatorVergelijk===null) {
$this->validatorVergelijk=false;
if(($this->getConfig("validator"))&&(file_exists($this->getConfig("validator")))) {
include_once($this->getConfig("validator"));
if (class_exists($this->getConfig("validatorClass"))) {
$this->validatorVergelijk=new Validator();
}else {
throw new Exception("Validator class "" .$this->getConfig("validatorClass"). "" does not exists.");
}
}else {
throw new Exception("File not found: "".$this->getConfig("validatorClass"). """);
}
}
if ((isset($function))&&(method_exists($this->validatorVergelijk, $function))) {
$this->fields["vergelijk1"]["validatorVergelijk"]=$function;
$this->fields["vergelijk1"]["field"]=$field1;
$this->fields["vergelijk2"]["field"]=$field2;
}else {
throw new Exception("Method "".$function. "" does not exists.");
}
}catch (Exception $e ){
$this->debug("Error in " . $this->getConfig("name") . " Form->validator: " .$e->getMessage());
}
}
*************************************************
USE
*************************************************

{$VAR}->Compare($elementid1,$elementid2,function);

  4. Re: Multiple validations on 1 element   Reply   Report abuse  
Picture of Gergely Aradszki Gergely Aradszki - 2010-07-07 15:52:00 - In reply to message 3 from Tim
The idea is good, but it has one flaw: by modifying the core class, you break the compatibility with following versions.

Since v.0.7.13 the validator functions can access the fields array of the form, so it is possible to write this function as a validator extension.

This has 2 advantages:
-core class will remain compatible
-you can initialize the compare validator for any two fields during setup, so the id-s are not hard-coded into the class

Anyway, I'll take your solution and implement it as a validator extension if you don't mind, because it really adresses a useful feature. Thanks for your time improving this class.

  5. Re: Multiple validations on 1 element   Reply   Report abuse  
Picture of Tim Tim - 2010-07-07 17:09:30 - In reply to message 4 from Gergely Aradszki
Oke thx for the advise! And ofcourse you could use it in your examples :)!