1

喂..

如何使用 ini 文件定义对 zend 表单的验证。

4

1 回答 1

1

在 application.ini (或你有什么)

form.test.title[] = NotEmpty
form.test.title.Regexp.validator = Regex
form.test.title.Regexp.breakChainOnFailure = false
form.test.title.Regexp.options = /\W/

form.test.name[] = NotEmpty
form.test.name[] = Alnum

然后在 PHP 代码中:

/* The experimental form */
        $form = new Zend_Form ();
        $form->addElement('text', 'title', array ('label' => 'test1'));
        $form->addElement('text', 'name', array ('label' => 'test2'));
        $form->addElement('submit', 'submit');

/* Zend_Registry::get('config') is where I keep my application.ini after it has been parsed in the Bootstrap */
        $config = Zend_Registry::get('config')->form->test;

        foreach ($config as $index => $value)
        {


    if ($value instanceof Zend_Config)
        {
            foreach ($value as $validator)
            {
                if (is_string ($validator))
                {
                    $form->$index->addValidator ($validator);
                }
                else
                {
                    $form->$index->addValidators (array ($validator->toArray ()));
                }
            }
        }
        else
        {
            $form->$index->addValidator ($value);
        }
    }
于 2011-03-07T18:26:43.450 回答