【问题标题】:zend2 required message change inside FilterInput::isValid()zend2 需要在 FilterInput::isValid() 中更改消息
【发布时间】:2013-11-18 13:34:40
【问题描述】:

我正在尝试更改 InputFilter::isValid() 方法中所需的消息。但我不知道如何为一个字段更改此特定消息。这是我的代码:

class EventFormInputFilter extends InputFilter
{
    protected $dbAdapter;
    protected $isEdit = false;


    public function __construct(Adapter $dbAdapter, $isEdit = false)
    {
        $factory = $this->getFactory();
        $this->dbAdapter = $dbAdapter;
        $this->isEdit = $isEdit;

        $this->add($factory->createInput(
            [
                'name' => 'market_id',
                'required' => false,
                'allow_empty' => false,
                'filters' => [
                    ['name' => 'Null'],
                ],
                'validators' => [
                    [
                        'name' => '\AssetBase\Validator\Db\RecordExists',
                        'options' => [
                            'field' => 'id',
                            'table' => 'markets',
                            'adapter' => $dbAdapter,
                        ],
                    ],
                ],
            ]
        ));

        $this->add($factory->createInput(
            [
                'name' => 'country_id',
                'required' => false,
                'allow_empty' => false,
                'filters' => [
                    ['name' => 'Null'],
                ],
                'validators' => [
                    [
                        'name' => '\AssetBase\Validator\Db\RecordExists',
                        'options' => [
                            'field' => 'id',
                            'table' => 'countries',
                            'adapter' => $dbAdapter,
                        ],
                    ],
                ]
            ]
        ));
    }

    public function isValid()
    {
        $marketId = $this->getValue('market_id');
        $countryId = $this->getValue('country_id');
        if (empty($marketId) && empty($countryId)) {
            $this->get('market_id')->setRequired(true);
            // how to change required error message here?
            $this->get('country_id')->setRequired(true);
            // how to change required error message here?
        }
        return parent::isValid();
    }
}

有什么想法吗?谢谢

【问题讨论】:

    标签: forms validation input zend-framework2


    【解决方案1】:

    您可以更改表单中所需的信息,如下所示。

    $inputFilter = new InputFilter();
    $factory = new InputFactory();
    

    创建 InputFilter 对象并应用如下验证。

        $inputFilter->add($factory->createInput(array(
            'name' => 'country_id',
            'required' => true,
            'filters' => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name' => 'NotEmpty',
                    'options' => array(
                        'messages' => array(
                            'isEmpty' => 'Country id is required.',
                        )
                    ),
                ),
            ),
        )));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      相关资源
      最近更新 更多