4 CXGN::Page::Form::ElementI -- a parent class for all form elements (such as edit boxes and drop down menus)
10 package CXGN
::Page
::Form
::ElementI
;
14 The following error return codes are defined in this class and are
19 $INTEGER_REQUIRED_ERROR
21 $NUMBER_REQUIRED_ERROR
27 $ALLELE_NAME_REQUIRED_ERROR
29 $LENGTH_EXCEEDED_ERROR # not yet implemented
34 our $INPUT_REQUIRED_ERROR = 1;
35 our $INTEGER_REQUIRED_ERROR = 2;
36 our $NUMBER_REQUIRED_ERROR = 3;
37 our $TOKEN_REQUIRED_ERROR= 4;
38 our $DATE_REQUIRED_ERROR=6;
39 our $LENGTH_EXCEEDED_ERROR = 5;
40 our $UNIQUE_REQUIRED_ERROR=7;
41 our $ALLELE_SYMBOL_REQUIRED_ERROR=8;
51 Args: a hash with the following keys:
52 display_name (name of the field for display purposes)
53 field_name (name of the form element)
54 id (the id of the html node)
55 contents (varies by field type; usually the node value)
56 selected (varies by field type; not used by all fields)
57 object (the object this field maps to)
58 getter (the getter function for this field in the object)
59 setter (the setter function for this field in the object)
60 validate (a string describing how to validate the user input; see set_validate() below)
62 All keys correspond to object properties with getter/setters
63 which are described in more detail below.
72 my $self = bless {}, $class;
74 # print STDERR "\nCLASS: $class.\n";
75 # foreach my $k (keys %args) { print STDERR "Args to add_select $k, $args{$k}\n"; }
76 if (!$args{field_name
}) {
78 foreach my $k (keys %args) {
79 $args_list .= "$k ($args{$k}) | ";
82 die qq { Usage
: CXGN
::Page
::Form
::ElementI
->new( display_name
=>"Foo:", field_name
=>"foo", contents
=>"bar", selected
=>0, length => 20, object
=> \
$foo_obj , setter
=>"set_foo", getter
=>"get_foo", validate
=>1)<br
/> Your argument list is
: $args_list. field_name is required
, others are optional
};
85 $self->set_display_name($args{display_name
});
86 $self->set_field_name($args{field_name
});
87 $self->set_id($args{id
});
88 $self->set_contents($args{contents
});
89 $self->set_selected($args{selected
});
90 $self->set_length($args{length});
91 $self->set_object($args{object
});
92 $self->set_object_getter($args{getter
});
93 $self->set_object_setter($args{setter
});
94 $self->set_store_enabled(1); #default is to store
95 $args{validate
} = "" if !defined($args{validate
}); #avoid error messages about uninitialized values in comparison
96 $self->set_validate($args{validate
});
97 my $formatting = $args{formatting
} || "";
98 $self->set_formatting($formatting);
103 =head2 get_display_name
114 sub get_display_name
{
116 return $self->{display_name
};
120 =head2 set_display_name
131 sub set_display_name
{
133 $self->{display_name
}=shift;
136 =head2 get_field_name
149 if (!exists($self->{field_name
})) { $self->{field_name
} = ""; }
150 return $self->{field_name
};
154 =head2 set_field_name
167 $self->{field_name
}=shift;
183 if (!exists($self->{id
})) { $self->{id
} = ""; }
217 if (!exists($self->{contents
})) { $self->{contents
}=""; }
218 return $self->{contents
};
235 $self->{contents
}=shift;
238 =head2 set_from_external
240 Set the appropriate fields ('contents', 'selected', etc) from a value not in the form of an ElementI,
241 such as one taken from a database or a request string.
242 Meant to be overridden by some provided subclasses (eg Checkbox, which uses its contents field unusually)
243 and some user-defined ones.
245 Args: a string with all provided values for this field separated by \0
249 sub set_from_external
251 my ($self, $value) = @_;
252 $self->set_contents($value);
269 if (!exists($self->{length})) { $self->{length}=20; }
270 return $self->{length};
287 $self->{length}=shift;
305 if (!exists($self->{selected
})) { $self->{selected
}=""; }
306 return $self->{selected
};
323 $self->{selected
}=shift;
341 if (!exists($self->{object
})) { $self->{object
}=""; }
342 return $self->{object
};
359 $self->{object
}=shift;
362 =head2 get_object_getter
373 sub get_object_getter
{
375 if (!exists($self->{object_getter
})) { $self->{object_getter
}=""; }
376 return $self->{object_getter
};
380 =head2 set_object_getter
391 sub set_object_getter
{
393 $self->{object_getter
}=shift;
396 =head2 get_object_setter
407 sub get_object_setter
{
409 if (!exists($self->{object_setter
})) { $self->{object_setter
}=""; }
410 return $self->{object_setter
};
414 =head2 set_object_setter
425 sub set_object_setter
{
427 $self->{object_setter
}=shift;
430 =head2 is_store_enabled
434 Ret: whether this field will store its value on the next form submission
441 sub is_store_enabled
{
443 return $self->{store_enabled
};
447 =head2 set_store_enabled
449 Usage: default is ON for all fields
452 Args: a value that will be interpreted in Boolean context
458 sub set_store_enabled
{
459 my ($self, $enable) = @_;
460 $self->{store_enabled
} = $enable;
464 =head2 accessors get_formatting, set_formatting
466 Usage: handle html tag formatting
476 return $self->{formatting
};
481 $self->{formatting
} = shift;
498 if (!exists($self->{validate
})) { $self->{validate
}=""; }
499 return $self->{validate
};
509 o a true value (1). This means that the
510 field is required, any data type may be given.
511 o "string": same as 1
512 o "integer": this field requires an integer
513 o "number": this field requires a number (maybe in
515 o "token": a string consisting of letter and numbers
516 only, no spaces allowed.
517 o "date": a string of the format \d+[-/]\d+[-/]\d+
518 o "unique": 1 if it must be a unique field.
519 (the database accessor class needs to implement
520 the exists_in_database function, as described in
521 L<CXGN::DB::ModifiableI>. Uniqueness
522 should also be enforced on the database level.
530 my $validation = shift;
531 if ($validation && ($validation!~/1|string|integer|number|token|date|allele_symbol/i)) {
532 die "unknown validation type";
534 $self->{validate
}=$validation;
541 Ret: an error code if the field does not validate,
542 0 if the field validates.
544 $INPUT_REQUIRED_ERROR
545 $INTEGER_REQUIRED_ERROR
546 $NUMBER_REQUIRED_ERROR
548 $TOKEN_REQUIRED_ERROR
549 $LENGTH_EXCEEDED_ERROR
550 $UNIQUE_REQUIRED_ERROR
551 $ALLELE_SYMBOL_REQUIRED_ERROR
560 if (($self->get_validate()=~/1|string/i) && !$self->get_contents()) {
561 return $INPUT_REQUIRED_ERROR;
563 if (($self->get_validate()=~/integer/i) && ($self->get_contents()!~/^\s*\-?\d+\s*$/) ) {
564 return $INTEGER_REQUIRED_ERROR;
566 if (($self->get_validate()=~/number/i) && ($self->get_contents() !~ /\s*\-?\d+(\.)?[Ee]?\-?\d*/) ) {
567 return $NUMBER_REQUIRED_ERROR;
569 if (($self->get_validate()=~/token/i) && ($self->get_contents() !~ m/^[A-Za-z0-9\-\_]+$/)) {
570 return $TOKEN_REQUIRED_ERROR;
572 if (($self->get_validate()=~/date/i) && ($self->get_contents() !~ /^\d+[-\/]\d
+[-\
/]+\d+$/)) {
573 return $DATE_REQUIRED_ERROR;
575 if (($self->get_validate()=~/allele_symbol/i) && ($self->get_contents() !~ /^[A-Za-z0-9]+(\-\d+)?$/)) {
576 return $ALLELE_SYMBOL_REQUIRED_ERROR;
578 # if ( length($self->get_contents())>$self->get_length()) {
579 # return $LENGTH_EXCEEDED_ERROR;
582 if ($self->get_validate()=~/unique/i) {
583 if ($self->get_object()->can("exists_in_database")) {
584 if ($self->get_object()->exists_in_database()) {
585 return $UNIQUE_REQUIRED_ERROR;
591 # print STDERR "VALIDATION OF FIELD ".$self->get_field_name()." CONTENTS: ".$self->get_contents()." - NO ERROR DETECTED\n";
592 # the field has validated
609 print STDERR
"ElementI does not render anything...please subclass.\n";