Field validate.pl
EPrints 3 Reference: Directory Structure - Metadata Fields - Repository Configuration - XML Config Files - XML Export Format - EPrints data structure - Core API - Data Objects
field_validate.pl contains configuration for a generalised validating metadata fields within a data object with the validate_field function.
Example
The following example checks if the value(s) of a metadata field:
- Are 'loosely' URLs if the field type is EPrints::MetaField::URL.
- Have both a non-empty family and given sub-field if the field is of type EPrints::MetaField::Name.
- Are email addresses if the field type is EPrints::MetaField::Email.
- Are no longer than the maxlength attribute for the field.
- username is being changed on a EPrints::DataObj::User data object and this will lead it to have the same username as another user. (Unless EPrints allow_duplicate_usernames configuration allows this).
$c->{validate_field} = sub { my( $field, $value, $repository, $for_archive ) = @_; my $xml = $repository->xml(); # only apply checks if the value is set return () if !EPrints::Utils::is_set( $value ); my @problems = (); # CHECKS IN HERE my $values = ref($value) eq "ARRAY" ? $value : [$value]; # closure for generating the field link fragment my $f_fieldname = sub { my $f = defined $field->property( "parent" ) ? $field->property( "parent" ) : $field; my $fieldname = $xml->create_element( "span", class=>"ep_problem_field:".$f->get_name ); $fieldname->appendChild( $f->render_name( $repository ) ); return $fieldname; }; # Loop over actual individual values to check URLs, names and emails foreach my $v (@$values) { next unless EPrints::Utils::is_set( $v ); if( $field->isa( "EPrints::MetaField::Url" ) ) { # Valid URI check (very loose) if( $v !~ /^\w+:/ ) { push @problems, $repository->html_phrase( "validate:missing_http", fieldname=>&$f_fieldname ); } } elsif( $field->isa( "EPrints::MetaField::Name" ) ) { # Check a name has a family part if( !EPrints::Utils::is_set( $v->{family} ) ) { push @problems, $repository->html_phrase( "validate:missing_family", fieldname=>&$f_fieldname ); } # Check a name has a given part elsif( !EPrints::Utils::is_set( $v->{given} ) ) { push @problems, $repository->html_phrase( "validate:missing_given", fieldname=>&$f_fieldname ); } } elsif( $field->isa( "EPrints::MetaField::Email" ) ) { # Check an email looks "ok". Just checks it has only one "@" and no # spaces. if( $v !~ /^[^ \@]+\@[^ \@]+$/ ) { push @problems,$repository->html_phrase( "validate:bad_email", fieldname=>&$f_fieldname ); } } # Check for overly long values # Applies to all subclasses of Id: Text, Longtext, Url etc. if( $field->isa( "EPrints::MetaField::Id" ) ) { if( EPrints::Utils::is_set( $v ) && length($v) > $field->property( "maxlength" ) ) { push @problems, $repository->html_phrase( "validate:truncated", fieldname=>&$f_fieldname ); } } # Unless duplicate usernames are explicitly permitted do not allow username to be changed to that of an existing user if ( $field->get_name eq "username" && $field->dataset->id eq "user" && defined $repository->param( 'dataobj' ) && !$repository->config( "allow_duplicate_usernames" ) ) { my $user_with_username = EPrints::DataObj::User::user_with_username( $repository, $v ); my $current_user = $field->dataset->dataobj( $repository->param( 'dataobj' ) ); if ( $user_with_username->id ne $current_user->id ) { push @problems, $repository->html_phrase( "validate:duplicate_username", username => $repository->make_text( $v ) ); } } } return( @problems ); };