Zend_Form 元素的命名
我想自动为我的 Zend_Form 元素添加前缀,以便在发布表单时可以更轻松地将它们映射到我的模型上。例如,我有一个包含与两个模型相关的元素的表单。
我创建的表单如下:
$this->setMethod('post');
$this->addElement('text', 'name', array(
'label' => 'Your Name',
'required' => true,
));
$this->addElement('text', 'tel', array(
'label' => 'Your Telephone',
'required' => true,
));
$this->addElement('text', 'email', array(
'label' => 'Your Email Address',
'required' => true,
'filters' => array('StringTrim','StringToLower'),
'validators' => array('EmailAddress'),
));
$this->addElement('password', 'password', array(
'label' => 'Your Password',
'required' => true,
));
$this->addElement('text', 'surgery_name', array(
'label' => 'Surgery Name',
'required' => true,
));
问题是“手术名称”字段需要手动添加“手术_”前缀。我理想的做法是为第一组字段(例如“用户”)设置前缀,然后为第二组字段(例如“手术”)设置前缀。
那么我的元素名称将类似于:
User.Name 用户邮箱 User.TelSurgery.Name
等
然后,当我将它们映射到我的模型时,
应该更容易找出要映射的 FORM 字段。
I want to auto-prefix my Zend_Form elements so that I can map them more easily onto my models when I POST the form. For example, I have a single form with elements that pertain to two models.
I create the form like so:
$this->setMethod('post');
$this->addElement('text', 'name', array(
'label' => 'Your Name',
'required' => true,
));
$this->addElement('text', 'tel', array(
'label' => 'Your Telephone',
'required' => true,
));
$this->addElement('text', 'email', array(
'label' => 'Your Email Address',
'required' => true,
'filters' => array('StringTrim','StringToLower'),
'validators' => array('EmailAddress'),
));
$this->addElement('password', 'password', array(
'label' => 'Your Password',
'required' => true,
));
$this->addElement('text', 'surgery_name', array(
'label' => 'Surgery Name',
'required' => true,
));
The problem is that the Surgery Name field needs to be manually prefixed with surgery_. What I'd ideally like to do is set a prefix for the first set of fields (say User), and then set a prefix for the second set of fields (say Surgery).
Then my element names would look something like:
User.Name
User.Email
User.Tel
Surgery.Name
etc
Then when I come to map them to my model it should be easier to work out which FORM fields to map.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是子表单的设计目的。因此,在您的情况下,您会这样做:
表单元素将被命名为:
user[name]
user[tel]
surgery[name]
和等等,这样就很容易把数据拉出来了。您还可以独立验证表单,将它们分成独立的类(这样您就可以将它们包含在其他表单中),以及一系列其他内容。手册中的更多信息: http:// /framework.zend.com/manual/en/zend.form.forms.html#zend.form.forms.subforms
This is what sub-forms were designed for. So in your case you'd do:
the form elements will be named:
user[name]
user[tel]
surgery[name]
and so on, so it is then easy to pull the data out. You can also validate the forms independently, split them out into standalone classes (so you could include them in other forms), and a whole host of other things.More info in the manual: http://framework.zend.com/manual/en/zend.form.forms.html#zend.form.forms.subforms
为了帮助我从表单转换为模型的能力,我做的一件事是执行以下一项或两项操作
从一个表单到另一个表单保持一致(如果我有多个可以编辑的表单)
由于某种原因相同的模型)并使用它返回模型对象
并正确映射它以及
toArray
或toForm
方法将吐出可使用的关联数组中的值
相似地
One thing I do to aid in my ability to go from form to model is to do one or both of the following
consistently from form to form (if I've got multiple forms that edit
the same model for some reason) and use it to return the model object
and map it correctly as well as a
toArray
ortoForm
method thatwill spit out the values in an associative array that can be used
similarly