开发者

Bulk user creation in Zend Framework. Already have single user creation for working

I am using Zend Framework I have a form that allows me to create a user. It has all of the necessary validation and it works well.

I was tasked开发者_运维问答 with creating a single page that allows me to create up to 25 new users with a single submit.

example:

  • salutation / first name / last name / email
  • salutation / first name / last name / email
  • salutation / first name / last name / email
  • salutation / first name / last name / email

How can I leverage the form I created for a single user and allow for each new user to get validated separately?

Is there some way to have an array of forms or something else that will achieve the same thing?


Try something like this (untested):

class BulkUserForm extends Zend_Form
{
    private $_howMany = 1;

    public function __construct( $howMany, $options )
    {
        $this->_howMany = (int) $howMany;
        parent::__construct( $options );
    }

    public function init()
    {
        for( $i = 1; $i <= $this->_howMany; $i++ )
        {
            $userForm = new YourOriginalUserForm();
            $userForm->setElementsBelongTo( 'user' . $i ); // not sure this is actually necessary anymore, because of the second param to addSubForm
            $this->addSubForm( $userForm, 'user' . $i, $i );
        }
    }
}

Usage:

$form = new BulkUserForm( 25 /* [, $options ] */ );
echo $form;

// validation:
$form->isValid( $this->getRequest()->getPost() ); // or something similar

Explanation:

The setElementsBelongTo() call on subforms should create notations similar to this (simplified of course):

<input type="radio" name="user1[salutation]">
<input type="text" name="user1[firstName]">
<input type="text" name="user1[lastName]">
<input type="text" name="user1[email]">

You form should be able to automatically recognize the submitted POST values when validating.

Of course you should probably also look into the laying out / decorating of the sub forms, but I'll leave that up to you as an exercise. ;-)

EDIT
Sorry, the second param to addSubForm() is mandatory (added it now, and also added the optional $order param). I believe it should also automatically set the sub form to belong to the correct namespace already. Not completely sure about that though. See what happens if you leave out the setElementsBelongTo() call on the sub form.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜