]> Dogcows Code - chaz/p5-CGI-Ex/blob - t/1_validate_12_change.t
CGI::Ex 2.10
[chaz/p5-CGI-Ex] / t / 1_validate_12_change.t
1 # -*- Mode: Perl; -*-
2
3 =head1 NAME
4
5 1_validate_12_change.t - Test CGI::Ex::Validate's ability to modify form fields
6
7 =cut
8
9 use strict;
10 use Test::More tests => 5;
11 use strict;
12
13 use_ok('CGI::Ex::Validate');
14 my $e;
15 my $v;
16 sub validate { scalar CGI::Ex::Validate::validate(@_) }
17
18
19 ###----------------------------------------------------------------###
20
21 $v = {
22 foo => {
23 max_len => 10,
24 replace => 's/[^\d]//g',
25 },
26 };
27
28 $e = validate({
29 foo => '123-456-7890',
30 }, $v);
31 ok(! $e, "Didn't get error");
32
33
34 my $form = {
35 key1 => 'Bu-nch @of characte#rs^',
36 key2 => '123 456 7890',
37 };
38
39
40 $v = {
41 key1 => {
42 replace => 's/[^\s\w]//g',
43 },
44 };
45
46 $e = validate($form, $v);
47 ok(! $e && $form->{key1} eq 'Bunch of characters', "No error and key1 updated");
48
49 $v = {
50 key2 => {
51 replace => 's/(\d{3})\D*(\d{3})\D*(\d{4})/($1) $2-$3/g',
52 },
53 };
54
55 $e = validate($form, $v);
56 ok(! $e && $form->{key2} eq '(123) 456-7890', "No error and phone updated");
57
58 $v = {
59 key2 => {
60 replace => 's/.+//g',
61 required => 1,
62 },
63 };
64
65 $e = validate($form, $v);
66 ok($e && $form->{key2} eq '', "Error with all replaced");
67
This page took 0.03804 seconds and 4 git commands to generate.