zend_form 基础使用方法示例
By admin
- One minute read - 137 wordspublic function indexAction()
{
$form = new Zend_Form();
if ($form->isValid($_POST)) {
// success!
$values = $form->getValues();
} else {
// failure
// deubg
$this->view->form = $form;
return $this->render('form');
}
$form->setAction('/members/index/register')
->setMethod('post');
$form->setAttrib('id', 'login');
// create and confirure username element
//$username = new Zend_Form_Element_Text('username');
$username = $form->createElement('text', 'username');
$username->addValidator('alnum');
$username->addValidator('regex', false, array('/^[a-z]/i'));
$username->setRequired(true);
$username->setFilters(array('StringtoLower','StringTrim'));
$username->setOrder(1);
//create and configure password element
$password = $form->createElement('password', 'password');
$password->addValidator('StringLength', false, array(6))
->setRequired(true)
->setOrder(2);
$form->addElement($username);
$form->addElement($password);
$form->addElement('submit', 'login', array('label' => 'login', 'order' => 3));
$this->view->form = $form;
/**
* 视图里的调用方法 form ?>
*
*/
/*
$username = new Zend_Form_Element_Textr('username');
$username->addValidator('alnum')
->addValidator('regex', false, array(('/^[a-z]/i')))
->setRequired(true)
->setFilters('StringtoLower');
//==========================================
$form->addElement('text', 'username', array(
'validators' => array(
'alnum',
array('regex', false, '/^[a-z]/i')
),
'required' => true,
'filters' => array('StringToLower'),
));
*/
//$form->render($view);
// Passing a form element type to the form object:
//$form->addElement('text', 'username');
//$form->addElemnet('password', 'password');
}
Zend_Form中的元数据和属性 :