Note that there are some explanatory texts on larger screens.

plurals
  1. POZend_Validate_Between on Another Value
    primarykey
    data
    text
    <p>What is the best way to validate a number range based on the value of another form element? If the user selects "percentage" as a discount type, the discount amount should be between 0 and 100, and not 140! The problem seems to be passing in another form element value.</p> <p>Also, I've viewed other resources, one dealing with a similar topic, but perhaps not all the way relevant. <a href="https://stackoverflow.com/questions/7585232/how-to-validate-a-field-of-zend-form-based-on-the-value-of-another-field">How to validate a field of Zend_Form based on the value of another field?</a></p> <p>Form.php</p> <pre><code>$isValid = new Application_Model_Validate(); $discount = $this-&gt;createElement('text', 'discount') -&gt;setLabel('Discount Amount') -&gt;setDescription("Enter an amount in the format \"200.00\" ") -&gt;setRequired(true) -&gt;setDecorators(array('Description', 'ViewHelper', 'Errors', array('HTMLTag', array('tag' =&gt; 'dd')), array('Label', array('tag' =&gt; 'dt')))); $discount-&gt;addValidators(array(array('Float')), $isValid-&gt;isValid(new Zend_Validate_Between(array('min' =&gt; '0', 'max' =&gt; '100')), $discountType)); $this-&gt;addElement($discount); </code></pre> <p>Application_Model_Validate.php</p> <pre><code>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 =&gt; 'Please enter a percentage greater than 0 and up to 100.' ); protected $_percentageOption; protected $_percentageValue; /* * Defined by Zend_Validate_Interface * Validate the percentage parameters */ public function isValid($value, $context = null) { $this-&gt;_setValue($value); /* * If context key is valid, return true */ if(is_array($context)) { if (isset($context['percentage']) &amp;&amp; ($value)) { return true; } } $this-&gt;_error(self::INVALID_PERCENTAGE); return false; } </code></pre> <p>If you need anymore information, just say. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload