jquery-validation icon indicating copy to clipboard operation
jquery-validation copied to clipboard

Enhancement: Enable class rules to optionally apply to child input elements.

Open roryhardy opened this issue 12 years ago • 9 comments

I'd like a way to give the validator class rules that apply to child input elements as opposed to the element the class is on. The goal is to have a container with the class of required and all child inputs would then be required; even if dynamically inserted.

Something along the lines of:

$('myForm(s)').find('.required input, .required select, .required textarea').each(function () {
  $(this).rules('add', {
      required: true
  });
});

works, but only against elements in the DOM during invocation.

Basically, I don't want to ask users to add the rules themselves when adding dynamic input or having to put the required attribute on them to make it work.

$('element').rules('add', {...});

I put together an example: http://jsfiddle.net/73Lf20y1/

roryhardy avatar Oct 22 '14 18:10 roryhardy

+1, would really like to have this feature for dynamically added form inputs.

bjankord avatar Oct 22 '14 18:10 bjankord

+1, would also really love to have this. Would have saved me a good deal of time and finger exercise.

timshoaf avatar Oct 22 '14 19:10 timshoaf

This would be a nice feature to have for me as well.

garrypolley avatar Oct 30 '14 21:10 garrypolley

An option that allows you to specify a callback that implements custom parsing to find rules in the DOM could work to address this. But that doesn't sound like it would be any easier to use then just putting the necessary classes directly on the right element or using the method you suggest.

What do you think?

jzaefferer avatar Nov 11 '14 21:11 jzaefferer

An option that allows you to specify a callback that implements custom parsing to find rules in the DOM could work to address this. But that doesn't sound like it would be any easier to use then just putting the necessary classes directly on the right element or using the method you suggest.

What do you think?

I'm not entirely sure how that would address the case of dynamic input. Could you elaborate?

Is what I'm trying to exemplify in the JSFiddle I linked in the PR clear? Basically, I don't want to ask developers to explicitly require specific input fields if their parent container already depicts that by having a required class of its own.

The method I suggested works great if I can anticipate no dynamically added content. If developers are going to be adding form elements after the DOM is loaded then it either needs to be re-run by the developers or they would have to add the attribute on each added input.

roryhardy avatar Dec 03 '14 19:12 roryhardy

If developers are going to be adding form elements after the DOM is loaded then it either needs to be re-run by the developers or they would have to add the attribute on each added input.

Both options sound fine to me. If there's code adding new elements, extend it to make the validation work for those. Should be easy enough to abstract as well.

That said, if someone has an idea how to solve this, please let me know. I can't think of anything that makes it any easier for the developer using this plugin.

jzaefferer avatar Dec 09 '14 14:12 jzaefferer

@jzaefferer If I read this thread correctly you are open to having this "just work". Meaning that if someone can figure it out, you'd be happy to have dynamically added elements get their validators attached with no extra rigamarole. The developer could add the node dynamically and it would work without extra work?

garrypolley avatar Dec 09 '14 14:12 garrypolley

Well, yeah, if there's a reasonable solution to that.

jzaefferer avatar Dec 09 '14 15:12 jzaefferer

I'm sorry for the lack of activity on this issue. Instead of leaving it open any longer, I decided to close old issues without trying to address them, to longer give the false impression that it will get addressed eventually, especially after several years with no activity. It doesn't mean I'm abandoning the project, just that I'm unable to work through 200+ open issues with the little time I can afford to spend on this project.

To the reporter (or anyone else interested in this issue): If you're still affected by the same issue, please consider opening a new issue, with a testpage that demonstrates the issue with a current version of the plugin. Even better, make an attempt to fix the issue yourself, and improve the project by sending a pull request. This may seem daunting at first, but you'll likely learn some useful skills that you can apply elsewhere as well. And you can help keep this project alive. We've documented how to do these things, too. A patch is worth a thousand issues!

jzaefferer avatar Jun 28 '15 19:06 jzaefferer