#18 ✓not_for_core
SWilk

form.getElement accept RegExp additionally to string as "name" parameter

Reported by SWilk | April 16th, 2008 @ 04:02 PM

I have a form with many checkboxes, which names looks like this

name='selectedLine[127][23]'

It is impossible to collect them with Form.getInputs, cause the

numbers in brackets are variable.

Anyway, all the checkboxes' names would match RegExp

/selectedLine\[\d+\]\[\d+\]/

I suggest adding support for RegExp matching of input names.

We can test if the "name" parameter is an RegExp by checking if it

implements the "test" and "exec" regExp methods. If so, we use

name.test(), else we use old method - direct comparison.

Of course it is possible that not-RegExp object will implement test

and exec methods, but then, I think it is safe to trust that the

programmer knows what (s)he passes to this method. If it is something

random, then i wouldn't made through (name != input.name) comparison

either.

It is also possible to pass any object with test() method witch would

do any test needed, not necessarily an RegExp test. Maybe we even

should'n test for .exec() method?

Patch attached. No unit test at the moment.

Comments and changes to this ticket

  • John-David Dalton

    John-David Dalton April 16th, 2008 @ 04:38 PM

    • Title changed from “form.getElement accept RegExp additionally to string as "name" parameter” to “[PATCH][ENH] form.getElement accept RegExp additionally to string as "name" parameter”

    marked as enhancement/patch attached, though I cannot see the attachement, it is it the same reason I cannot mark tickets invalid or dups?

  • SWilk

    SWilk April 16th, 2008 @ 04:50 PM

    This is my first ticket, I might have done something wrong.

    I'll try to attach the patch again to this post.

  • Juriy Zaytsev

    Juriy Zaytsev April 16th, 2008 @ 07:15 PM

    @jdalton

    the attachments are on the right. I can see them even when logged out.

  • Juriy Zaytsev

    Juriy Zaytsev April 16th, 2008 @ 07:28 PM

    I don't see a reason to create another condition block specifically for regexp.

    
    if (typeName && input.type != typeName)
      continue;
    
    // we could check if argument has test property
    // but then we should probably check is test property is callable (i.e. can be invoked)
    
    if (name && (name.test ? !name.test(input.name) : name != input.name))
      continue;
    
    
  • Tobie Langel

    Tobie Langel April 16th, 2008 @ 10:12 PM

    • State changed from “new” to “enhancement_patch”
    • Title changed from “[PATCH][ENH] form.getElement accept RegExp additionally to string as "name" parameter” to “form.getElement accept RegExp additionally to string as "name" parameter”
  • John-David Dalton

    John-David Dalton April 16th, 2008 @ 11:34 PM

    Instead of a passing a regexp could pass an iterator function that returns its own evaluatuation.

    Similar to:

    http://www.prototypejs.org/api/e...

    Or how Juriy does it here:

    http://github.com/kangax/protoli...

    I believe he has more examples of such usage, though I cannot find it.

  • Juriy Zaytsev

    Juriy Zaytsev April 17th, 2008 @ 01:43 AM

    @jdalton

    I don't think iterator is a good candidate for this. I can't really think of any cases when iterator as a name filter of #getInputs would be useful. Do you have any in mind?

    The function-value passing into setStyle/writeAttribute (that I proposed earlier) is another thing. It is sometimes useful to perform some action on the original value/style before setting it to something else (conditional setter, replacement, appending, prepending, etc.)

  • Tobie Langel

    Tobie Langel April 18th, 2008 @ 01:11 PM

    • State changed from “enhancement_patch” to “enhancement”
  • Juriy Zaytsev

    Juriy Zaytsev April 24th, 2008 @ 12:01 AM

    @sWilk

    Could you please make tests for this patch.

    Thanks.

  • SWilk

    SWilk April 24th, 2008 @ 09:26 AM

    Yes, I'll submit tests this weekend, probably in Sunday.

    There is a bug in the patch at the moment, it bails out when no "name" parameter is passed. I forgot to test for name itself. I have already added a condition to test it.

    Your suggestion is of course free of this bug, and it's much simpler, so I think I'll make test for your version to.

    Regards,

    Szymon

  • Tobie Langel

    Tobie Langel April 24th, 2008 @ 03:44 PM

    • State changed from “enhancement” to “not_for_core”

    What about using something as simple as:

    $(form).select('input[name^=selectedLine]');
    

    Unless I'm mistaking (in which case, please feel free to reopen this ticket), this seems like a reasonable enough solution to this issue which the current API already offers.

    Closing as not_for_core.

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:01 AM

    • Tag set to section:dom

    [not-tagged:"form" tagged:"section:dom" bulk edit command]

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

People watching this ticket

Pages