#1283: Allow extraction of subpatterns in regex validator
-------------------------+--------------------------------------------------
Reporter: david | Owner: david
Type: enhancement | Status: reopened
Priority: normal | Milestone: 1.0.4
Component: validation | Version: 1.0.3
Severity: normal | Resolution:
Keywords: | Has_patch: 0
-------------------------+--------------------------------------------------
Changes (by david):
* status: closed => reopened
* resolution: fixed =>
Comment:
Reopening after a discussion on IRC showed that it would be even more
useful if this validator could export more than one named subpattern (to
basically split arguments into several chunks).
Decisions:
1. parameter {{{extract}}} is dropped
1. thus the ability to overwrite the input argument by omitting
{{{export}}} is also removed again
1. if {{{export}}} is a string, the whole value just gets exported as
usual
1. if {{{export}}} is an array, it's treated as a map of named subpattern
names as keys and destination arguments as values
Example for the case above:
{{{
#!xml
<validator class="regex">
<argument>username</argument>
<ae:parameter
name="pattern">#^\s*(?'localpart'.+?)(@yourcompany\.com)?\s*$#u</ae:parameter>
<ae:parameter name="export">
<ae:parameter name="localpart">user</ae:parameter>
</ae:parameter>
</validator>
}}}
To overwrite the input:
{{{
#!xml
<validator class="regex">
<argument>username</argument>
<ae:parameter
name="pattern">#^\s*(?'localpart'.+?)(@yourcompany\.com)?\s*$#u</ae:parameter>
<ae:parameter name="export">
<ae:parameter name="localpart">username</ae:parameter>
</ae:parameter>
</validator>
}}}
To export two subpatterns:
{{{
#!xml
<validator class="regex">
<argument>username</argument>
<ae:parameter
name="pattern">#^(?'startdate'\d{4}/\d{2}/\d{2})\s*-\s*(?'enddate'\d{4}/\d{2}/\d{2})$#u</ae:parameter>
<ae:parameter name="export">
<ae:parameter name="startdate">startdate</ae:parameter>
<ae:parameter name="enddate">enddate</ae:parameter>
</ae:parameter>
</validator>
}}}
{{{
00:28 < hzilla> lo again - is it poss to use datetimevalidator to validate
a date range egg yyyy/mm/dd - yyyy/mm/dd ?
00:32 < saracen> You could use the DateTimeValidator on both the start
date and the end date.
00:32 < saracen> Or do you mean the format comes in like this: yyyy/mm/dd
- yyyy/mm/dd
00:34 < hzilla> yeh 2nd
00:34 < hzilla> combined in one string
00:35 < hzilla> i could split in action and register validators?
00:35 < saracen> Yeah, but that's messy. :), hmm... All you need is
another validator which could split the string and export the result, then
you can use the DateTimeValidator on the exports
00:36 < saracen> If only the regex validator allowed for exported values
:). You could just create your own validator which splits up that specific
string.
00:36 < saracen> DateTimeValidator also allows a custom string format for
input, but I can't think of a way that would cater for this
00:37 < saracen> 'extract' Name of subpattern to extract and set or
export on match
00:37 < saracen> Oh, the regex validator can do this
00:37 < hzilla> oh yeh? nice one
00:38 < saracen> Wombert would know more. But basically you could setup
two regex validators, one for the first date range, another for the second
date range. Then export those values, and give them to DateTimeValidator
00:38 < saracen> But maybe even that's a sucky solution
00:39 < hzilla> doesn't seem too bad to me
00:39 < Wombert> saracen: it does
00:39 < Wombert> (support that)
00:39 < Wombert> since 1.0.4
00:39 < hzilla> :(
00:39 < saracen> Ah :). That'll be why it was news to me when I just
looked on trac src
00:40 < saracen> hzilla: Custom validator then perhaps?
00:40 < Wombert> <ae:parameter
name="extract">namedsubpattern</ae:parameter>
00:40 < saracen> Unless you can use 1.0.4
00:40 < Wombert> derp, "nameofsubpattern"
00:40 < Wombert> stable 1.0.4 is coming tomorrow anyway...
00:40 < hzilla> ok soundsgood
00:40 < hzilla> its late anyway!
00:40 < Wombert> but careful with extract, if you don't use export, it'll
overwrite the argument
00:40 < saracen> I'm terrible with regex, so can't provide much in the way
of the pattern to use
00:41 < hzilla> that's ok
00:41 < saracen> I always use export as a rule
00:41 < Wombert> ^(?P'startdate'\d{4}/\d{2}/\d{2})
00:42 < Wombert> should work then
00:42 < Wombert> <ae:parameter name="extract">startdate</ae:parameter>
00:42 < hzilla> awesomeness
00:42 < Wombert> and the other one
00:42 < Wombert> (?P'startdate'\d{4}/\d{2}/\d{2})$
00:42 < saracen> So as my idea for the solution goes, I'm awesome, right,
right?
00:42 < Wombert> although that's not enough yet really
00:42 < Wombert> yeah good one saracen
00:42 < saracen> :D
00:42 < Wombert> although I think the validator maybe should even support
several subpattern exports
00:43 < Wombert> or umm
00:43 < Wombert> nah wouldn't work
00:43 < Wombert> I guess
00:43 * Wombert ponders
00:43 < saracen> Why wouldnt it? Can't you just make the export field an
array? export_name[0], export_name[1]. I mean, validators can deal with
arrays anyway, so passing them to another validator would still be fine
00:44 < Wombert> yeah
00:44 < Wombert> <ae:parameter name="export"> and then a map
00:44 < Wombert> I think
00:44 < Wombert> that might work
00:44 < Wombert> but then "extract" becomes pointless
00:44 < saracen> Good. I'd never have remembered the name of it anyway
00:45 -!- hzilla_ [[email protected]] has joined
#agavi
00:46 < Wombert> haha
00:46 < Wombert> well the problem is if you have just one
00:46 < Wombert> then "export" won't cut it
00:46 < Wombert> so... meh
00:47 < saracen> Well, surely you'd just export $matches? Or is that bad
in case of optional matches?
00:47 -!- hzilla [[email protected]] has quit [Ping
timeout: 272 seconds]
00:47 < Wombert> well
00:47 < Wombert> yes
00:48 < Wombert> and
00:48 < Wombert> humm
00:48 * Wombert ponders
00:48 < Wombert> one problem is finding all matches. well, iterate over
keys and see if it's a string
00:48 < Wombert> but maybe you don't want all of them (e.g. when reusing a
pattern)
00:48 < Wombert> there is just one case I think where export can be an
array
00:48 < Wombert> and that's in datetime
00:49 < Wombert> where you then have a map of calendar field names (e.g.
"year") as the key and the argument name to export to as the value
00:49 < Wombert> and I'd rather have it consistent with that
00:49 < Wombert> so you'd have a pattern with two named subpatterns, and
then export is an array mapping the subpattern name to an argument name
00:49 < Wombert> but then if you have just *one* subpattern...
00:49 < Wombert> meh meh meh
00:50 < saracen> With one subpattern, I'd still output an array
00:50 < Wombert> alternatively, export as a string could export the whole
string
00:50 < Wombert> and if it's an array, it's for subpattern names
00:50 < Wombert> that might work
00:50 < Wombert> what if it can't find the subpatterns?
00:50 < Wombert> ignore?
00:50 < Wombert> fail?
00:50 < Wombert> ignore I'd say, since it's the job of the pattern to
validate the args
00:51 < Wombert> besides, it'd be an empty string I think, that's what
preg_match does
00:51 < saracen> Yeah, just ignore. If you wanted to fail on a specific
pattern not being found, you could just chain it with another validator
00:51 < Wombert> can you export into the same arg?
00:51 < Wombert> saracen: nah, the regex would fail in that case anyway ;)
00:52 < saracen> oh yeah ;x
00:53 < Wombert> http://trac.agavi.org/ticket/1283
00:53 < Wombert> that's the ticket for it
00:53 < saracen> I think you should come up with a solution tonight, so
that when you release 1.0.4 tomorrow, hzilla can use it :P
00:53 < Wombert> ok, so drop "export"
00:53 < Wombert> derp
00:53 < Wombert> drop "extract"
00:53 < saracen> extract :P
00:54 < saracen> Yeah. See. It's a confusing name
00:54 < Wombert> and if "export" is an array, use that to extract named
subpatterns
00:54 < Wombert> I was about to say the same thing
00:54 < Wombert> but yes, overall, that sounds like a good plan
00:54 < Wombert> simpler and more powerful
00:54 < Wombert> thank you kindly, sir, for this conversation
00:54 < saracen> I want you to know, my trousers were down the entire
time.
00:55 < Wombert> it is possible to export to the same arg as the input,
right?
00:55 < Wombert> yeah should be
00:55 < hzilla_> i missed a bit of that conversation - so u were referring
handling > 1 named patterns?
00:55 < Wombert> hzilla_: exactly
00:55 < Wombert> hzilla_: that'll also make it easier for you :)
00:56 < hzilla_> yeh this is going to be very useful for various things i
expect
}}}
--
Ticket URL: <http://trac.agavi.org/ticket/1283#comment:4>
Agavi <http://www.agavi.org/>
An MVC Framework for PHP5
_______________________________________________
Agavi Tickets Mailing List
[email protected]
http://lists.agavi.org/mailman/listinfo/tickets