Jump to content
Wiki Home
EPrints Home
Bazaar
Files
Main menu
Main menu
move to sidebar
hide
Navigation
Front Page
EPrints 3 Manual
EPrints Training Course
EPrints Glossary
Installing EPrints
How-to Configure EPrints
EPrints 3.2+ API
EPrints 3 Reference
EPrints Plugins
Wiki
All categories
Recent changes
Main Page
Help
EPrints Documentation
Search
Search
Appearance
Log in
Request account
Personal tools
Log in
Request account
Pages for logged out editors
learn more
Contributions
Talk
Editing
Field validate.pl
(section)
Page
Discussion
British English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Example == The following example checks if the value(s) of a metadata field: # Are 'loosely' URLs if the field type is [[API:EPrints/MetaField/Url|EPrints::MetaField::URL]]. # Have both a non-empty <tt>family</tt> and <tt>given</tt> sub-field if the field is of type [[API:EPrints/MetaField/Name|EPrints::MetaField::Name]]. # Are email addresses if the field type is [[API:EPrints/MetaField/Email|EPrints::MetaField::Email]]. # Are no longer than the <tt>maxlength</tt> attribute for the field. # <tt>username</tt> is being changed on a [[API:EPrints/DataObj/User|EPrints::DataObj::User]] data object and this will lead it to have the same username as another user. (Unless EPrints <tt>allow_duplicate_usernames</tt> 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 ); };
Summary:
Please note that all contributions to EPrints Documentation may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
EPrints Documentation:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)