Semantic-UI icon indicating copy to clipboard operation
Semantic-UI copied to clipboard

[From Validation] should be initializing on all elements with a specified identifier to have the same ruleset be applied to multiple fields

Open msj121 opened this issue 12 years ago • 17 comments

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

msj121 avatar Feb 25 '14 23:02 msj121

Yes, it is quite easy.

Form validation matches on id="foo", then name="foo", then data-validate="foo"

See this JSFiddle

jlukic avatar Feb 26 '14 16:02 jlukic

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.

msj121 avatar Feb 26 '14 18:02 msj121

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

Anachron avatar Feb 26 '14 19:02 Anachron

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 avatar Feb 26 '14 21:02 msj121

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

Anachron avatar Feb 26 '14 21:02 Anachron

@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 avatar May 29 '15 09:05 burka

@burka Totally agree! Seems like right now we cannot have same rule applied to multiple fields. Think this should be reopened.

avalanche1 avatar Sep 11 '15 11:09 avalanche1

should be reopened indeed

roelvan avatar Dec 28 '15 14:12 roelvan

+1

jhaesus avatar Mar 31 '16 14:03 jhaesus

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

shivekundefined avatar May 10 '16 13:05 shivekundefined

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.

Emielangelaan avatar Jul 26 '16 15:07 Emielangelaan

Any update on this? Being able to validate on fields with the same name would be very helpful.

picitelli avatar Mar 09 '17 05:03 picitelli

+1

Johanbo123 avatar Mar 29 '17 15:03 Johanbo123

+1

kodamirmo avatar Jun 15 '17 05:06 kodamirmo

+1

kabapy avatar Jan 06 '18 16:01 kabapy

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

kabapy avatar Jan 06 '18 18:01 kabapy

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.

stale[bot] avatar Apr 06 '18 19:04 stale[bot]