1 package Reaction::UI::ViewPort::Field;
5 class Field is 'Reaction::UI::ViewPort', which {
8 isa => 'Str', is => 'rw', required => 1
12 isa => 'Reaction::InterfaceModel::Action',
13 is => 'ro', required => 0, predicate => 'has_action',
17 isa => 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute',
18 is => 'ro', predicate => 'has_attribute',
22 is => 'rw', lazy_build => 1, trigger_adopt('value'),
23 clearer => 'clear_value',
27 isa => 'Int', is => 'rw', default => 0
30 has label => (isa => 'Str', is => 'rw', lazy_build => 1);
33 isa => 'Str', is => 'rw', required => 1, default => sub { '' }
36 implements BUILD => as {
38 if (!$self->has_attribute != !$self->has_action) {
39 confess "Should have both action and attribute or neither";
43 implements _build_label => as {
45 my $label = join(' ', map { ucfirst } split('_', $self->name));
46 # print STDERR "Field " . $self->name . " has label '$label'\n";
50 implements _build_value => as {
52 if ($self->has_attribute) {
53 my $reader = $self->attribute->get_read_method;
54 my $predicate = $self->attribute->predicate;
55 if (!$predicate || $self->action->$predicate) {
56 return $self->action->$reader;
62 implements adopt_value => as {
64 $self->needs_sync(1) if $self->has_attribute;
67 implements sync_to_action => as {
69 return unless $self->needs_sync && $self->has_attribute && $self->has_value;
70 my $attr = $self->attribute;
71 if (my $tc = $attr->type_constraint) {
72 my $value = $self->value;
73 if ($tc->has_coercion) {
74 $value = $tc->coercion->coerce($value);
76 my $error = $tc->validate($self->value);
78 $self->message($error);
82 my $writer = $attr->get_write_method;
83 confess "No writer for attribute" unless defined($writer);
84 $self->action->$writer($self->value);
88 implements sync_from_action => as {
90 return unless !$self->needs_sync && $self->has_attribute;
91 $self->message($self->action->error_for($self->attribute)||'');
94 override accept_events => sub { ('value', super()) };
102 Reaction::UI::ViewPort::Field
106 This viewport is the base class for all field types.
114 L<Reaction::InterfaceModel::Action>
118 L<Reaction::Meta::InterfaceModel::Action::ParameterAttribute>
126 User friendly label, by default is based on the name.
130 Optional string relating to the field.
134 =head2 L<Reaction::UI::ViewPort>
136 =head2 L<Reaction::UI::ViewPort::DisplayField>
138 =head2 L<Reaction::UI::ViewPort::Field::Boolean>
140 =head2 L<Reaction::UI::ViewPort::Field::ChooseMany>
142 =head2 L<Reaction::UI::ViewPort::Field::ChooseOne>
144 =head2 L<Reaction::UI::ViewPort::Field::DateTime>
146 =head2 L<Reaction::UI::ViewPort::Field::File>
148 =head2 L<Reaction::UI::ViewPort::Field::HiddenArray>
150 =head2 L<Reaction::UI::ViewPort::Field::Number>
152 =head2 L<Reaction::UI::ViewPort::Field::Password>
154 =head2 L<Reaction::UI::ViewPort::Field::String>
156 =head2 L<Reaction::UI::ViewPort::Field::Text>
158 =head2 L<Reaction::UI::ViewPort::Field::TimeRange>
162 See L<Reaction::Class> for authors.
166 See L<Reaction::Class> for the license.