开发者

Naming of Zend_Form elements

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('Stri开发者_Python百科ngTrim','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.


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜