summaryrefslogtreecommitdiff
path: root/lib/VNDB/Handler
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2015-09-20 16:14:59 +0200
committerYorhel <git@yorhel.nl>2015-09-20 16:14:59 +0200
commited86cfd12b0bed7352e2be525b8e63cb4d6d5448 (patch)
tree321f346791cbff80c8355a736e6f4ecb90084d49 /lib/VNDB/Handler
parentbb7a7cc178144457d401c3c6479a41788cf681dd (diff)
formValidate: Add json template and remove json_validate() function
This is less convenient than I had expected, because all the form handling code is designed to work with plain strings rather than any scalar. This means the json data has to be encoded again to get into $frm (not doing this means that, if the form didn't validate, the field won't be filled out correctly). And then decoded for validation, and then encoded again for comparison. I suspect the better solution is to fix the form handling code to handle arbitrary data structures: comparison can be done by deep comparison rather than a simple string compare, and the form generator can auto-encode-to-json if it sees a complex object. Another advantage of this solution is that the comparison function can be less strict with respect to number formatting. In the current scheme you have to be very careful that numbers are not automatically coerced into string format, otherwise the comparison will fail. Either way, that's an idea for the future...
Diffstat (limited to 'lib/VNDB/Handler')
-rw-r--r--lib/VNDB/Handler/Staff.pm15
-rw-r--r--lib/VNDB/Handler/VNEdit.pm31
2 files changed, 22 insertions, 24 deletions
diff --git a/lib/VNDB/Handler/Staff.pm b/lib/VNDB/Handler/Staff.pm
index 52b2e1f1..753a1ef8 100644
--- a/lib/VNDB/Handler/Staff.pm
+++ b/lib/VNDB/Handler/Staff.pm
@@ -209,26 +209,25 @@ sub edit {
{ post => 'l_site', required => 0, template => 'weburl', maxlength => 250, default => '' },
{ post => 'l_twitter', required => 0, maxlength => 16, default => '', regex => [ qr/^\S+$/, mt('_staffe_form_tw_err') ] },
{ post => 'l_anidb', required => 0, template => 'id', default => undef },
- { post => 'aliases', required => 0, maxlength => 5000, default => '' },
+ { post => 'aliases', template => 'json', json_fields => [
+ { field => 'name', required => 1, maxlength => 200 },
+ { field => 'orig', required => 0, maxlength => 200, default => '' },
+ { field => 'aid', required => 0, template => 'uint', default => 0 },
+ ]},
{ post => 'editsum', template => 'editsum' },
{ post => 'ihid', required => 0 },
{ post => 'ilock', required => 0 },
);
- my $aliases = json_validate($frm, 'aliases',
- { field => 'name', required => 1, maxlength => 200 },
- { field => 'orig', required => 0, maxlength => 200, default => '' },
- { field => 'aid', required => 0, template => 'id', default => 0 },
- );
if(!$frm->{_err}) {
+ my $aliases = json_decode $frm->{aliases};
$aliases = [ sort { $a->{name} cmp $b->{name} } @$aliases ];
my %old_aliases = $sid ? ( map +($_->{id} => 1), @{$self->dbStaffAliasIds($sid)} ) : ();
$frm->{primary} = 0 unless exists $old_aliases{$frm->{primary}};
# reset aid to zero for newly added aliases.
$_->{aid} *= $old_aliases{$_->{aid}} ? 1 : 0 for (sort { $a->{name} cmp $b->{name} } @$aliases);
- }
- if(!$frm->{_err}) {
+
$frm->{aliases} = json_encode $aliases;
$frm->{ihid} = $frm->{ihid} ?1:0;
$frm->{ilock} = $frm->{ilock}?1:0;
diff --git a/lib/VNDB/Handler/VNEdit.pm b/lib/VNDB/Handler/VNEdit.pm
index 409c80cb..1883b072 100644
--- a/lib/VNDB/Handler/VNEdit.pm
+++ b/lib/VNDB/Handler/VNEdit.pm
@@ -117,38 +117,37 @@ sub edit {
{ post => 'anime', required => 0, default => '' },
{ post => 'image', required => 0, default => 0, template => 'id' },
{ post => 'img_nsfw', required => 0, default => 0 },
- { post => 'credits', required => 0, default => '[]', maxlength => 5000 },
- { post => 'seiyuu', required => 0, default => '[]', maxlength => 5000 },
+ { post => 'credits', template => 'json', json_fields => [
+ { field => 'aid', required => 1, template => 'id' },
+ { field => 'role', required => 1, enum => $self->{staff_roles} },
+ { field => 'note', required => 0, maxlength => 250, default => '' },
+ ]},
+ { post => 'seiyuu', template => 'json', json_fields => [
+ { field => 'aid', required => 1, template => 'id' },
+ { field => 'cid', required => 1, template => 'id' },
+ { field => 'note', required => 0, maxlength => 250, default => '' },
+ ]},
{ post => 'vnrelations', required => 0, default => '', maxlength => 5000 },
{ post => 'screenshots', required => 0, default => '', maxlength => 1000 },
{ post => 'editsum', required => !$nosubmit, template => 'editsum' },
{ post => 'ihid', required => 0 },
{ post => 'ilock', required => 0 },
);
- my $raw_c = !$frm->{_err} && json_validate($frm, 'credits',
- { field => 'aid', required => 1, template => 'id' },
- { field => 'role', required => 1, enum => $self->{staff_roles} },
- { field => 'note', required => 0, maxlength => 300, default => '' },
- );
- my $raw_s = !$frm->{_err} && json_validate($frm, 'seiyuu',
- { field => 'aid', required => 1, template => 'id' },
- { field => 'cid', required => 1, template => 'id' },
- { field => 'note', required => 0, maxlength => 300, default => '' },
- );
-
# handle image upload
$frm->{image} = _uploadimage($self, $frm) if !$nosubmit;
+ my $raw_c = !$frm->{_err} && json_decode $frm->{credits};
+ my $raw_s = !$frm->{_err} && json_decode $frm->{seiyuu};
my (@credits, @seiyuu);
if(!$nosubmit && !$frm->{_err}) {
# ensure submitted alias IDs exist within database
my @alist = map $_->{aid}, @$raw_c, @$raw_s;
- my %staff = @alist ? map +($_->{aid} => 1), @{$self->dbStaffGet(aid => \@alist, results => 200)} : ();
+ my %staff = @alist ? map +($_->{aid}, 1), @{$self->dbStaffGet(aid => \@alist, results => 200)} : ();
# check for duplicate credits
my $last_c = { aid => 0, role => '' };
for my $c (sort { $a->{aid} <=> $b->{aid} || $a->{role} cmp $b->{role} } @$raw_c) {
- next unless exists $staff{$c->{aid}};
+ next unless exists $staff{0+$c->{aid}};
# discard entries with identical name & role
next if $last_c->{aid} == $c->{aid} && $last_c->{role} eq $c->{role};
push @credits, $c;
@@ -160,7 +159,7 @@ sub edit {
my %vn_chars = map +($_->{id} => 1), @$chars;
my $last_s = { aid => 0, cid => 0 };
for my $s (sort { $a->{aid} <=> $b->{aid} || $a->{cid} <=> $b->{cid} } @$raw_s) {
- next unless $staff{$s->{aid}} && $vn_chars{$s->{cid}}; # weed out odd credits
+ next unless $staff{0+$s->{aid}} && $vn_chars{0+$s->{cid}}; # weed out odd credits
next if $last_s->{aid} == $s->{aid} && $last_s->{cid} == $s->{cid};
push @seiyuu, $s;
$last_s = $s;