Table of Contents

Zend_Validate_Array class

About

This class was developed when ZF did not have good Zend_Filter_Input. When Zned_Filter_Input was created (ZF 1.0.0) this class lost some of its benefits but is is still useful for people which want to add end-user custom error messages. You can then use Zend_Translate_* . Also Zend_Validate_* classes are not designed to have end-user friendly error messages (to many of them and to detailed ex. Zend_Validate_EmailAddress).

I have no time to polish this class. Here I mean that it does not confirm ZF codding standards and that some of method names could sound strange. I have created proposal in ZF developer (in archive as there were low interest for this) wiki so any contribution is welcome especially if someone want to polish this class.

:!: NOTE - I will be much happier if ZF implemented setUserMessage/getUserMessage method in Zend_Validate_Abstract. But they did not because they said that Zend_Validate is low level and messages are not meant to be passed to users 8-O . Anyway I think they now have some problems. Especially in context of Zend_Form as it is using those low level error messages and displaying them to users :-)

:!: NOTE - There is uptodate version in SVN of my project loota http://svn2.assembla.com/svn/loota/trunk/Core/lib/ZFext/Validate/

Download

Reference

Until better documentation is written please see code below. I hope it is self explaining. if you have questions please send it to boris@kodmasin.net.

Case 21.09.2008.

        $inputValidator = new Zend_Validate_Array(array('roleIdEdit', 'rolenameEdit', 'roledescEdit', 'roleSpendedEdit'));
    	$inputValidator->addFilter('*', new Zend_Filter_StringTrim());
    	$id = $inputValidator->getField('roleIdEdit');
    	$id->addFilter(new Zend_Filter_Int())
    		->addValidator(new Zend_Validate_Array_Required(), "")
    		->addValidator(new Zend_Validate_Int(), $this->trans->_("ID must be integer!"));
    	$name = $inputValidator->getField("rolenameEdit");
    	$name->addValidator(new Zend_Validate_Array_Required(), $this->trans->_("You must enter role name"), array('breakChain' => true))
    		->addValidator(new Zend_Validate_Alnum(), $this->trans->_("Role name must be made form letters and numbers only!"))
    		->addValidator(new Zend_Validate_StringLength(1, 30), $this->trans->_("Role name must have between 1 and 30 cagrachters long!"));
    	$description = $inputValidator->getField('roledescEdit');
    	$description->addValidator(new Zend_Validate_StringLength(0, 249), "Description can not have more then 249 charachters");
 
    	if($inputValidator->isValid($data)){
    		$this->logger->debug("It is ok!!!");
    	}
    	else{
    		$this->logger->info(print_r($inputValidator->getUserMessages(), true));
    	}

Case 28.12.2007.

    public function ajaxAddAction()
    {
    	require_once '../Core/lib/ZFext/Validate/Array.php';
    	require_once '../Core/lib/ZFext/Validate/Required.php';
    	require_once 'Zend/Filter/StringTrim.php';
    	require_once 'Zend/Validate/EmailAddress.php';
    	require_once 'Zend/Validate/Alnum.php';
	require_once 'Zend/Validate/StringLength.php';
	require_once '../applications/users/database/users.php';
	require_once '../Core/lib/ZFext/Validate/Equal.php';
	require_once '../Core/lib/UserAuth.php';
 
    	//this is my extension see /Core/lib/ZFext/Validate/Array.php
	$input = new Zend_Validate_Array(array("username", "password", "passwordr"));
    	$input->addFilter("username", new Zend_Filter_StringTrim())
    	      ->addValidate("username", new Required(), $this->trans->_("Morate unjeti korisničko ime."))
    	      ->addValidate("username", new Zend_Validate_Alnum(), $this->trans->_("Korisničko ime smije sadržavati samo brojeve i slova"))
    	      ->addValidate("username", new Zend_Validate_StringLength(4,8), $this->trans->_("Korisničko ime mora sadžavati minimalno 4 a maksimalno 8 znakova."))
    	      ->addValidate("password", new Required(), $this->trans->_("Morate unjeti lozinka."))
    	      ->addValidate("password", new Zend_Validate_Alnum(), $this->trans->_("Lozinka smije sadržavati samo brojeve i slova"))
    	      ->addValidate("password", new Zend_Validate_StringLength(4,8), $this->trans->_("Lozinka mora sadžavati minimalno 4 a maksimalno 8 znakova."))
    	      ->addValidate("passwordr", new Required(), $this->trans->_("Morate unjeti potvrdu passworda."))
    	      ->addValidate("password", new Equal($input, 'passwordr'), $this->trans->_("Pasvordi nisu jednaki."));
    	$status = null;
 
    	if($input->isValid($_POST)){
    		try{
    			$user_t= new Users_TUsers(array('db' => $this->db));
 
    			$user_t->insert(array(
    				'username' => $input->username,
    				'password' => UserAuth::hash_password($input->password)
    			));
    			$status = array("type" => "ok", "message" => $this->trans->_("Uspješno ste stvorili korisnika!"));
    		}
    		catch(Zend_Db_Exception $e){
    			$error_code = $e->getCode();
    			$message = $this->trans->_("Došlo je to pogreške pri radu s bazom (" . $error_code . "). Javite se administratoru!");
    			$db_message = $e->getMessage();
    			if(strpos($db_message, "users_username_key") !== FALSE){
    				$message = $this->trans->_("Već postoji korisnik s željenim korisničkim imenom. Odaberite drugo.");
    			}
    			//if($error_code)
    			$this->logger->err($e);
    			$status = array("type" => "error", "message" => $message);
    		}
    	}
    	else{
    		$messages = $input->getUserMessages();
    		$status = array(
    			"type" => "error",
    			"message" => $this->trans->_("Došlo je do pogrešaka prilikom kreiranja novog korisnika."),
    			"errors" => $messages
    		);
    	}
    	echo json_encode($status);
    }

Case 09.12.2007.

    public function ajaxListAction(){
    	require_once '../Core/lib/ZFext/Validate/Array.php';
    	require_once 'Zend/Filter/StringTrim.php';
    	require_once 'Zend/Validate/Alnum.php';
		require_once 'Zend/Validate/StringLength.php';
		require_once '../applications/users/database/users.php';
		require_once 'Zend/Validate/Digits.php';
 
    	$page = 1;
    	$limit = 25;
    	$filterField = "username";
    	$filter = null;
    	$sortField = "username";
    	$sortType = 0;
    	//set requested values
    	if(isset($_GET)){
    		$input = new Zend_Validate_Array(array("page", "limit", "filterField", "filter", "sortField", "sortType"));
    		$input->addFilter("*", new Zend_Filter_StringTrim())
    		      ->addValidate(array("page", "limit", "sortType"), new Zend_Validate_Digits(), $this->trans->_("Mora biti broj."))
    		      ->addValidate("filter", new Zend_Validate_StringLength(0, 30), $this->trans->_("filter smije sadržavati najviše 30 znakova."))
    		      ->addValidate(array( "filterField", "sortField"), new Zend_Validate_StringLength(0, 30), $this->trans->_("Smije sadržavati najviše 30 znakova."))
    		      ->addValidate(array("filterField", "sortField"), new Zend_Validate_Alnum(), $this->trans->_("Smije sadrzavati samo alfanumeričke znakove."));
    		if($input->isValid($_GET)){
    			if($input->page !== null)
    				$page = $input->page;
    			if($input->limit !== null)
    				$page = $inpur->limit;
    			if($input->filterField !== null)
    				$filterField = $input->filterField;	
    			if($input->filter !== null)
    				$filter = $input->filter;
    			if($input->sortField !== null)
    				$sortField = $input->sortField;
    			if($input->sortType !== null)
    				$sortType = $input->sortType;
    		}
    	}
    	try{
    		//db stuff ....
    	}
    	catch(Zend_Db_Exception $e){
    		//db error stuff ....
    	}
    }