summaryrefslogtreecommitdiffstats
path: root/extensions/Example
diff options
context:
space:
mode:
authorMax Kanat-Alexander <mkanat@bugzilla.org>2010-02-11 01:00:32 +0100
committerMax Kanat-Alexander <mkanat@bugzilla.org>2010-02-11 01:00:32 +0100
commitf6fc02e572ba07782a272e8fc8f9f45e13ba4896 (patch)
treef88912a82e4a85661d61716e5f3975ea35ed838b /extensions/Example
parent80477beb472a83f6fa711358372db9edf6209c39 (diff)
downloadbugzilla-f6fc02e572ba07782a272e8fc8f9f45e13ba4896.tar.gz
bugzilla-f6fc02e572ba07782a272e8fc8f9f45e13ba4896.tar.xz
Bug 545524: New Hook: object_validators
r=mkanat, a=mkanat (module owner)
Diffstat (limited to 'extensions/Example')
-rw-r--r--extensions/Example/Extension.pm40
-rw-r--r--extensions/Example/template/en/default/hook/global/user-error-errors.html.tmpl3
2 files changed, 43 insertions, 0 deletions
diff --git a/extensions/Example/Extension.pm b/extensions/Example/Extension.pm
index 1a483ad53..5f4cdf995 100644
--- a/extensions/Example/Extension.pm
+++ b/extensions/Example/Extension.pm
@@ -25,6 +25,7 @@ use strict;
use base qw(Bugzilla::Extension);
use Bugzilla::Constants;
+use Bugzilla::Error;
use Bugzilla::Group;
use Bugzilla::User;
use Bugzilla::Util qw(diff_arrays html_quote);
@@ -376,6 +377,45 @@ sub object_end_of_update {
}
}
+sub object_validators {
+ my ($self, $args) = @_;
+ my ($class, $validators) = @$args{qw(class validators)};
+
+ if ($class->isa('Bugzilla::Bug')) {
+ # This is an example of adding a new validator.
+ # See the _check_example subroutine below.
+ $validators->{example} = \&_check_example;
+
+ # This is an example of overriding an existing validator.
+ # See the check_short_desc validator below.
+ my $original = $validators->{short_desc};
+ $validators->{short_desc} = sub { _check_short_desc($original, @_) };
+ }
+}
+
+sub _check_example {
+ my ($invocant, $value, $field) = @_;
+ warn "I was called to validate the value of $field.";
+ warn "The value of $field that I was passed in is: $value";
+
+ # Make the value always be 1.
+ my $fixed_value = 1;
+ return $fixed_value;
+}
+
+sub _check_short_desc {
+ my $original = shift;
+ my $invocant = shift;
+ my $value = $invocant->$original(@_);
+ if ($value !~ /example/i) {
+ # Uncomment this line to make Bugzilla throw an error every time
+ # you try to file a bug or update a bug without the word "example"
+ # in the summary.
+ #ThrowUserError('example_short_desc_invalid');
+ }
+ return $value;
+}
+
sub page_before_template {
my ($self, $args) = @_;
diff --git a/extensions/Example/template/en/default/hook/global/user-error-errors.html.tmpl b/extensions/Example/template/en/default/hook/global/user-error-errors.html.tmpl
index df5a203dd..50d20a9f2 100644
--- a/extensions/Example/template/en/default/hook/global/user-error-errors.html.tmpl
+++ b/extensions/Example/template/en/default/hook/global/user-error-errors.html.tmpl
@@ -9,4 +9,7 @@
[% IF error == "example_my_error" %]
[% title = "Example Error Title" %]
This is the error message! It contains <em>some html</em>.
+[% ELSIF error == "example_short_desc_invalid" %]
+ [% title = "Bad Summary" %]
+ The Summary must contain the word "example".
[% END %]