使用ini文件将验证添加到zend表单

时间:2011-03-07 15:09:55

标签: zend-framework zend-form zend-controller

斐伊川..

请问我是否使用ini文件为zend表单定义了验证。

1 个答案:

答案 0 :(得分: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);
        }
    }