Get the ids or names of dynamic fields with Jquery

0

I am making a system of purchases and sales, in which I use multiple items for the same, I use a package (gem) that allows me to add those dynamic fields called: "Cocoon" is a package for the Ruby On Rails framework , however, it generates automatic ids and names, which it is difficult for me to extract, the situation that I am validating my fields with Jquery-validation, in which form, I could obtain the ids or names in this case, regardless of the number of items that are , and verify if it is blank or not, the verification I do with the code below, but not being able to have the dynamic "name" of the field makes it difficult for me to validate these dynamic fields.

jQuery(document).ready(function($){
    $("#form").validate({
    rules: {
        "product[code]": {
        required: true, maxlength: 32, normalizer: function(value) { return $.trim(value); }
        },
        "product[name]": {
        required: true, maxlength: 32, normalizer: function(value) { return $.trim(value); }
        },
    },
    messages: {
        "product[code]": { required: "El campo es requerido", maxlength: "No se permiten mas de 32 caracteres" },
        "product[name]": { required: "El campo es requerido", maxlength: "No se permiten mas de 32 caracteres" },
    }
    });
});

The previous code would validate the messages in the following way:

As you can see in the image, for the price field, it generates the following name: "input_input_items_attributes_1534806645872_price", which "1534806645872" is generated randomly, or as a kind of timestamp, that name, should be able to place it within the rules and messages to validate the field, and be able to put it as:

jQuery(document).ready(function($){
    $("#form").validate({
    rules: {
        "input_input_items_attributes_XXXXX_price": {
        required: true, maxlength: 32, normalizer: function(value) { return $.trim(value); }
    },
    messages: {
        "input_input_items_attributes_XXXXX_price": { required: "El campo es requerido", maxlength: "No se permiten mas de 32 caracteres" },
    }
    });
});

In conclusion, it is possible to obtain the id in a dynamic way, and that Jquery can read it to validate the item, regardless of the name you have, I appreciate your help!

    
asked by Hector Hernandez 21.08.2018 в 01:20
source

1 answer

0

You could iterate over all the inputs and add the rules. If you have different rules for different inputs, you could add classes and do it in the same way. Something like this:

jQuery(document).ready(function($){
    var rules = {};
    var messages = {};
    $('#form input').each(function(i, input) {
        rules[$(input).attr('id')] = { required: true, maxlength: 32, normalizer: function(value) { return $.trim(value); };
        messages[$(input).attr('id')] = { required: "El campo es requerido", maxlength: "No se permiten mas de 32 caracteres" };
    });
    $("#form").validate({
        rules: rules,
        messages: messages
    });
});
    
answered by 21.08.2018 в 01:51