Set a minimum limit in a custom multifield component

有些话、适合烂在心里 提交于 2019-12-11 08:52:47

问题


I have a custom multifield component with a maximum limit of 5 elements applied to the 'Add Item' button.

I need to add a similar listener logic to the 'OK' button of the dialog box to check if the minimum number of items (3) have been added or not.

How can that be achieved ? I didn't find any sample code to add a listener code to the button ..


回答1:


I've done this same sort of thing with a listener on the beforesubmit event of the Dialog (see beforesubmit for CQ.Dialog at http://dev.day.com/docs/en/cq/current/widgets-api/index.html - search for "Dialog"):

<listeners
  jcr:primaryType="nt:unstructured"
  beforesubmit="function(dialog){return myNamespace.myCustomFunction(dialog);}"/>

Then the custom JavaScript function, included on the page via a client library, could be something like this:

myNamespace = {};
myNamespace.myCustomFunction = function (dialog) {
    var isValid = function () {
        var valStatus = true;
        ... custom JavaScript/jQuery to check if 3 items exist ...
        return valStatus;
    };
    if (!isValid()) {
        CQ.Ext.Msg.show({title: 'Validation Error', msg: 'Must contain at least 3 items!', buttons: CQ.Ext.MessageBox.OK, icon: CQ.Ext.MessageBox.ERROR});
        return false;
    } else {
        return true;
    }
}


来源:https://stackoverflow.com/questions/21736103/set-a-minimum-limit-in-a-custom-multifield-component

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!