3

ここでも簡単な質問があります。

ユーザーが選択できるボックスに最大値を設定するための既存のzendバリデーターはありますか?3つ以下のボックスを選択してほしい。

Webを検索したところ、form要素のisValid関数にエラーを設定するだけでした。しかし、選択したボックスごとにエラーが表示されるという問題が発生しました。(4回以上)または、この問題に対処する方法を知っている人はいますか?このエラーを1回だけ表示できれば、問題も解決します。

助けてくれてありがとう。

4

2 に答える 2

4

あなたは私のバリデーターを使うことができます、それは値の数に対してチェックします。私はまったく同じ目的で使用しました-複数選択で選択された値の最大数と最小数を検証するために:

<?php
class App_Validate_ValuesNumber extends Zend_Validate_Abstract
{
    const TOO_LESS = 'tooLess';
    const TOO_MUCH = 'tooMuch';

    protected $_type = null;
    protected $_val = null;

    /**
     * @var array
     */
    protected $_messageTemplates = array(
        self::TOO_LESS => "At least %num% values required",
        self::TOO_MUCH => "Not more then %num%  required",
    );

    /**
     * @var array
     */
    protected $_messageVariables = array(
        'num' => '_val'
    );
    /**
     * Constructor for the integer validator
     *
     * @param string $type Comparison type, that should be used
     *                     TOO_LESS means that value should be greater then items number
     *                     TOO_MUCH means opposite
     * @param int    $val  Value to compare items number with
     */
    public function __construct($type, $val)
    {
        $this->_type = $type;
        $this->_val = $val;
    }

    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if $value is a valid integer
     *
     * @param  string|integer $value
     * @return boolean
     */
    public function isValid($value)
    {
        // Value shoul dbe greated
        if ($this->_type == self::TOO_LESS) {
            if (count($value) < $this->_val) {
                $this->_error(self::TOO_LESS);
                return false;
            }
        }

        // Value should be less
        if ($this->_type == self::TOO_MUCH) {
            if (count($value) > $this->_val) {
                $this->_error(self::TOO_MUCH);
                return false;
            }
        }
        return true;
    }
}
于 2011-11-07T20:11:35.230 に答える
1

今日はこれと戦った。これはzendのバグです。http://framework.zend.com/issues/browse/ZF-11667。この問題には修正の相違がありますが、1.12がリリースされるまで修正されません。待ちたくなかったので、Zend_Form_Elementにパッチを適用しました。修正はうまく機能します。修正する前は、MultiChecksのエラーメッセージがチェックされたボックスごとに1回繰り返されていました。

于 2012-06-01T21:11:47.447 に答える