Created
November 30, 2009 12:56
-
-
Save nebiros/245430 to your computer and use it in GitHub Desktop.
Zend_Filter_Input on steroids
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class IndexController extends Zend_Controller_Action { | |
public function someAction() { | |
if ( true === $this->getRequest()->isPost() ) { | |
// Validate form. | |
$someForm = new App_Filter_Input_SomeForm(); | |
$someForm->setData( $this->getRequest()->getPost() ); | |
if ( false === $someForm->isValid() ) { | |
if ( true === $someForm->hasInvalid() || true === $someForm->hasMissing() ) { | |
$this->view->errors = $someForm->getMergedMessages(); | |
} | |
return $this->render(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* App/Filter/Input | |
* | |
* @author jfalvarez | |
*/ | |
class App_Filter_Input extends Zend_Filter_Input { | |
const APPEND = "append"; | |
const PREPEND = "prepend"; | |
const SEPARATOR = ", "; | |
/** | |
* | |
* @var array | |
*/ | |
protected $_initials = array(); | |
/** | |
* | |
* @var array | |
*/ | |
protected $_mergedMessages = array(); | |
/** | |
* | |
* @var array | |
*/ | |
protected $_messagesPerLine = array(); | |
/** | |
* | |
* @param array $initials | |
* @param array $filterRules | |
* @param array $validatorRules | |
* @param array $data | |
* @param array $options | |
*/ | |
public function __construct( Array $initials = null, Array $filterRules = null, Array $validatorRules = null, Array $data = null, Array $options = null ) { | |
if ( null !== $initials ) { | |
$this->_initials = $initials; | |
} | |
$this->init(); | |
if ( null === $filterRules ) { | |
$filterRules = $this->getFilters(); | |
} | |
if ( null === $validatorRules ) { | |
$validatorRules = $this->getValidators(); | |
} | |
parent::__construct( $filterRules, $validatorRules, $data, $options ); | |
} | |
/** | |
* Used by extending classes. | |
* | |
* @return void | |
*/ | |
public function init() {} | |
/** | |
* Set option. | |
* | |
* @param mixed $key | |
* @param mixed $value | |
* @return App_Filter_Input | |
*/ | |
public function setInitial( $key, $value = null ) { | |
$this->_initials[$key] = $value; | |
return $this; | |
} | |
/** | |
* Get init option. | |
* | |
* @param mixed $key | |
* @param null|mixed $default | |
* @return mixed | |
*/ | |
public function getInitial( $key, $default = null ) { | |
if ( true === isset( $this->_initials[$key] ) ) { | |
return $this->_initials[$key]; | |
} | |
return $default; | |
} | |
/** | |
* | |
* @param array $initials | |
* @return App_Filter_Input | |
*/ | |
public function setInitials( Array $initials ) { | |
$this->_initials = $initials; | |
return $this; | |
} | |
/** | |
* | |
* @return array | |
*/ | |
public function getInitials() { | |
return $this->_initials; | |
} | |
/** | |
* | |
* @param array $initials | |
* @return App_Filter_Input | |
*/ | |
public function addInitials( Array $initials ) { | |
$this->_initials = $this->_initials + $initials; | |
return $this; | |
} | |
/** | |
* Reset initial options. | |
* | |
* @return App_Filter_Input | |
*/ | |
public function clearInitials() { | |
$this->_initials = array(); | |
return $this; | |
} | |
/** | |
* | |
* @param array $filters | |
* @return App_Filter_Input | |
*/ | |
public function setFilters( Array $filters ) { | |
$this->_filterRules = $filters; | |
return $this; | |
} | |
/** | |
* | |
* @return array | |
*/ | |
public function getFilters() { | |
return $this->_filterRules; | |
} | |
/** | |
* | |
* @param array $filters | |
* @return App_Filter_Input | |
*/ | |
public function addFilters( Array $filters ) { | |
$this->_filterRules = $this->_filterRules + $filters; | |
return $this; | |
} | |
/** | |
* | |
* @param array $validators | |
* @return App_Filter_Input | |
*/ | |
public function setValidators( Array $validators ) { | |
$this->_validatorRules = $validators; | |
return $this; | |
} | |
/** | |
* | |
* @return array | |
*/ | |
public function getValidators() { | |
return $this->_validatorRules; | |
} | |
/** | |
* | |
* @param array $validators | |
* @return App_Filter_Input | |
*/ | |
public function addValidators( Array $validators ) { | |
$this->_validatorRules = $this->_validatorRules + $validators; | |
return $this; | |
} | |
/** | |
* | |
* @return array | |
*/ | |
public function getData() { | |
return $this->_data; | |
} | |
/** | |
* | |
* @return array | |
*/ | |
public function getMergedMessages() { | |
$messages = $this->getMessages(); | |
if ( true === empty( $messages ) ) { | |
return $this->_mergedMessages; | |
} | |
foreach ( $messages as $field => $rules ) { | |
foreach ( $rules as $message ) { | |
$this->_mergedMessages[] = $message; | |
} | |
} | |
return $this->_mergedMessages; | |
} | |
/** | |
* | |
* @param array $options | |
* @return string | |
*/ | |
public function getLineMessages( Array $options = array() ) { | |
$messages = $this->getMessages(); | |
if ( true === empty( $messages ) ) { | |
return null; | |
} | |
if ( true === empty( $options["text"] ) ) { | |
$options["text"] = null; | |
} | |
if ( true === empty( $options["textPosition"] ) ) { | |
$options["textPosition"] = self::PREPEND; | |
} | |
if ( true === empty( $options["messageSeparator"] ) ) { | |
$options["messageSeparator"] = self::SEPARATOR; | |
} | |
$fieldMessages = array(); | |
foreach ( $messages as $field => $rules ) { | |
$fieldMessages[] = implode( $options["messageSeparator"], array_values( $rules ) ); | |
} | |
$message = implode( $options["messageSeparator"], $fieldMessages ); | |
if ( null !== $options["text"] ) { | |
switch ( $options["textPosition"] ) { | |
case self::APPEND: | |
$message = $message . " " . $options["text"]; | |
break; | |
case self::PREPEND: | |
default: | |
$message = $options["text"] . " " . $message; | |
break; | |
} | |
} | |
return $message; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* App/Filter/Input/SomeForm | |
* | |
* @author jfalvarez | |
*/ | |
class App_Filter_Input_SomeForm extends App_Filter_Input { | |
public function init() { | |
$filters = array( | |
"*" => array( "StringTrim", "StripTags" ) | |
); | |
$this->setFilters( $filters ); | |
$validators = array( | |
"field1" => array( | |
"Alpha", | |
"NotEmpty", | |
Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED, | |
Zend_Filter_Input::MESSAGES => array( | |
0 => "Just alpha chars", | |
1 => "This field is required" | |
) | |
), | |
"field2" => array( | |
"Float", | |
"NotEmpty", | |
Zend_Filter_Input::PRESENCE => Zend_Filter_Input::PRESENCE_REQUIRED, | |
Zend_Filter_Input::MESSAGES => array( | |
0 => "Just alpha chars", | |
1 => "This field is required" | |
), | |
), | |
"field3" => array( | |
array( "Regex", "/^[a-zA-ZáéíóúÁÉÍÓÚñÑ\s]+$/" ), | |
"NotEmpty", | |
Zend_Filter_Input::MESSAGES => array( | |
0 => "Just alpha chars", | |
1 => "This field is required" | |
) | |
), | |
); | |
$this->setValidators( $validators ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment