Zend_Form 元素的命名

发布于 2024-11-26 15:01:59 字数 1169 浏览 0 评论 0原文

我想自动为我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把回忆走一遍 2024-12-03 15:01:59

这就是子表单的设计目的。因此,在您的情况下,您会这样做:

$this->setMethod('post');

$user = new Zend_Form_SubForm();

$user->addElement('text', 'name', array(
    'label'     => 'Your Name',
    'required'  => true,
));

$user->addElement('text', 'tel', array(
    'label'     => 'Your Telephone',
    'required'  => true,
));     

$user->addElement('text', 'email', array(
    'label'     => 'Your Email Address',
    'required'  => true,
    'filters'   => array('StringTrim','StringToLower'),
    'validators'    => array('EmailAddress'),
));

$user->addElement('password', 'password', array(
    'label'     => 'Your Password',
    'required'  => true,
));

$this->addSubForm($user, 'user');


$surgery = new Zend_Form_SubForm();

$surgery->addElement('text', 'name', array(
    'label'     => 'Surgery Name',
    'required'  => true,
));

$this->addSubForm($surgery, 'surgery');

表单元素将被命名为: 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:

$this->setMethod('post');

$user = new Zend_Form_SubForm();

$user->addElement('text', 'name', array(
    'label'     => 'Your Name',
    'required'  => true,
));

$user->addElement('text', 'tel', array(
    'label'     => 'Your Telephone',
    'required'  => true,
));     

$user->addElement('text', 'email', array(
    'label'     => 'Your Email Address',
    'required'  => true,
    'filters'   => array('StringTrim','StringToLower'),
    'validators'    => array('EmailAddress'),
));

$user->addElement('password', 'password', array(
    'label'     => 'Your Password',
    'required'  => true,
));

$this->addSubForm($user, 'user');


$surgery = new Zend_Form_SubForm();

$surgery->addElement('text', 'name', array(
    'label'     => 'Surgery Name',
    'required'  => true,
));

$this->addSubForm($surgery, 'surgery');

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

梦一生花开无言 2024-12-03 15:01:59

为了帮助我从表单转换为模型的能力,我做的一件事是执行以下一项或两项操作

  • 创建一个函数,用于在表单和名称元素之间进行转换
    从一个表单到另一个表单保持一致(如果我有多个可以编辑的表单)
    由于某种原因相同的模型)并使用它返回模型对象
  • 创建将使用 post 数组的构造函数或模型方法
    并正确映射它以及 toArraytoForm 方法
    将吐出可使用的关联数组中的值
    相似地

One thing I do to aid in my ability to go from form to model is to do one or both of the following

  • Create a function for converting to/from forms and name elements the
    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
  • Create a constructor or model method that will consume the post array
    and map it correctly as well as a toArray or toForm method that
    will spit out the values in an associative array that can be used
    similarly
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文