Forms Zend表格注册
我是新来的。 我尝试在Zend中创建注册表。 我得到了一个表单数组,但它返回false 每次再见我都会回来。 我不知道为什么???很简单: 首先,在应用程序/表单下创建注册表单或使用zend工具Forms Zend表格注册,forms,zend-framework,Forms,Zend Framework,我是新来的。 我尝试在Zend中创建注册表。 我得到了一个表单数组,但它返回false 每次再见我都会回来。 我不知道为什么???很简单: 首先,在应用程序/表单下创建注册表单或使用zend工具 zf enable form zf create form registration 这将在application/forms下创建一个名为Registration.php的文件 class Application_Form_Registration extends Zend_Form {
zf enable form
zf create form registration
这将在application/forms下创建一个名为Registration.php的文件
class Application_Form_Registration extends Zend_Form
{
public function init()
{
$firstname = $this->createElement('text','firstname');
$firstname->setLabel('First Name:')
->setRequired(false);
$lastname = $this->createElement('text','lastname');
$lastname->setLabel('Last Name:')
->setRequired(false);
$email = $this->createElement('text','email');
$email->setLabel('Email: *')
->setRequired(false);
$username = $this->createElement('text','username');
$username->setLabel('Username: *')
->setRequired(true);
$password = $this->createElement('password','password');
$password->setLabel('Password: *')
->setRequired(true);
$confirmPassword = $this->createElement('password','confirmPassword');
$confirmPassword->setLabel('Confirm Password: *')
->setRequired(true);
$register = $this->createElement('submit','register');
$register->setLabel('Sign up')
->setIgnore(true);
$this->addElements(array(
$firstname,
$lastname,
$email,
$username,
$password,
$confirmPassword,
$register
));
}
}
这是一个简单的表单,只对需要的字段进行有限的验证
然后,必须使用相应的操作在视图中呈现窗体:
例如,在UserController中,添加名为
public function registerAction() {
//send the form to the view (register)
$userForm = new Application_Form_Registration();
$this->view->form = $userForm;
//check if the user entered data and submitted it
if ($this->getRequest()->isPost()) {
//check the form validation if not valid error message will appear under every required field
if ($userForm->isValid($this->getRequest()->getParams())) {
//send data to the model to store it
$userModel = new Application_Model_User();
$userId = $userModel->addUser($userForm->getValues());
}
}
}
最后两个步骤是在名为register的视图中呈现窗体
使用
并在模型中添加名为addUser的方法,以处理向数据库中插入数据的操作
<?= $this->form ?>