我们应该如何使用zend view helper form和validators + filters?

时间:2010-07-06 07:43:00

标签: zend-framework zend-form zend-view

我们应该如何使用zend view helper form和validators + filters?

示例错过了验证码+过滤器:http://framework.zend.com/manual/en/zend.view.helpers.html

<form action="action.php" method="post">
 <p>
  <label>Your Email:
   <?php echo $this->formText('email', 'you@example.com', array('size' => 32)) ?>
  </label>
 </p>
 <p>
  <label>Your Country:    
   <?php echo $this->formSelect('country', 'us', null, $this->countries) ?>    
  </label>
 </p>
 <p>
  <label>Would you like to opt in?
   <?php echo $this->formCheckbox('opt_in', 'yes', null, array('yes', 'no')) ?>
  </label>
 </p>
</form>

谢谢,

1 个答案:

答案 0 :(得分:0)

将表单创建为单独的类,然后您可以使用所需的所有验证器和过滤器。文档中有完整的设置信息:

http://framework.zend.com/manual/en/zend.form.quickstart.html

docs中的示例:

$form = new Zend_Form();
$form->setAction('/user/login')
     ->setMethod('post');

// Create and configure username element:
$username = $form->createElement('text', 'username');
$username->addValidator('alnum')
         ->addValidator('regex', false, array('/^[a-z]+/'))
         ->addValidator('stringLength', false, array(6, 20))
         ->setRequired(true)
         ->addFilter('StringToLower');

// Create and configure password element:
$password = $form->createElement('password', 'password');
$password->addValidator('StringLength', false, array(6))
         ->setRequired(true);

// Add elements to form:
$form->addElement($username)
     ->addElement($password)
     // use addElement() as a factory to create 'Login' button:
     ->addElement('submit', 'login', array('label' => 'Login'));