dzięki wielkie za pomoc : )
skoro już założyłem temat, to zadam jeszcze jedno nurtujące mnie pytanie. Znalazłem w sieci klase walidacji, na której trochę chcę się wzorować. Jest troche długa ale sądzę, że na tyle czytelna, ze nie powinno być problemu:
class validation {
//field with text to validation
private $checkString = array();
//field with bool values; true ? false
private $_is_valid = array();
//return array with errors
//allowed method for valid
private $allowedMethod = array( 'match', 'maxlenght', 'minlenght', 'required', 'maxtimechar', 'type', 'between');
/**
* fields with error texts
*
*/
public $error_max_lenght = 'Too long';
public $error_min_lenght = 'Too short';
public function setString( $name, $val)
{
//set arraz
$this -> checkString[$name] = $val;
//return obiect
return $this;
}
public function setValidations( $val)
{
//set array
$this -> name = $val;
//return obiect
return $this;
}
/**
* obiect chech from setValidation, if method is no allowed, is skipped
*
*/
public function __call( $m, $a)
{
//chech whether is allowed
if( in_array( $m, $this -> allowedMethod) {
//set
$this -> obiectString[ $this -> name][$m] = $a[0];
}
//return obiect
return $this;
}
//start validation
public function start()
{
//loop array, return array from inside
foreach( $this -> obiectString as $arrays => $valueArrays) {
//here too must be array
//in loop
//data from $valueArrays(array) loop and get names of field with strings to validation
foreach( $valueArrays as $valData => $value) {
//here start validate parser
switch( $valData) {
/**
* case math
* here developer can use youself regex
*
*/
case 'match':
if( preg_match( $value, $this -> checkString[$arrays])) {
$this -> _is_valid
[$arrays][$valData] = preg_match( $value, $this -> checkString[$arrays]);
} else {
$this -> _is_valid[$arrays][$valData] = false;
//set errors array
$this -> error[$arrays][] = $this -> error_match;
}
break;
/**
* case maxlenght
* maximum lenght of string
*
*/
case 'maxlenght':
if( strlen( $this -> checkString[$arrays]) <= $value) {
$this -> _is_valid[$arrays][$valData] = true;
} else {
$this -> _is_valid[$arrays][$valData] = false;
//set errors array
$this -> error[$arrays][] = $this -> error_max_lenght;
}
break;
case 'minlenght':
if( strlen( $this -> checkString[$arrays]) >= $value) {
$this -> _is_valid[$arrays][$valData] = true;
} else {
$this -> _is_valid[$arrays][$valData] = false;
//set errors array
$this -> error[$arrays][] = $this -> error_min_lenght;
}
break;
case 'type':
//next switch
switch( $value) {
case 'string':
if( is_string($this -> checkString[$arrays])) {
$this -> _is_valid[$arrays][$valData] = TRUE;
} else {
$this -> _is_valid[$arrays][$valData] = FALSE;
//set errors array
$this -> error[$arrays][] = $this -> error_type_string;
}
break;
}
}
} else {
//throw exception
throw new Exception( 'Input data must be in array<br />Method: setValidations; first paramert');
}
}
}
/**
* isValid, here return true or false
*
* @access public
* @param string @val here set name of validation string
*
*/
public function isValid( $val) {
//clean buffor
$unset = $this -> checkString;
//multiple valid
foreach( $val as $item) {
$check[] = $this -> isValid( $item);
}
//check whether is minimum one time false, if is return false
return false;
//to return true, must be all times true
} else {
return true;
}
} else {
//check whether is minimum one time false, if is return false
if( in_array( 0
, $this -> _is_valid
[$val])) {
return false;
//to return true, must be all times true
} else {
return true;
}
}
}
i teraz tak, autor tej klasy przedstawia jak powinno się z niej korzystać:
$validation
-> setValidations( 'first_string') -> minlenght( 15) -> maxlenght( 1574)
-> setValidations( 'second_string') -> required( true)
-> setValidations( 'third_string') -> required( true) -> type( 'string') -> between( array( 10
,1587
)) -> start();
i tutaj jest moje pytanie, bo próbuje to przeanalizować już 2 dzień i nie za bardzo mi to wychodzi...
nie wiem jak to się dzieje, że odwołując się do wartości walidacji, np "axlenght( 1574)" nie jest to odwołanie się do metody "axlenght" tylko do 'case' w metodzie start(). Albo inaczej - jeśli za bardzo zagmatwałem:
Jak to sie dzieje, ze te wszystkie wartości (np: minlenght( 15) -> maxlenght( 1574)) są analizowane dopiero w metodzie start() jako 'case' ?
ps: usunąłem pare case'ów z klasy bo temat za długi, oraz medote adderror