0

別のフォーム要素の値に基づいて数値範囲を検証する最良の方法は何ですか? ユーザーが割引タイプとして「パーセンテージ」を選択した場合、割引額は 140 ではなく 0 から 100 の間である必要があります。問題は、別のフォーム要素の値を渡しているようです。

また、同様のトピックを扱っている他のリソースも見ましたが、おそらく関連性があまりありません。 別のフィールドの値に基づいて Zend_Form のフィールドを検証する方法は?

Form.php

$isValid = new Application_Model_Validate();
$discount = $this->createElement('text', 'discount')
                 ->setLabel('Discount Amount') 
                 ->setDescription("Enter an amount in the 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 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->_setValue($value);

     /*
     * If context key is valid, return true
     */

    if(is_array($context))
    {
       if (isset($context['percentage']) && ($value))
        {
           return true;                
        }
    }

    $this->_error(self::INVALID_PERCENTAGE);
    return false;

 }

さらに情報が必要な場合は、言ってください。

4

2 に答える 2

0

コードを少し変更し、ドロップダウン ボックスを追加しました。

形:

$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);

バリデーター:

<?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);
    }
}

ここで、 を使用してコントローラーでフォームを検証する$form->isValid($this->getRequest()->getParams())と、ドロップダウン ボックスで「パーセンテージ」が選択されている場合は 0 から 100 の間の入力を受け取り、それ以外の場合は 0 から 140 の間の入力を受け取ります。

于 2012-06-04T21:46:44.943 に答える
0

別の解決策:

//controller validation
$discountValidate = new Application_Model_Validate();
$discountValidate->_checkDiscount($data['discountType'], $data['discount']);

if ($discountValidate->isValid())
{
    //do
}

else
{
     $element = $form->getElement('discount');
     $element->addError($discountValidate->getError());
}
于 2012-06-26T01:05:59.930 に答える