Permalink
Browse files

Update to share with laptop

  • Loading branch information...
1 parent bd25bcd commit 278b9d8ee1b1c4adf459969efe0accd267b0b014 James Laver committed Jun 20, 2009
View
@@ -4,6 +4,7 @@ our $VERSION = 0.01;
use Moose;
use MooseX::AttributeHelpers;
+use List::Util 'all';
has _fields_a => (
metaclass => 'Collection::Array',
@@ -62,8 +63,6 @@ sub BUILD {
my @search_path = (
#This will load Email and Password etc.
'SparkX::Form::Field',
- #This will load Mandatory, Optional and any specific type plugins
- 'Spark::Form::Field'
);
if ($self->plugin_ns) {
unshift @search_path, ($self->plugin_ns);
@@ -112,17 +111,19 @@ sub get {
sub validate {
my ($self) = @_;
-
- $self->valid(1);
- $self->_clear_errors();
- foreach my $field ($self->fields_a) {
- $field->validate;
- unless ($field->valid) {
- $self->_error($_) foreach $field->errors;
+ if (all { $_->meta->does_role('Spark::Form::Field::Validateable') } $self->fields_a) {
+ $self->valid(1);
+ $self->_clear_errors();
+ foreach my $field ($self->fields_a) {
+ $field->validate;
+ unless ($field->valid) {
+ $self->_error($_) foreach $field->errors;
+ }
}
+ $self->valid;
+ } else {
+ die ("Not all fields in this form are validateable.");
}
-
- $self->valid;
}
sub data {
@@ -140,9 +141,9 @@ sub _valid_custom_field {
my ($self,$thing) = @_;
eval {
#Minimum spec for a field:
- # implements:
+ # isa:
# - Spark::Form::Field
- $thing->meta->does_role('Spark::Form::Field')
+ $thing->isa('Spark::Form::Field')
} or 0;
}
@@ -225,7 +226,7 @@ Spark Form - A simple yet powerful forms validation system that promotes reuse.
=head1 SYNOPSIS
use Spark::Form;
- use CGI; #Because it makes for a quick and oversimplisticn example
+ use CGI; #Because it makes for a quick and oversimplistic example
use Third::Party::Field;
$form = Spark::Form->new(plugin_ns => 'MyApp::Field');
# Add a couple of inbuilt modules
View
@@ -1,42 +1,13 @@
package Spark::Form::Field;
-use Moose::Role;
-use MooseX::AttributeHelpers;
-
-requires 'validate';
+use Moose;
has name => (
isa => 'Str',
is => 'ro',
required => 1,
);
-has label => (
- isa => 'Str',
- is => 'rw',
- required => 0,
-);
-
-has valid => (
- isa => 'Bool',
- is => 'rw',
- required => 0,
- default => 0,
-);
-
-has _errors => (
- metaclass => 'Collection::Array',
- isa => 'ArrayRef[Str]',
- is => 'ro',
- required => 0,
- default => sub{[]},
- provides => {
- push => '_add_error',
- elements => 'errors',
- clear => '_clear_errors',
- },
-);
-
has form => (
isa => 'Spark::Form',
is => 'rw',
@@ -48,30 +19,43 @@ has value => (
required => 0,
);
-sub error {
- my ($self,$error) = @_;
-
- $self->valid(0);
- $self->_add_error($error);
-}
-
sub human_name {
my ($self) = @_;
- return ($self->label || $self->name||'');
+ $self->can('label') && $self->label or $self->name or '';
}
-before 'validate' => sub {
- my ($self) = @_;
- $self->_clear_errors;
-};
-
1;
__END__
=head1 DESCRIPTION
-Field role. Must implement this to be considered a field.
+Field superclass. Must subclass this to be considered a field.
+
+=head1 SYNOPSIS
+
+ package My::Field;
+ use Moose;
+ require Spark::Form::Field;
+ extends 'Spark::Form::Field';
+ with 'Spark::Form::Field::Role::Validateable';
+ with 'Spark::Form::Field::Role::Printable::XHTML';
+
+ sub validate {
+ #validate existence of data
+ !!shift->value;
+ }
+
+ sub to_xhtml {
+ #Rather poorly construct an XHTML tag
+ '<input type="checkbox" value="' . shift-value . '">';
+ }
+
+Note that you might want to look into HTML::Tiny.
+Or better still, L<SparkX::Form::Field::Printable::StarTML>.
+
+There are a bunch of prebuilt fields you can actually use in
+L<SparkX::Form::BasicFields>.
=head1 ACCESSORS
@@ -80,18 +64,6 @@ Field role. Must implement this to be considered a field.
Name of the field in the data source. Will be slurped on demand.
Required at validation time, not at construction time.
-=head2 label => Str
-
-Optional. Human readable label for the field.
-
-=head2 valid => Bool
-
-Treat as readonly. Whether the field is valid.
-
-=head2 errors => ArrayRef
-
-Treat as readonly. The list of errors generated in validation.
-
=head2 form => Spark::Form
Reference to the form it is a member of.
@@ -102,27 +74,14 @@ Value in the field.
=head1 METHODS
-=head2 error (Str)
-
-Adds an error to the current field's list.
-
=head2 human_name
Returns the label if present, else the field name.
-=head1 INTERFACE
-
-To be considered a field, you need to implement this role.
-
-It requires the following methods:
-
-=head2 validate
-
-This should take C<value> and set C<valid> to 1 or call C<error>.
-
=head1 SEE ALSO
-L<Spark::Form::Field::Role::Comparable> - Fields that need to match (eg. email)
-L<Spark::Form::Field::Role::Regexp> - Fields validated by a simple regexp
-
+L<Spark::Form::Field::Role::Validateable> - Fields that can be validated
+L<Spark::Form::Field::Role::Printable> - Fields that can be printed
+L<SparkX::Form::BasicValidators> - Set of validators to use creating fields
+L<SparkX::Form::BasicFields> - Ready to use fields
=cut
@@ -0,0 +1,46 @@
+package Spark::Form::Field::Role::Printable;
+
+use Moose::Role;
+
+has label => (
+ isa => 'Str',
+ is => 'rw',
+ required => 0,
+);
+
+1;
+__END__
+
+=head1 NAME
+
+Spark::Form::Field::Role::Printable - Printability for form fields
+
+=head1 DESCRIPTION
+
+A fairly free-form module, this is mostly used for checking that it's printable at all.
+You probably want one of the roles under this hierarchy, but not just this one.
+
+=head1 SYNOPSIS
+
+ package MyApp::Field::CustomText;
+ use Moose;
+ extends 'Spark::Form::Field';
+ with 'Spark::Form::Field::Role::Printable';
+
+ sub to_string {
+ my $self = shift;
+ sprintf("%s: %s",$self->label, $self->value);
+ }
+
+=head1 VARS
+
+=head2 label :: Str [Optional]
+
+A label that will be printed next to said field in the printed out version
+
+=head1 SEE ALSO
+
+L<Spark::Form::Field::Printable::HTML> - Role for being printable under HTML4
+L<Spark::Form::Field::Printable::XHTML> - Role for being printable under XHTML1
+
+=cut
@@ -0,0 +1,39 @@
+package Spark::Form::Field::Role::Printable::HTML;
+
+use Moose::Role;
+with "Spark::Form::Field::Role::Printable";
+
+requires 'to_html';
+
+1;
+__END__
+
+=head1 NAME
+
+Spark::Form::Field::Role::Printable::HTML - a html4-printable form field role
+
+=head1 SYNOPSIS
+
+ package MyApp::Form::Field::CustomText;
+ use Moose;
+ extends 'Spark::Form::Field';
+ with 'Spark::Form::Field::Role::Printable::HTML';
+ use HTML::Tiny;
+
+ sub to_html {
+ my ($self) = @_;
+ my $html = HTML::Tiny->new( mode => 'html' );
+ $html->input({type => 'text', value => $self->value});
+ }
+
+=head1 METHODS
+
+=head2 to_html :: Undef => Str
+
+This function should return a html string representing your control
+
+=head1 SEE ALSO
+
+L<Spark::Form::Field>
+
+=cut
@@ -0,0 +1,39 @@
+package Spark::Form::Field::Role::Printable::XHTML;
+
+use Moose::Role;
+with "Spark::Form::Field::Role::Printable";
+
+requires 'to_xhtml';
+
+1;
+__END__
+
+=head1 NAME
+
+Spark::Form::Field::Role::Printable::XHTML - a xhtml1-printable form field role
+
+=head1 SYNOPSIS
+
+ package MyApp::Form::Field::CustomText;
+ use Moose;
+ extends 'Spark::Form::Field';
+ with 'Spark::Form::Field::Role::Printable::XHTML';
+ use HTML::Tiny;
+
+ sub to_xhtml {
+ my ($self) = @_;
+ my $html = HTML::Tiny->new( mode => 'xml' );
+ $html->input({type => 'text', value => $self->value});
+ }
+
+=head1 METHODS
+
+=head2 to_xhtml :: Undef => Str
+
+This function should return a html string representing your control
+
+=head1 SEE ALSO
+
+L<Spark::Form::Field>
+
+=cut
Oops, something went wrong.

0 comments on commit 278b9d8

Please sign in to comment.