zend form表单校验失效

gezp 2015-10-13 11:19:20
问题1:提交时校验没有反映
问题2:不知道返回按钮怎么写
以下是代码
<?php

class Oi_Form_User extends Zend_Dojo_Form
{

public function init()
{
/* Form Elements & Other Definitions Here ... */
$this->setMethod('post');

// 登录ID
$username = $this->createElement('TextBox','username');
$username->setLabel('登录名称:');
$username->setRequired(true);
$username->addValidator('stringLength',false,array(5,20));
$username->addErrorMessage('请输入5个以上字母或2~6个以上汉字!');
$this->addElement($username);

// 密码
$pwd = $this->createElement('PasswordTextBox','password');
$pwd->setLabel('密码:');
$pwd->setRequired(true);
$pwd->addValidator('stringLength',false,array(6));
$pwd->addErrorMessage('密码至少有6个字符');
$this->addElement($pwd);

//确认密码
$pwd2 = $this->createElement('PasswordTextBox','password2');
$pwd2->setLabel('确认密码:');
$pwd2->setRequired(true);
$pwd2->addValidator('identical',false,array('token'=>'password'));
$pwd2->addErrorMessage('两次密码不一致!');
$this->addElement($pwd2);

// 性别
$sex = $this->createElement('radio','sex');
$sex->setLabel('性别:');
$sex->addMultiOptions(array('0'=>'女','1'=>'男'));
$this->addElement($sex);

// 生日
$birthday = $this->createElement('DateTextBox','birthday');
$birthday->setLabel('生日:');
$birthday->setRequired(true);
$this->addElement($birthday);

// 是否结婚
$marriage = $this->createElement('Radio','marriage');
$marriage->setLabel('婚姻状况:');
$marriage->addMultiOptions(array('1'=>'是','0'=>'否'));
$this->addElement($marriage);

// 住址
$address = $this->createElement('TextBox','address');
$address->setLabel('住址:');
$address->setRequired(true);
$address->addValidator('stringLength',false,array(5));
$address->addErrorMessage('住址不能为空!');
$this->addElement($address);

// QQ
$qq = $this->createElement('TextBox','qq');
$qq->setLabel('QQ:');
$qq->setRequired(true);
$qq->addValidator('int',false,array(5));
$qq->addErrorMessage('请输入至少5个有效数字!');
$this->addElement($qq);

// 角色
$role = $this->createElement('RadioButton','role');
$role->setLabel('角色:');
$role->addMultiOptions(array('admin'=>'管理员','order'=>'订餐'));
$this->addElement($role);

// 手机
$telephone = $this->createElement('TextBox','telephone');
$telephone->setLabel('手机:');
$telephone->setRequired(true);
$telephone->addValidator('int',false,array(11));
$telephone->addErrorMessage('请输入11位手机号码');
$this->addElement($telephone);

// 座机
$phone = $this->createElement('TextBox','phone');
$phone->setLabel('座机:');
$phone->setRequired(true);
$phone->addValidator('int',false,array(7));
$phone->addErrorMessage('请输入电话号码!');
$this->addElement($phone);

$submit = $this->createElement('SubmitButton','提交');
$this->addElement($submit);

}

controller 如下
public function registerAction()
{
// 初始化Form
$formUser = new Oi_Form_User();
$formUser->removeElement('role');

if($this->getRequest()->isPost()){
if($formUser->isValid($_POST)){
$data = $formUser->getValues();
// 初始化model
$modelUser = new Oi_Model_User();
$userId = $modelUser->createUser($data);
if($userId){
$this->_redirect('/user/');
}
}
}

$this->view->formUser = $formUser;
}
...全文
101 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
gezp 2015-10-13
  • 打赏
  • 举报
回复
第一个问题已经解决,是数字校验的问题,把addValidator('int',false,array(7));中的int字符串换成new Zend_Validate_Int()就好了.但是还是不知道为什么用int字符串不行.
第二个问题有没有人知道阿.我刚接触zend没多长时间,请大家指教

4,251

社区成员

发帖
与我相关
我的任务
社区描述
国内外优秀PHP框架讨论学习
社区管理员
  • Framework
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧