简体   繁体   English

使用Zend Framework接受动态创建的表单元素中的值

[英]Accepting values from dynamically created form elements with Zend Framework

If this question exists elsewhere on SO please enlighten me, was not having any luck in my searches. 如果这个问题在其他地方存在,请启发我,我的搜索没有任何运气。

The Dilemma: 困境:

I'm building an interface that will enable my wife to upload/manage the photos on her site. 我正在建立一个界面,使我的妻子可以上传/管理她网站上的照片。 To streamline the process - i built a component to duplicate the 'add a photo' form row wherein she would be able to add multiple images at once Like so: 为了简化流程-我构建了一个组件来复制“添加照片”表单行,在该行中,她可以一次添加多个图像,如下所示:

The mark up: (generated by Zend_Form) 标记:(由Zend_Form生成)

<section id="add-photos">
    <form enctype="multipart/form-data" action="/admin/photo/add-photos" method="post">
        <dl class="zend_form">
            <dt id="AddPhotos-label">&#160;</dt>
                <dd id="AddPhotos-element">
                    <fieldset id="fieldset-AddPhotos">
                       <dl> <-- This element is what get's duplicated on the 'add 
                                another image' button -->
                         <input type="hidden" name="id" value="" id="id" />
                         <dt id="image_name-label">
                             <label for="image_name" class="required">Image Name</label>
                         </dt>   
                         <dd id="image_name-element">
                             <input type="text" name="image_name" id="image_name"    
                                 value="" />
                         </dd>
                         <dt id="album_name-label">
                             <label for="album_name" class="required">Album Name</label> 
                         </dt>
                         <dd id="album_name-element">
                              <input type="text" name="album_name" id="album_name" 
                                  value="" />
                         </dd>
                         <dt id="category_name-label">
                              <label for="category_name" class="required">Category 
                                   Name</label>
                         </dt>
                         <dd id="category_name-element">
                               <input type="text" name="category_name"  
                                       id="category_name" value="" />
                         </dd>
                         <dt id="image-label">
                               <label for="image" class="optional">Image:</label>
                         </dt>
                         <dd>
                             <input type="hidden" name="MAX_FILE_SIZE" value="134217728" 
                                  id="MAX_FILE_SIZE" />
                             <input type="file" name="image" id="image" />
                         </dd>
                         <dt id="thumbnail-label">
                             <label for="thumbnail" class="optional">Is Thumbnail?
                             </label>       
                         </dt>
                         <dd id="thumbnail-element">
                             <input type="hidden" name="thumbnail" value="0" />
                             <input type="checkbox" name="thumbnail" id="thumbnail" 
                                 value="1" />
                         </dd>
                         <dt id="AddAnotherPhoto-label">&#160;</dt>
                         <dd id="AddAnotherPhoto-element">
                             <button name="AddAnotherPhoto" id="AddAnotherPhoto"  
                                 type="button">Add Another Photo</button>
                         </dd>
                     </dl>
                 </fieldset>
             </dd>
            <dt id="submit-label">&#160;</dt>
                <dd id="submit-element">
                    <input type="submit" name="submit" id="submit" value="Submit" />
                </dd>
            </dl>
        </form>     
</section>

If the user were to click on the 'AddAnotherPhoto' button, it would add another <dl> inside the <fieldset> . 如果用户单击“ AddAnotherPhoto”按钮,则会在<fieldset>添加另一个<dl> <fieldset> So the structure would become: 因此,结构将变为:

<fieldset>
    <dl></dl>
    <dl></dl>
</fieldset>

But, as you would expect with my controller action: 但是,正如您对我的控制器操作所期望的那样:

public function addPhotosAction()
{
    $form = new Form_AddPhoto();
    if($this->_request->isPost()){
        if($form->isValid($_POST)){
            $photo_model = new Admin_Model_Photo();

            $photo_model->addPhoto(
                $form->getValue('image_name'),
                $form->getValue('album_name'),
                $form->getValue('category_name'),
                $form->getValue('thumbnail')
            );
        }
    }
    $form->setAction('/admin/photo/add-photos');
    $this->view->form = $form;
}   

It's only capturing one set of elements. 它仅捕获一组元素。 So my questions is: how do I capture all incoming elements in a grouped fashion? 所以我的问题是:如何以分组方式捕获所有传入元素? Is there a way iterate through the request object and call the addPhoto method in the Photo Model for each set? 有没有一种方法可以遍历请求对象并为每个集合在照片模型中调用addPhoto方法?

I tried doing this in on the front-end through an $.ajax() where the iteration was easily executed, however the data was not so easy to pass to the action. 我尝试通过一个易于执行迭代的$.ajax()在前端进行此操作,但是将数据传递给操作并不那么容易。

Let me know if there are better ways architecturally to solve this - or perhaps other examples where this is being done. 让我知道是否在架构上有更好的方法可以解决此问题-也许还有其他示例可以解决此问题。

Thanks, Ken 谢谢,肯

I had a hard time explaining this so let me know if you need something clarified. 我很难解释这一点,所以请告诉我您是否需要澄清。

If i where ui would create a subform for each addphoto part. 如果我在哪里ui会为每个addphoto部分创建一个子窗体。 then if u copy it make sure u change the id which it will be grouped with. 然后,如果您复制它,请确保您更改将与之分组的ID。

With this subform u can then use the following function 在此子窗体中,您可以使用以下功能

$form->setElementsBelongTo(<uniqid>);

This makes it group the elements in the uniqid so u will get all post results. 这使它可以对uniqid中的元素进行分组,因此您将获得所有发布结果。

So your element will have a name that looks like name="test[image_name]" 因此,您的元素将具有一个类似于name =“ test [image_name]”的名称

In your controller u can then user $form->getValues() to get all values of your form then loop that data with a foreach loop 然后,在您的控制器中,您可以使用$ form-> getValues()获取表单的所有值,然后使用foreach循环将该数据循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM