Weitere ähnliche Inhalte Ähnlich wie Data::Verifier and Message::Stack (20) Kürzlich hochgeladen (20) Data::Verifier and Message::Stack1. DATA::VERIFIER
&
MESSAGE::STACK
Invigorating The Forgotten Bits Of Your Web App
2. CORY G WATSON: GPHAT
• Magazines.com: Group Director, Development
• Cold Hard Code: Creative Director
• Contributor and Color Commentator on various projects.
3. BIASES: I HAVE THEM
• I’m a closet designer
• I hate All-In-One Form handlers
• Messaging & Validation are not sexy
4. VALIDATION: PRIMER
• Client side is advancing.
• It won’t replace server side.*
• Duplication of effort.
5. VALIDATION:
DATA::FORMVALIDATOR
• 8+ years old
• Big, Complex
7. D:V – FEATURES
• Reusable constraints and coercion
• Required, optional & dependents
• Length checking
• Global and per-field filters
9. WHY MOOSE?
• Simplicity
• Type Constraints
• Coercion
• Serialization
10. GETTING STARTED
my $dv = Data::Verifier->new(…);
my $results = $dv->verify(%params);
if($results->success) {
# o/
} else {
# :(
}
11. PROFILE
profile => %fields
• Simple HashRef
• A key for each field
• Opposite of D:FV, fields before required/optional
12. FIELD KEYS
• coerce • min_length
• coercion • post_check
• dependent • required
• filters • type
• max_length
13. EASY ONES
• required: Optional? Then it’s not required!
• max_length => $x
• min_length => $y
• type => $moose_type
14. EXAMPLE
filters => [ qw(trim) ],
profile => {
email => {
min_length => 5
type => EmailAddress,
required => 1
}
}
15. COERCE
• Same as Moose’s coerce flag
• true tells D:V to use Moose coercions
• Occludes coercion
16. COERCION
• Allows one-off coercions
• Does not define a global Moose coercion
• Saves cluttering your ::Types with coercions
18. DEPENDENT
• Field A depends on Field B
• More? Field A can depend on an entire profile.
• Creates a new D:V, verifies it, merges it with this one.
20. FILTERS
filters => [ qw(trim) ]
• Comes with some built-ins.
• Also allows CodeRefs
• Global and per-field
21. POST_CHECK
• Checked at end of verification.
• Accepts a CodeRef
• Given Results object as argument.
• $password1 eq $password2 scenarios
23. RESULT METHODS
• get_original_value # unmodified
• get_value # filters, coercions
• is_missing, is_valid
• More!
24. SERIALIZATION
• MooseX::Storage
• caveat: value isn’t serialized, arbitrary type
• use get_original_value
• validity and missing is retained
25. MESS-AGING
• Error variable
• Error ArrayRef
• Error plugin?
27. MESSAGE
• id: Localization
• level: ‘error’, ‘info’
• params: [ ‘gphat@coldhardcode’ ]
• scope: ‘login_form’
• subject: ‘email’
• text: ‘Invalid email address.’
29. MESSAGE::STACK
• List of messages
• Order is retained
• Convenience methods
• Many methods return sub-Stacks
31. BEING INFORMED
• count
• id, level, scope, subject
• has_* (boolean)
• for_* (subset Message::Stack)
32. Error Message!
Error Message!
Line Item 1 Line Item 1
Delivery Options Gift Options
Line Item 2 Line Item 2
Delivery Options Gift Options
CRAPPY FORM: WHERE ARE
33. Error Message!
Line Item 1 Line Item 1
Delivery Options Gift Options
Error Message!
Line Item 2 Line Item 2
Delivery Options Gift Options
GOOD FORM: PRECISE
34. METHODS
• Errors: for_level(‘error’);
• Sub-Form: for_scope(‘line-item1’);
• Specific field: has_subject(‘email’);
• Cat: c.loc(message.id, message.params)
35. HINTS
• Templates can get messy.
• Works well with macros.
• Use a lot or a little.
• foreach($stack->messages)
38. DATA::MANAGER
my $dm = Data::Manager->new;
my $v = Data::Verifier->new...;
$dm->set_verifier(‘foo’, $v);
$dm->verify(‘foo’, { ... });
my $res => $dm->get_results(‘foo’);
39. DATA::MANAGER (CONT)
• A single Message::Stack
• Multiple Data::Verifier objects (scopes)
• success
• messages_for_scope
40. SUMMARY
• Simplified validation
• Reusable types
• Serialization for redirects.
• Messaging is important.
• Context, context, context!
42. COLOPHON
• Titling: Tungsten 72pt, Hoefler & Frere-Jones
• Body: Hoefler Text 38pt, Hoefler & Frere-Jones
• Diagrams: OmniGraffle, The Omni Group
• Presentation: Keynote ’09, Apple, Inc
• Theme: Modified “Showroom”