簡體   English   中英

使用Zend Framework接受動態創建的表單元素中的值

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

如果這個問題在其他地方存在,請啟發我,我的搜索沒有任何運氣。

困境:

我正在建立一個界面,使我的妻子可以上傳/管理她網站上的照片。 為了簡化流程-我構建了一個組件來復制“添加照片”表單行,在該行中,她可以一次添加多個圖像,如下所示:

標記:(由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>

如果用戶單擊“ AddAnotherPhoto”按鈕,則會在<fieldset>添加另一個<dl> <fieldset> 因此,結構將變為:

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

但是,正如您對我的控制器操作所期望的那樣:

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;
}   

它僅捕獲一組元素。 所以我的問題是:如何以分組方式捕獲所有傳入元素? 有沒有一種方法可以遍歷請求對象並為每個集合在照片模型中調用addPhoto方法?

我嘗試通過一個易於執行迭代的$.ajax()在前端進行此操作,但是將數據傳遞給操作並不那么容易。

讓我知道是否在架構上有更好的方法可以解決此問題-也許還有其他示例可以解決此問題。

謝謝,肯

我很難解釋這一點,所以請告訴我您是否需要澄清。

如果我在哪里ui會為每個addphoto部分創建一個子窗體。 然后,如果您復制它,請確保您更改將與之分組的ID。

在此子窗體中,您可以使用以下功能

$form->setElementsBelongTo(<uniqid>);

這使它可以對uniqid中的元素進行分組,因此您將獲得所有發布結果。

因此,您的元素將具有一個類似於name =“ test [image_name]”的名稱

然后,在您的控制器中,您可以使用$ form-> getValues()獲取表單的所有值,然后使用foreach循環將該數據循環

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM