validation - Zend_Validate_Between on Another Value -


what best way validate number range based on value of form element? if user selects "percentage" discount type, discount amount should between 0 , 100, , not 140! problem seems passing in form element value.

also, i've viewed other resources, 1 dealing similar topic, perhaps not way relevant. how validate field of zend_form based on value of field?

form.php

$isvalid = new application_model_validate(); $discount = $this->createelement('text', 'discount')                  ->setlabel('discount amount')                   ->setdescription("enter amount in format \"200.00\" ")                  ->setrequired(true)                  ->setdecorators(array('description', 'viewhelper', 'errors',                            array('htmltag', array('tag' => 'dd')),                            array('label', array('tag' => 'dt'))));  $discount->addvalidators(array(array('float')), $isvalid->isvalid(new zend_validate_between(array('min' => '0', 'max' => '100')), $discounttype)); $this->addelement($discount); 

application_model_validate.php

require_once 'zend/validate/abstract.php';  class application_model_validate extends zend_validate_abstract { /*  *  validation failure message key  */ const invalid_percentage = 'invalidpercentage';  /*  * validation failure message template definitions  */ protected $_messagetemplates = array(   self::invalid_percentage => 'please enter percentage greater 0 , 100.'  );   protected $_percentageoption; protected $_percentagevalue;  /*  * defined zend_validate_interface  * validate percentage parameters  */ public function isvalid($value, $context = null) {     $this->_setvalue($value);       /*      * if context key valid, return true      */      if(is_array($context))     {        if (isset($context['percentage']) && ($value))         {            return true;                         }     }      $this->_error(self::invalid_percentage);     return false;   } 

if need anymore information, say.

i modified code bit , added drop down box:

form:

$this->addelement('select', 'discounttype'); $this->getelement('discounttype')     ->addmultioptions(         array('percentage' => 'percentage', 'other' => 'other') );  $discount = $this->createelement('text', 'discount')          ->setlabel('discount amount')           ->setrequired(true);  $discount->addvalidators(     array('float', new application_model_validate(0, 140)) ); $this->addelement($discount); 

validator:

<?php  class application_model_validate extends zend_validate_between {     public function isvalid($value, $context = null)     {         $this->_setvalue($value);          if ($context['discounttype'] == 'percentage') {             $this->setmax(100);         }          return parent::isvalid($value, $context);     } } 

now, if validate form in controller using $form->isvalid($this->getrequest()->getparams()), take input between 0 , 100 if in drop down box 'percentage' selected , input between 0 , 140 otherwise.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -