0

I wanted to know how can we write validator for aui:select field which is in Autofield category.

This is my structure of the code:

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

There is no problem with the functionality of Autofields. I am able to create duplicate entries while viewing of the form by looping through my items collection ,and there is also no problem while "CREATING" the form by using the liferay provided PLUS icon.

I have aui:select element in the container, which will be duplicated according to Autofield functionality. How can I provide a validator for this aui:select element. ?

Kiran Kulkarni
  • 1,434
  • 2
  • 18
  • 40
  • I would also include that you have [asked](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) the question on the liferay forums. – Origineil Jun 06 '14 at 17:27
  • Are the fields part of a form? – Origineil Jun 07 '14 at 04:48
  • Yes Origineil, the fields are part of the form – Kiran Kulkarni Jun 09 '14 at 05:00

1 Answers1

1

Assuming some "template" <aui:select> exists within your form similar to:

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

In your auto-fields, you would need to provide an on event listener for the clone event. Within the callback you lookup the <aui:select> from within the row container node that was just created (passed into the callback as a parameter).

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

Ideally, you should be able to use a child selector to get the node from within the clone container. I had to provide a different way since I couldn't get that method to work. The reason I can use my approach is due to the fact that I know what the elementIdPrefix is. For the sake of being able to provide an example, I went ahead and took advantage of this fact.

For a more dynamic approach, a selector such as myNode.one('> selectorString'); would have to be used.

Origineil
  • 3,108
  • 2
  • 12
  • 17
  • Thanks Origineil, I will save this for my future reference. What I did was kind of hack. I created an input field with the same name as of "select" field ;-) and i have set it as "hidden" for which I am using a custom validator function where I am checking the value of "select" field. Sure, will check with your approach when I find time. I am marking it as "Accepted" – Kiran Kulkarni Jun 10 '14 at 04:33
  • Hi there, is there any way to apply custom validator on autofield? – Poorav Solanki Sep 01 '17 at 10:46