2014-06-06 97 views
0

我想知道我們如何爲aui選擇驗證器:選擇Autofield類別中的字段。aui的Validator:在Liferay中選擇autofield

這是我的代碼結構:

for(loop total number of items) 
{ 
// CREATE aui select, and aui inputs by appending the index 
} 

沒有與Autofields的功能沒有任何問題。通過循環訪問我的項目集合,我可以在查看錶單的同時創建重複條目,使用Liferay提供的PLUS圖標「創建」表單時也沒有問題。

我有aui:select容器中的元素,它將根據Autofield功能進行復制。我怎樣才能爲這個aui:select元素提供驗證器。 ?

+0

我還包括你有[問](http://www.liferay.com/community/forums//message_boards/message/38630011?_19_redirect=http%3A%2F%2Fwww.liferay.com% 2Fcommunity%2Fforums%2F-%2Fmessage_boards%2Fsearch%3F_19_keywords%3Dautofields%26_19_searchCategoryId%3D0%26_19_breadcrumbsCategoryId%3D0%26_19_redirect%3Dhttp%253A%252F%252Fwww.liferay.com%252Fcommunity%252Fforums%252F-%252Fmessage_boards%252Fmessage%252F15088039% 26_19_formDate%3D1402068697228)生命線論壇上的問題。 – Origineil

+0

字段是表單的一部分嗎? – Origineil

+0

是的Origineil,這些字段是表格的一部分 –

回答

1

假設一些「模板」 <aui:select>您的形式類似於中存在:

<aui:select id="elementIdPrefix0" name="elementIdPrefix0" label="Number" showEmptyOption='true' > <!-- options go here --></aui:select> 

在你auto-fields,你就需要爲clone事件提供on事件偵聽器。在回調中,您可以從剛剛創建的行容器節點中查找<aui:select>作爲參數傳入回調)。

<script> 
    AUI().use('liferay-auto-fields', 'aui-form-validator', function(A){ 

    //Setup rules 
    var elementIdPrefix = '<portlet:namespace />elementIdPrefix', 
     myRules = {}, 
     rulesRepository = {}; 

     rulesRepository[elementIdPrefix] = {required:true}; 
     myRules [elementIdPrefix + '0'] = rulesRepository[elementIdPrefix]; 

     //Define validator 
     var validator = new A.FormValidator({ 
          boundingBox: '#<portlet:namespace />myForm', 
          rules: myRules 
          }); 

    new Liferay.AutoFields({ 
    contentBox: '#my-fields', 
    fieldIndexes: '<portlet:namespace />indexes', 
    on: { 
     'clone': function(container){ 

      //Lookup the clone 
      AUI().all('[name^=<portlet:namespace />elementId]').each(function(node, index){ 

      if(container.row.contains(node)){ 
       console.log("Assign to " + node.get('id')) 
       //inject the rules 
       myRules [node.get('id')] = rulesRepository[elementIdPrefix] 
      } 
      }) 
     } 
    } 
}).render(); 
}); 

</script> 

理想情況下,你應該能夠使用一個子選擇從clone容器內獲得的節點。我不得不提供一種不同的方式,因爲我無法使這種方法起作用。我可以使用我的方法的原因是因爲我知道elementIdPrefix是什麼。爲了能夠舉一個例子,我繼續並利用了這個事實。

對於更動態的方法,必須使用選擇器(如myNode.one('> selectorString');)。

+0

謝謝Origineil,我將保存這個以備將來參考。我所做的就是一種破解。我創建了一個與「select」字段具有相同名稱的輸入字段;並且我已將其設置爲「隱藏」,因此我正在使用自定義驗證器函數來檢查「select」字段的值。當然,當我找到時間時會檢查你的方法。我將它標記爲「已接受」 –

+0

嗨,有沒有辦法將自定義驗證器應用於自動字段? –

+0

在這裏,它適用於我。 https://stackoverflow.com/a/46001206/4587294 –

相關問題