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!