[From Validation] should be initializing on all elements with a specified identifier to have the same ruleset be applied to multiple fields
Is there a way to validate multiple form elements with the same name/id/class?
With php you submit multiple elements with name="value[]" and it is turned into an array on post.
But it might be valuable to allow one to validate all inputs with class "notEmpty" and then to make a rule with an identifier: "notEmpty" and try to add "." before just as much as "#" is added.
This is extremely useful when dynamically adding fields to a form especially when the fields allow for multiple of the same information like contacts, as you can see:
http://jsfiddle.net/wq4wL/
Using name for multiple fields won't work since they are all highlighted with errors even if one has a value and one not. Please advise the best solution.....
Yes, it is quite easy.
Form validation matches on id="foo", then name="foo", then data-validate="foo"
See this JSFiddle
I think that misses the point.... It will become difficult to maintain a form that has fields added dynamically. You will need to change each id (even by incrementing) and also you would need to create a separate rule for each and continually remove rules etc....
Perhaps a feature for the future to consider; otherwise I will do it manually for now with ids.
First off, id's should always be ... unique. That's why they are IDs. Secondly, classes are thought to say which type of elements the div/block/inline belongs to. Last but not least, name was thought for forms to change the name in the POST-Data. But also, they are meant to be unique like IDs.
So, the right way to do is something like
<input id="data.contact.0.name" name="data[contact][0][name]"/> <label for="data.contact.0.name">
This will build a POST-Array for data that looks like the declaration.
You can build a wrapper script that finds the latest index and increases it by 1 to generate the new element based on the current existing values.
So, @jlukic did show you a way how to do it, however, I do not recommend to actually have name or ID not unique.
The only difference between name and id is, that for radios you have to use the same name. But still, ID should be unique in the complete page, all other names except for radio elements should be unique in the form-parent.
http://stackoverflow.com/questions/1363693/do-input-field-names-have-to-be-unique-across-forms/1363717#1363717
It is just a lot of heavy lifting that seems unnecessary, especially since my form has 4 or 5 fields that are replicated. So incremented and handling removals and rules is quite a lot to throw on the web browser when it is unnecessary. I will likely write my own validation to use the class value personally. I do recognize that you might be right about the class name being misused. For the record, I originally used name="contact[]", which php automatically handles and returns an array, but semantic-ui can't handle that even though it is standard in php development. So I suppose there won't be any inclusion of this feature, which is fine.
I suppose it is something only I need. For the record I do respect and think its good that the framework maintain the standards, but I sometimes still need to cross the line for myself, but @Anachron I do appreciate your points.
@msj121 I just wanted to throw in why contact[] works,- basically, HTTP is smart enough to put all the different fields internally correctly into the array, so the first name=contact[] is actually name=contact[0] and so on. However, you were never meant to validate those hacks, but rather the name specifically (name=contact[0]). (Except radio, of course)
@jlukic @Anachron The data-validate does not work for multiple inputs as suggested in you answer, or am I doing wrong? See: http://jsfiddle.net/n8uqe74c/ It only validates the first field and displays any error message on all fields with the same data attribute. As I have a form with n (1-300) repeating rows with up to up to 10 columns I would love to specify just those 10 input types. Thanks!
@burka Totally agree! Seems like right now we cannot have same rule applied to multiple fields. Think this should be reopened.
should be reopened indeed
+1
Well don't know but above examples not working. It is not validating dynamically added fields individually. See this fiddle -> http://jsfiddle.net/shivek/mf0kcL9q/2/
But I found a hack and done this.. https://jsfiddle.net/d1x99xwg/12/
Not sure whether there is another better way then this or not but it worked for me. Any suggestions or any other working solutions are welcome and appreciated.
@jlukic your suggestions are required on above. Thanks..!!
Agreed that all fields with the same name should be initialized for validation. We have a dynamic form where more date fields can be added when needed. When added, the fields are required and should be validated. We just replicate the part of the validation object a couple of times now and give each field an incrementing ID. Very ugly, but it works.
Any update on this? Being able to validate on fields with the same name would be very helpful.
+1
+1
+1
here is my code, i use 2 data attribute: 1- data-validate: the data validate name used as identifier by the validator 2- data-validate-group : to search for the validation group and rename all group members data-validate attribute
then i create the validator object and init the validation on the form on the submit event
validationObj = {
error: function (errors) {
var
html = '<ul class="list">'
;
$.each(errors, function (index, value) {
html += '<li>' + value + '</li>';
});
html += '</ul>';
return $(html);
},
fields: {
}
}
// re-index all complex validation ids in data-validate
var complexItems = [];
$("[data-validate-group='complex-allocation']").each(function (index, item) {
var $item = $(item);
$item.attr("data-validate", "complex-allocation-" + index);
complexItems.push("complex-allocation-" + index)
});
if (advancedAllocationFlag) {
// advanced complex allocation validation
for (i = 0; i < complexItems.length; i++) {
validationObj.fields[complexItems[i]] = 'empty';
}
} else {
for (i = 0; i < simple_allocation_validation.length; i++) {
validationObj.fields[simple_allocation_validation[i]] = 'empty';
}
}
// add validation
$('#mainForm').form(validationObj);
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.