]> Dogcows Code - chaz/p5-CGI-Ex/blob - t/1_validate_12_change.t
CGI::Ex 2.27
[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 => 10;
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 key3 => '123',
38 };
39
40
41 $v = {
42 key1 => {
43 replace => 's/[^\s\w]//g',
44 },
45 };
46
47 $e = validate($form, $v);
48 ok(! $e, "No error");
49 is($form->{'key1'}, 'Bunch of characters', "key1 updated");
50
51 $v = {
52 key2 => {
53 replace => 's/(\d{3})\D*(\d{3})\D*(\d{4})/($1) $2-$3/g',
54 },
55 };
56
57 $e = validate($form, $v);
58 ok(! $e, "No error");
59 is($form->{'key2'}, '(123) 456-7890', "Phone updated");
60
61 $v = {
62 key2 => {
63 replace => 's/.+//g',
64 required => 1,
65 },
66 };
67
68 $e = validate($form, $v);
69 ok($e, "Error");
70 is($form->{'key2'}, '', "All replaced");
71
72 $v = {
73 key3 => {
74 replace => 's/\d//',
75 },
76 };
77 $e = validate($form, $v);
78 ok(! $e, "No error");
79 is($form->{'key3'}, '23', "Non-global is fine");
This page took 0.039286 seconds and 4 git commands to generate.