| 1 | package Strehler::FormFu::Validator::CategoryUnique; | ||||||||||||||||||||||||||||
| 2 | 5 | $Strehler::FormFu::Validator::CategoryUnique::VERSION = '1.6.8'; | |||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Strehler::Meta::Category; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use base 'HTML::FormFu::Validator'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 14 | ▶ | sub validate_value { | ||||||||||||||||||||||||||
|
Subroutine
validate_value: called CC=7 SCAR=19.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 12 | 14 | my $query = $self->form->query(); | |||||||||||||||||||||||||||
| 13 | 14 | ▶ | my $parent = $query->param('parent') || ""; | ||||||||||||||||||||||||||
|
Condition100%2/2
$query->param('parent') || ''
MC/DC100%1/1
$query->param('parent') || ''
$query->param('parent')
Truth table100%2/2
A
A= $query->param('parent')
| |||||||||||||||||||||||||||||
| 14 | 14 | my $category = $query->param('category'); | |||||||||||||||||||||||||||
| 15 | 14 | ▶ | my $prev_parent = $query->param('prev-parent') || ""; | ||||||||||||||||||||||||||
|
Condition50%1/2
$query->param('prev-parent') || ''
MC/DC0%0/1
$query->param('prev-parent') || ''
$query->param('prev-parent')
Truth table50%1/2
A
A= $query->param('prev-parent')
| |||||||||||||||||||||||||||||
| 16 | 14 | ▶ | my $prev_name = $query->param('prev-name') || ""; | ||||||||||||||||||||||||||
|
Condition50%1/2
$query->param('prev-name') || ''
MC/DC0%0/1
$query->param('prev-name') || ''
$query->param('prev-name')
Truth table50%1/2
A
A= $query->param('prev-name')
| |||||||||||||||||||||||||||||
| 17 | 14 | my $category_name; | |||||||||||||||||||||||||||
| 18 | 14 | ▶ | if($parent eq $prev_parent && $category eq $prev_name) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$parent eq $prev_parent and $category eq $prev_name
MC/DC0%0/2
$parent eq $prev_parent and $category eq $prev_name
$parent eq $prev_parent
$category eq $prev_name
Truth table66%2/3
A and B
A= $parent eq $prev_parentB= $category eq $prev_name
| |||||||||||||||||||||||||||||
| 19 | { | ||||||||||||||||||||||||||||
| 20 | 0 | return 1; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | 14 | ▶ | if($parent) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | { | ||||||||||||||||||||||||||||
| 24 | 4 | my $parent_element = Strehler::Meta::Category->new($parent); | |||||||||||||||||||||||||||
| 25 | 4 | $category_name = $parent_element->ext_name() . '/' . $category; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | else | ||||||||||||||||||||||||||||
| 28 | { | ||||||||||||||||||||||||||||
| 29 | 10 | $category_name = $category; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 14 | my $category_element = Strehler::Meta::Category->explode_name($category_name); | |||||||||||||||||||||||||||
| 32 | 14 | return ! $category_element->exists(); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =encoding utf8 | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head1 NAME | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | Strehler::FormFu::Validator::CategoryUnique - FormFu Validator for Category form. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | A FormFu Validator to ensure that a user can't insert a category with the same name of a category already inserted. | ||||||||||||||||||||||||||||
| 44 | Parent value of the form must be checked because two category with the same name under different parents can exist. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | This validator hasn't the standard HTML::FormFu validators namespace because it makes sense only in a Strehler system. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | In category form configuration: | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | - name: category | ||||||||||||||||||||||||||||
| 53 | label: Category | ||||||||||||||||||||||||||||
| 54 | constraints: | ||||||||||||||||||||||||||||
| 55 | - type: Required | ||||||||||||||||||||||||||||
| 56 | message: 'Category needed' | ||||||||||||||||||||||||||||
| 57 | - type: Regex | ||||||||||||||||||||||||||||
| 58 | regex: '^[^\/]*$' | ||||||||||||||||||||||||||||
| 59 | message: 'Invalid character' | ||||||||||||||||||||||||||||
| 60 | validators: | ||||||||||||||||||||||||||||
| 61 | - type: '+Strehler::FormFu::Validator::CategoryUnique' | ||||||||||||||||||||||||||||
| 62 | message: 'Category already exists' | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 5 | 1; | |||||||||||||||||||||||||||