/usr/share/perl5/HTML/Widget.pm is in libhtml-widget-perl 1.11-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 | package HTML::Widget;
use warnings;
use strict;
use base 'HTML::Widget::Accessor';
use HTML::Widget::Result;
use Scalar::Util 'blessed';
use Carp qw/croak/;
# For PAR
use Module::Pluggable::Fast
search =>
[qw/HTML::Widget::Element HTML::Widget::Constraint HTML::Widget::Filter/],
require => 1;
__PACKAGE__->plugins;
__PACKAGE__->mk_accessors(
qw/container indicator query subcontainer uploads strict empty_errors
element_container_class xhtml_strict unwrapped explicit_ids/
);
__PACKAGE__->mk_ro_accessors(qw/implicit_subcontainer/);
# Custom attr_accessor for id provided later
__PACKAGE__->mk_attr_accessors(qw/action enctype method/);
use overload '""' => sub { return shift->attributes->{id} }, fallback => 1;
*const = \&constraint;
*elem = \&element;
*name = \&id;
*tag = \&container;
*subtag = \&subcontainer;
*result = \&process;
*indi = \&indicator;
*constrain_all = \*constraint_all;
our $VERSION = '1.11';
=head1 NAME
HTML::Widget - HTML Widget And Validation Framework
=head1 NOTE
L<HTML::Widget> is no longer under active development and the current
maintainers are instead pursuing an intended replacement (see the
L<mailing-list|/SUPPORT> for details).
Volunteer maintainers / developers for L<HTML::Widget>, please contact
the L<mailing-list|/SUPPORT>.
=head1 SYNOPSIS
use HTML::Widget;
# Create a widget
my $w = HTML::Widget->new('widget')->method('get')->action('/');
# Add a fieldset to contain the elements
my $fs = $w->element( 'Fieldset', 'user' )->legend('User Details');
# Add some elements
$fs->element( 'Textfield', 'age' )->label('Age')->size(3);
$fs->element( 'Textfield', 'name' )->label('Name')->size(60);
$fs->element( 'Submit', 'ok' )->value('OK');
# Add some constraints
$w->constraint( 'Integer', 'age' )->message('No integer.');
$w->constraint( 'Not_Integer', 'name' )->message('Integer.');
$w->constraint( 'All', 'age', 'name' )->message('Missing value.');
# Add some filters
$w->filter('Whitespace');
# Process
my $result = $w->process;
my $result = $w->process($query);
# Check validation results
my @valid_fields = $result->valid;
my $is_valid = $result->valid('foo');
my @invalid_fields = $result->have_errors;
my $is_invalid = $result->has_errors('foo');;
# CGI.pm-compatible! (read-only)
my $value = $result->param('foo');
my @params = $result->param;
# Catalyst::Request-compatible
my $value = $result->params->{foo};
my @params = keys %{ $result->params };
# Merge widgets (constraints and elements will be appended)
$widget->merge($other_widget);
# Embed widgets (as fieldset)
$widget->embed($other_widget);
# Get list of elements
my @elements = $widget->get_elements;
# Get list of constraints
my @constraints = $widget->get_constraints;
# Get list of filters
my @filters = $widget->get_filters;
# Complete xml result
[% result %]
[% result.as_xml %]
# Iterate over elements
<form action="/foo" method="get">
[% FOREACH element = result.elements %]
[% element.field_xml %]
[% element.error_xml %]
[% END %]
</form>
# Iterate over validation errors
[% FOREACH element = result.have_errors %]
<p>
[% element %]:<br/>
<ul>
[% FOREACH error = result.errors(element) %]
<li>
[% error.name %]: [% error.message %] ([% error.type %])
</li>
[% END %]
</ul>
</p>
[% END %]
<p><ul>
[% FOREACH element = result.have_errors %]
[% IF result.error( element, 'Integer' ) %]
<li>[% element %] has to be an integer.</li>
[% END %]
[% END %]
</ul></p>
[% FOREACH error = result.errors %]
<li>[% error.name %]: [% error.message %] ([% error.type %])</li>
[% END %]
# XML output looks like this (easy to theme with css)
<form action="/foo/bar" id="widget" method="post">
<fieldset>
<label for="widget_age" id="widget_age_label"
class="labels_with_errors">
Age
<span class="label_comments" id="widget_age_comment">
(Required)
</span>
<span class="fields_with_errors">
<input id="widget_age" name="age" size="3" type="text"
value="24" class="Textfield" />
</span>
</label>
<span class="error_messages" id="widget_age_errors">
<span class="Regex_errors" id="widget_age_error_Regex">
Contains digit characters.
</span>
</span>
<label for="widget_name" id="widget_name_label">
Name
<input id="widget_name" name="name" size="60" type="text"
value="sri" class="Textfield" />
<span class="error_messages" id="widget_name_errors"></span>
</label>
<input id="widget_ok" name="ok" type="submit" value="OK" />
</fieldset>
</form>
=head1 DESCRIPTION
Create easy to maintain HTML widgets!
Everything is optional, use validation only or just generate forms,
you can embed and merge them later.
The API was designed similar to other popular modules like
L<Data::FormValidator> and L<FormValidator::Simple>,
L<HTML::FillInForm> is also built in (and much faster).
This Module is very powerful, don't misuse it as a template system!
=head1 METHODS
=head2 new
Arguments: $name, \%attributes
Return Value: $widget
Create a new HTML::Widget object. The name parameter will be used as the
id of the form created by the to_xml method.
The C<attributes> argument is equivalent to using the L</attributes>
method.
=cut
sub new {
my ( $self, $name, $attrs ) = @_;
$self = bless {}, ( ref $self || $self );
$self->container('form');
$self->subcontainer('fieldset');
$self->name( defined $name ? $name : 'widget' );
if ( defined $attrs ) {
croak 'attributes argument must be a hash-reference'
if ref($attrs) ne 'HASH';
$self->attributes->{$_} = $attrs->{$_} for keys %$attrs;
}
return $self;
}
=head2 action
Arguments: $uri
Return Value: $uri
Get/Set the action associated with the form. The default is no action,
which causes most browsers to submit to the current URI.
=head2 attributes
=head2 attrs
Arguments: %attributes
Arguments: \%attributes
Return Value: $widget
Arguments: none
Return Value: \%attributes
Accepts either a list of key/value pairs, or a hash-ref.
$w->attributes( $key => $value );
$w->attributes( { $key => $value } );
Returns the C<$widget> object, to allow method chaining.
As of v1.10, passing a hash-ref no longer deletes current
attributes, instead the attributes are added to the current attributes
hash.
This means the attributes hash-ref can no longer be emptied using
C<< $w->attributes( { } ); >>. Instead, you may use
C<< %{ $w->attributes } = (); >>.
As a special case, if no arguments are passed, the return value is a
hash-ref of attributes instead of the object reference. This provides
backwards compatibility to support:
$w->attributes->{key} = $value;
L</attrs> is an alias for L</attributes>.
=head2 container
Arguments: $tag
Return Value: $tag
Get/Set the tag used to contain the XML output when as_xml is called on the
HTML::Widget object.
Defaults to C<form>.
=head2 element_container_class
Arguments: $class_name
Return Value: $class_name
Get/Set the container_class override for all elements in this widget. If set to
non-zero value, process will call $element->container_class($class_name) for
each element. Defaults to not set.
See L<HTML::Widget::Element/container_class>.
=head2 elem
=head2 element
Arguments: $type, $name, \%attributes
Return Value: $element
Add a new element to the Widget. Each element must be given at least a type.
The name is used to generate an id attribute on the tag created for the
element, and for form-specific elements is used as the name attribute. The
returned element object can be used to set further attributes, please see
the individual element classes for the methods specific to each one.
The C<attributes> argument is equivalent to using the
L<attributes|HTML::Widget::Element/attributes> method.
If the element starts with a name other than C<HTML::Widget::Element::>,
you can fully qualify the name by using a unary plus:
$self->element( "+Fully::Qualified::Name", $name );
The type can be one of the following:
=over 4
=item L<HTML::Widget::Element::Block>
my $e = $widget->element('Block');
Add a Block element, which by default will be rendered as a C<DIV>.
my $e = $widget->element('Block');
$e->type('img');
=item L<HTML::Widget::Element::Button>
my $e = $widget->element( 'Button', 'foo' );
$e->value('bar');
Add a button element.
my $e = $widget->element( 'Button', 'foo' );
$e->value('bar');
$e->content('<b>arbitrary markup</b>');
$e->type('submit');
Add a button element which uses a C<button> html tag rather than an
C<input> tag. The value of C<content> is not html-escaped, so may contain
html markup.
=item L<HTML::Widget::Element::Checkbox>
my $e = $widget->element( 'Checkbox', 'foo' );
$e->comment('(Required)');
$e->label('Foo');
$e->checked('checked');
$e->value('bar');
Add a standard checkbox element.
=item L<HTML::Widget::Element::Fieldset>
my $e = $widget->element( 'Fieldset', 'foo' );
$e->legend('Personal details');
$e->element('Textfield', 'name');
$e->element('Textarea', 'address');
Adds a nested fieldset element, which can contain further elements.
=item L<HTML::Widget::Element::Hidden>
my $e = $widget->element( 'Hidden', 'foo' );
$e->value('bar');
Add a hidden field. This field is mainly used for passing previously gathered
data between multiple page forms.
=item L<HTML::Widget::Element::Password>
my $e = $widget->element( 'Password', 'foo' );
$e->comment('(Required)');
$e->fill(1);
$e->label('Foo');
$e->size(23);
$e->value('bar');
Add a password field. This is a text field that will not show the user what
they are typing, but show asterisks instead.
=item L<HTML::Widget::Element::Radio>
my $e = $widget->element( 'Radio', 'foo' );
$e->comment('(Required)');
$e->label('Foo');
$e->checked('checked');
$e->value('bar');
Add a radio button to a group. Radio buttons with the same name will work as
a group. That is, only one item in the group will be "on" at a time.
=item L<HTML::Widget::Element::RadioGroup>
my $e = $widget->element( 'RadioGroup', 'name' );
$e->comment('(Required)');
$e->label('Foo'); # Label for whole radio group
$e->value('bar'); # Currently selected value
$e->labels([qw/Fu Bur Garch/]); # default to ucfirst of values
This is a shortcut to add multiple radio buttons with the same name at the
same time. See above.
=item L<HTML::Widget::Element::Reset>
$e = $widget->element( 'Reset', 'foo' );
$e->value('bar');
Create a reset button. The text on the button will default to "Reset", unless
you call the value() method. This button resets the form to its original
values.
=item L<HTML::Widget::Element::Select>
my $e = $widget->element( 'Select', 'foo' );
$e->comment('(Required)');
$e->label('Foo');
$e->size(23);
$e->options( foo => 'Foo', bar => 'Bar' );
$e->selected(qw/foo bar/);
Create a dropdown or multi-select list element with multiple options. Options
are supplied in a key => value list, in which the keys are the actual selected
IDs, and the values are the strings displayed in the dropdown.
=item L<HTML::Widget::Element::Span>
my $e = $widget->element( 'Span' );
$e->content('bar');
Create a simple span tag, containing the given content. Spans cannot be
constrained as they are not entry fields.
The content may be a string, an L<HTML::Element|HTML::Element> object,
or an array-ref of L<HTML::Element|HTML::Element> objects.
=item L<HTML::Widget::Element::Submit>
$e = $widget->element( 'Submit', 'foo' );
$e->value('bar');
Create a submit button. The text on the button will default to "Submit", unless
you call the value() method.
$e = $widget->element( 'Submit', 'foo' );
$e->value('bar');
$e->src('image.png');
$e->width(100);
$e->height(35);
Create an image submit button. The button will be displayed as an image,
using the file at url C<src>.
=item L<HTML::Widget::Element::Textarea>
my $e = $widget->element( 'Textarea', 'foo' );
$e->comment('(Required)');
$e->label('Foo');
$e->cols(30);
$e->rows(40);
$e->value('bar');
$e->wrap('wrap');
Create a textarea field. This is a multi-line input field for text.
=item L<HTML::Widget::Element::Textfield>
my $e = $widget->element( 'Textfield', 'foo' );
$e->comment('(Required)');
$e->label('Foo');
$e->size(23);
$e->maxlength(42);
$e->value('bar');
Create a single line text field.
=item L<HTML::Widget::Element::Upload>
my $e = $widget->element( 'Upload', 'foo' );
$e->comment('(Required)');
$e->label('Foo');
$e->accept('text/html');
$e->maxlength(1000);
$e->size(23);
Create a field for uploading files. This will probably be rendered as a
textfield, with a button for choosing a file.
Adding an Upload element automatically calls
C<< $widget->enctype('multipart/form-data') >> for you.
=back
=cut
sub element {
my ( $self, $type, $name, $attrs ) = @_;
my $abs = $type =~ s/^\+//;
$type = "HTML::Widget::Element::$type" unless $abs;
my $element = $self->_instantiate( $type, { name => $name } );
$element->{_anonymous} = 1 if !defined $name;
if ( $element->isa('HTML::Widget::Element::Block')
and not $element->{_pseudo_block} )
{
push @{ $self->{_elements} }, $element;
}
else {
croak "'$type' element not permitted at top level in xhtml_strict mode"
if $self->xhtml_strict;
my $implicit_subcontainer = $self->_get_implicit_subcontainer;
$implicit_subcontainer->push_content($element);
}
if ( defined $attrs ) {
croak 'attributes argument must be a hash-reference'
if ref($attrs) ne 'HASH';
$element->attributes->{$_} = $attrs->{$_} for keys %$attrs;
}
return $element;
}
sub _first_element {
return $_[0]->{_elements}->[0];
}
sub _get_implicit_subcontainer {
my $self = shift;
return $self->_first_element if ( $self->implicit_subcontainer );
if ( $self->_first_element ) {
croak
"already a top-level container when trying to setup implicit container";
}
$self->{implicit_subcontainer} = 1;
my $container;
if ( $self->subcontainer eq 'fieldset' ) {
$container = $self->_instantiate('HTML::Widget::Element::Fieldset');
}
else {
$container = $self->_instantiate('HTML::Widget::Element::Block');
$container->type( $self->subcontainer );
}
# Save away the parent widget's name for possible later use in
# H::W::Element::Block.
$container->name( '_implicit_' . $self->name );
push @{ $self->{_elements} }, $container;
return $container;
}
=head2 id
=head2 name
Arguments: $name
Return Value: $name
Get or set the widget id.
L</name> is an alias for L</id>.
=cut
# Yuck - the name bodge above requires this nasty if uncommonly used fixup
sub id {
my $self = shift;
if ( $self->implicit_subcontainer
and $_[0]
and $_[0] ne $self->{attributes}->{id} )
{
my $curr = $self->{attributes}->{id};
# fix up legacy widget names
map { $_->name( '_implicit_' . $_[0] ); }
grep { $_->name =~ /^_implicit_$curr/; } @{ $self->{_elements} };
}
return ( $self->{attributes}->{id} || $self ) unless @_ > 0;
$self->{attributes}->{id} = ( @_ == 1 ? $_[0] : [@_] );
return $self;
}
=head2 get_elements
Arguments: %options
Return Value: @elements
my @elements = $self->get_elements;
my @elements = $self->get_elements( type => 'Textfield' );
my @elements = $self->get_elements( name => 'username' );
Returns a list of all elements added to the widget.
If a 'type' argument is given, only returns the elements of that type.
If a 'name' argument is given, only returns the elements with that name.
=cut
sub get_elements {
my ( $self, %opt ) = @_;
my @elements;
@elements = @{ $self->{_elements} } if $self->{_elements};
@elements = @{ $self->_first_element->content }
if $self->implicit_subcontainer;
return _search_elements( \%opt, @elements );
}
sub _search_elements {
my ( $opt, @elements ) = @_;
if ( exists $opt->{type} ) {
my $type = "HTML::Widget::Element::" . $opt->{type};
@elements = grep { $_->isa($type) } @elements;
}
if ( exists $opt->{name} ) {
@elements = grep {
defined($_->name) && $_->name eq $opt->{name}
} @elements;
}
return @elements;
}
=head2 get_elements_ref
Arguments: %options
Return Value: \@elements
Accepts the same arguments as L</get_elements>, but returns an arrayref
of results instead of a list.
=cut
sub get_elements_ref {
my $self = shift;
return [ $self->get_elements(@_) ];
}
=head2 get_element
Arguments: %options
Return Value: $element
my $element = $self->get_element;
my $element = $self->get_element( type => 'Textfield' );
my $element = $self->get_element( name => 'username' );
Similar to get_elements(), but only returns the first element in the list.
Accepts the same arguments as get_elements().
=cut
sub get_element {
my ( $self, %opt ) = @_;
return ( $self->get_elements(%opt) )[0];
}
=head2 find_elements
Arguments: %options
Return Value: @elements
Similar to L</get_elements>, and accepts the same arguments, but performs a
recursive search through block-level elements.
=cut
sub find_elements {
my ( $self, %opt ) = @_;
my @elements = map { $_->find_elements } @{ $self->{_elements} };
return _search_elements( \%opt, @elements );
}
=head2 const
=head2 constraint
Arguments: $type, @field_names
Return Value: $constraint
Set up a constraint on one or more elements. When process() is called on the
Widget object, with a $query object, the parameters of the query are checked
against the specified constraints. The L<HTML::Widget::Constraint> object is
returned to allow setting of further attributes to be set. The string 'Not_'
can be prepended to each type name to negate the effects. Thus checking for a
non-integer becomes 'Not_Integer'.
If the constraint package name starts with something other than
C<HTML::Widget::Constraint::>, you can fully qualify the name by using a
unary plus:
$self->constraint( "+Fully::Qualified::Name", @names );
Constraint checking is done after all L<HTML::Widget::Filter> have been
applied.
@names should contain a list of element names that the constraint applies to.
The type of constraint can be one of:
=over 4
=item L<HTML::Widget::Constraint::All>
my $c = $widget->constraint( 'All', 'foo', 'bar' );
The fields passed to the "All" constraint are those which are required fields
in the form.
=item L<HTML::Widget::Constraint::AllOrNone>
my $c = $widget->constraint( 'AllOrNone', 'foo', 'bar' );
If any of the fields passed to the "AllOrNone" constraint are filled in, then
they all must be filled in.
=item L<HTML::Widget::Constraint::Any>
my $c = $widget->constraint( 'Any', 'foo', 'bar' );
At least one or more of the fields passed to this constraint must be filled.
=item L<HTML::Widget::Constraint::ASCII>
my $c = $widget->constraint( 'ASCII', 'foo' );
The fields passed to this constraint will be checked to make sure their
contents contain ASCII characters.
=item L<HTML::Widget::Constraint::Bool>
my $c = $widget->constraint( 'Bool', 'foo' );
The fields passed to this constraint will be checked to make sure their
contents contain a C<1> or C<0>.
=item L<HTML::Widget::Constraint::Callback>
my $c = $widget->constraint( 'Callback', 'foo' )->callback(sub {
my $value=shift;
return 1;
});
This constraint allows you to provide your own callback sub for validation.
The callback sub is called once for each submitted value of each named field.
=item L<HTML::Widget::Constraint::CallbackOnce>
my $c = $widget->constraint( 'CallbackOnce', 'foo' )->callback(sub {
my $value=shift;
return 1;
});
This constraint allows you to provide your own callback sub for validation.
The callback sub is called once per call of L</process>.
=item L<HTML::Widget::Constraint::Date>
my $c = $widget->constraint( 'Date', 'year', 'month', 'day' );
This constraint ensures that the three fields passed in are a valid date.
=item L<HTML::Widget::Constraint::DateTime>
my $c =
$widget->constraint( 'DateTime', 'year', 'month', 'day', 'hour',
'minute', 'second' );
This constraint ensures that the six fields passed in are a valid date and
time.
=item L<HTML::Widget::Constraint::DependOn>
my $c =
$widget->constraint( 'DependOn', 'foo', 'bar' );
If the first field listed is filled in, all of the others are required.
=item L<HTML::Widget::Constraint::Email>
my $c = $widget->constraint( 'Email', 'foo' );
Check that the field given contains a valid email address, according to RFC
2822, using the L<Email::Valid> module.
=item L<HTML::Widget::Constraint::Equal>
my $c = $widget->constraint( 'Equal', 'foo', 'bar' );
$c->render_errors( 'foo' );
The fields passed to this constraint must contain the same information, or
be empty.
=item L<HTML::Widget::Constraint::HTTP>
my $c = $widget->constraint( 'HTTP', 'foo' );
This constraint checks that the field(s) passed in are valid URLs. The regex
used to test this can be set manually using the ->regex method.
=item L<HTML::Widget::Constraint::In>
my $c = $widget->constraint( 'In', 'foo' );
$c->in( 'possible', 'values' );
Check that a value is one of a specified set.
=item L<HTML::Widget::Constraint::Integer>
my $c = $widget->constraint( 'Integer', 'foo' );
Check that the field contents are an integer.
=item L<HTML::Widget::Constraint::Length>
my $c = $widget->constraint( 'Length', 'foo' );
$c->min(23);
$c->max(50);
Ensure that the contents of the field are at least $min long, and no longer
than $max.
=item L<HTML::Widget::Constraint::Number>
my $c = $widget->constraint( 'Number', 'foo' );
Ensure that the content of the field is a number.
=item L<HTML::Widget::Constraint::Printable>
my $c = $widget->constraint( 'Printable', 'foo' );
The contents of the given field must only be printable characters. The regex
used to test this can be set manually using the ->regex method.
=item L<HTML::Widget::Constraint::Range>
my $c = $widget->constraint( 'Range', 'foo' );
$c->min(23);
$c->max(30);
The contents of the field must be numerically within the given range.
=item L<HTML::Widget::Constraint::Regex>
my $c = $widget->constraint( 'Regex', 'foo' );
$c->regex(qr/^\w+$/);
Tests the contents of the given field(s) against a user supplied regex.
=item L<HTML::Widget::Constraint::String>
my $c = $widget->constraint( 'String', 'foo' );
The field must only contain characters in \w. i.e. [a-zaZ0-9_]
=item L<HTML::Widget::Constraint::Time>
my $c = $widget->constraint( 'Time', 'hour', 'minute', 'second' );
The three fields passed to this constraint must constitute a valid time.
=back
=cut
sub constraint {
my ( $self, $type, @names ) = @_;
croak('constraint requires a constraint type') unless $type;
my $abs = $type =~ s/^\+//;
my $not = 0;
if ( $type =~ /^Not_(\w+)$/i ) {
$not++;
$type = $1;
}
$type = "HTML::Widget::Constraint::$type" unless $abs;
my $constraint = $self->_instantiate( $type, { names => \@names } );
$constraint->not($not);
push @{ $self->{_constraints} }, $constraint;
return $constraint;
}
=head2 constraint_all
=head2 constrain_all
Arguments: @constraint_types
Return Value: @constraints
$w->element( Textfield => 'name' );
$w->element( Textfield => 'password' );
$w->constraint_all( 'All' );
For each named type, add a constraint to all elements currently defined.
Does not add a constraint for elements which return false for
L<HTML::Widget::Element/allow_constraint>; this includes
L<HTML::Widget::Element::Span> and any element that inherits from
L<HTML::Widget::Element::Block>.
=cut
sub constraint_all {
my $self = shift;
my @constraint;
for my $element ( $self->find_elements ) {
if ( $element->allow_constraint ) {
for (@_) {
push @constraint, $self->constraint( $_, $element->name );
}
}
}
return @constraint;
}
=head2 get_constraints
Arguments: %options
Return Value: @constraints
my @constraints = $self->get_constraints;
my @constraints = $self->get_constraints( type => 'Integer' );
Returns a list of all constraints added to the widget.
If a 'type' argument is given, only returns the constraints of that type.
=cut
sub get_constraints {
my ( $self, %opt ) = @_;
if ( exists $opt{type} ) {
my $type = "HTML::Widget::Constraint::$opt{type}";
return grep { $_->isa($type) } @{ $self->{_constraints} };
}
return @{ $self->{_constraints} };
}
=head2 get_constraints_ref
Arguments: %options
Return Value: \@constraints
Accepts the same arguments as L</get_constraints>, but returns an arrayref
of results instead of a list.
=cut
sub get_constraints_ref {
my $self = shift;
return [ $self->get_constraints(@_) ];
}
=head2 get_constraint
Arguments: %options
Return Value: $constraint
my $constraint = $self->get_constraint;
my $constraint = $self->get_constraint( type => 'Integer' );
Similar to L</get_constraints>, but only returns the first constraint in the
list.
Accepts the same arguments as L</get_constraints>.
=cut
sub get_constraint {
my ( $self, %opt ) = @_;
return ( $self->get_constraints(%opt) )[0];
}
=head2 embed
Arguments: @widgets
Arguments: $element, @widgets
Insert the contents of another widget object into this one. Each embedded
object will be represented as another set of fields (surrounded by a fieldset
tag), inside the created form. No copy is made of the widgets to embed, thus
calling as_xml on the resulting object will change data in the widget objects.
With an element argument, the widgets are embedded into the provided element.
No checks are made on whether the provided element belongs to $self.
Note that without an element argument, embed embeds into the top level
of the widget, and NOT into any subcontainer (whether created by you
or implicitly created). If this is not what you need, you can choose
one of:
# while building $self:
$in_here = $self->element('Fieldset', 'my_fieldset');
# later:
$self->embed($in_here, @widgets);
# these are equivalent:
$self->embed(($self->find_elements)[0], @widgets);
$self->embed_into_first(@widgets); # but this is faster!
If you are just building a widget and do not need to import constraints
and filters from another widget, do not use embed at all, simply
assemble using the methods provided by L<HTML::Widget::Element::Fieldset>.
=cut
sub embed {
my ( $self, @widgets ) = @_;
my $dest;
if ( $widgets[0]->isa('HTML::Widget::Element') ) {
croak "destination element is not a container"
unless $widgets[0]->isa('HTML::Widget::Element::NullContainer');
$dest = shift @widgets;
}
for my $widget (@widgets) {
if ($dest) {
$dest->push_content( @{ $widget->{_elements} } );
}
else {
push @{ $self->{_elements} }, @{ $widget->{_elements} }
if $widget->{_elements};
}
push @{ $self->{_constraints} }, @{ $widget->{_constraints} }
if $widget->{_constraints};
push @{ $self->{_filters} }, @{ $widget->{_filters} }
if $widget->{_filters};
}
return $self;
}
=head2 embed_into_first
Arguments: @widgets
As for L</embed>, but embed into the first subcontainer (fieldset) rather than
into the top level form.
=cut
sub embed_into_first {
my $self = shift;
my $dest = $self->_first_element;
return $self->embed( $dest, @_ );
}
=head2 empty_errors
Arguments: $bool
Return Value: $bool
After validation, if errors are found, a span tag is created with the id
"fields_with_errors". Set this value to cause the span tag to always be
generated.
=head2 enctype
Arguments: $enctype
Return Value: $enctype
Set/Get the encoding type of the form. This can be either
"application/x-www-form-urlencoded" which is the default, or
"multipart/form-data".
See L<http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4>.
If the widget contains an Upload element, the enctype is automatically set to
'multipart/form-data'.
=head2 explicit_ids
Argument: $bool
Return Value: $bool
When true; elements, fieldsets and blocks will not be given DOM id's, unless
explicitly set with L<attributes|HTML::Widget::Accessor/attributes>.
$w->element( 'Textfield', 'foo', {id => 'my_id'} )
The form itself will always be given an L</id>, which is C<widget> by default.
=head2 filter
Arguments: $type, @field_names
Return Value: $filter
Add a filter. Like constraints, filters can be applied to one or more elements.
These are applied to actually change the contents of the fields, supplied by
the user before checking the constraints. It only makes sense to apply filters
to fields that can contain text - Password, Textfield, Textarea, Upload.
If the filter starts with a name other than C<HTML::Widget::Filter::>,
you can fully qualify the name by using a unary plus:
$self->filter( "+Fully::Qualified::Name", @names );
There are currently two types of filter:
=over 4
=item L<HTML::Widget::Filter::Callback>
my $f = $widget->filter( 'Callback', 'foo' );
$f->callback( \&my_callback );
Filter given field(s) using a user-defined subroutine.
=item L<HTML::Widget::Filter::HTMLEscape>
my $f = $widget->filter( 'HTMLEscape', 'foo' );
Escapes HTML entities in the given field(s).
=item L<HTML::Widget::Filter::HTMLStrip>
my $f = $widget->filter( 'HTMLStrip', 'foo' );
Strips HTML tags from the given field(s).
my $f = $widget->filter( 'HTMLStrip', 'foo' );
$f->allow( 'p', 'br' );
Specify a list of HTML tags which shouldn't be stripped.
=item L<HTML::Widget::Filter::LowerCase>
my $f = $widget->filter( 'LowerCase', 'foo' );
Make given field(s) all lowercase.
=item L<HTML::Widget::Filter::TrimEdges>
my $f = $widget->filter( 'TrimEdges', 'foo' );
Removes whitespace from the beginning and end of the given field(s).
=item L<HTML::Widget::Filter::UpperCase>
my $f = $widget->filter( 'UpperCase', 'foo' );
Make given field(s) all uppercase.
=item L<HTML::Widget::Filter::Whitespace>
my $f = $widget->filter( 'Whitespace', 'foo' );
Removes all whitespace from the given field(s).
=back
=cut
sub filter {
my ( $self, $type, @names ) = @_;
my $abs = $type =~ s/^\+//;
$type = "HTML::Widget::Filter::$type" unless $abs;
my $filter = $self->_instantiate( $type, { names => \@names } );
$filter->init($self);
push @{ $self->{_filters} }, $filter;
return $filter;
}
=head2 filter_all
Arguments: @filter_types
Return Value: @filters
$w->element( Textfield => 'name' );
$w->element( Textfield => 'age' );
$w->filter_all( 'Whitespace' );
For each named type, add a filter to all elements currently defined.
Does not add a filter for elements which return false for
C<HTML::Widget::Element/allow_filter>; this includes
L<HTML::Widget::Element::Span> and any element that inherits from
L<HTML::Widget::Element::Block>.
=cut
sub filter_all {
my $self = shift;
my @filter;
for my $element ( $self->find_elements ) {
if ( $element->allow_filter ) {
for (@_) {
push @filter, $self->filter( $_, $element->name );
}
}
}
return @filter;
}
=head2 get_filters
Arguments: %options
Return Value: @filters
my @filters = $self->get_filters;
my @filters = $self->get_filters( type => 'Integer' );
Returns a list of all filters added to the widget.
If a 'type' argument is given, only returns the filters of that type.
=cut
sub get_filters {
my ( $self, %opt ) = @_;
if ( exists $opt{type} ) {
my $type = "HTML::Widget::Filter::$opt{type}";
return grep { $_->isa($type) } @{ $self->{_filters} };
}
return @{ $self->{_filters} };
}
=head2 get_filters_ref
Arguments: %options
Return Value: \@filters
Accepts the same arguments as L</get_filters>, but returns an arrayref
of results instead of a list.
=cut
sub get_filters_ref {
my $self = shift;
return [ $self->get_filters(@_) ];
}
=head2 get_filter
Arguments: %options
Return Value: $filter
my @filters = $self->get_filter;
my @filters = $self->get_filter( type => 'Integer' );
Similar to L</get_filters>, but only returns the first filter in the
list.
Accepts the same arguments as L</get_filters>.
=cut
sub get_filter {
my ( $self, %opt ) = @_;
return ( $self->get_filters(%opt) )[0];
}
=head2 indi
=head2 indicator
Arguments: $field_name
Return Value: $field_name
Set/Get a boolean field. This is a convenience method for the user, so they
can keep track of which of many Widget objects were submitted. It is also
used by L<Catalyst::Plugin::HTML::Widget>
=head2 legend
Arguments: $legend
Return Value: $legend
Set/Get a legend for this widget. This tag is used to label the fieldset.
=cut
sub legend {
my ( $self, $legend ) = @_;
croak "'legend' not permitted at top level in xhtml_strict mode"
if $self->xhtml_strict;
my $top_level = $self->_get_implicit_subcontainer;
unless ( $top_level->can('legend') ) {
croak "implicit subcontainer does not support 'legend'";
}
$top_level->legend($legend);
return $self;
}
=head2 merge
Arguments: @widgets
Arguments: $element, @widgets
Merge elements, constraints and filters from other widgets, into this one. The
elements will be added to the end of the list of elements that have been set
already.
Without an element argument, and with standard widgets, the contents of the
first top-level element of each widget will be merged into the first
top-level element of this widget.
This emulates the previous behaviour.
With an element argument, the widgets are merged into the named element.
No checks are made on whether the provided element belongs to $self.
=cut
sub merge {
my ( $self, @widgets ) = @_;
my $dest;
if ( $widgets[0]->isa('HTML::Widget::Element') ) {
croak "destination element is not a container"
unless $widgets[0]->isa('HTML::Widget::Element::NullContainer');
$dest = shift @widgets;
}
else {
$dest = $self->_first_element;
croak "merge only supported if destination first element is container"
if $dest
and not $dest->isa('HTML::Widget::Element::NullContainer');
$dest = $self->_get_implicit_subcontainer unless $dest;
}
for my $widget (@widgets) {
my $source = $widget->_first_element;
croak "merge only supported if source first element is container"
unless $source
and $source->isa('HTML::Widget::Element::NullContainer');
$dest->push_content( @{ $source->content } );
push @{ $self->{_constraints} }, @{ $widget->{_constraints} }
if $widget->{_constraints};
push @{ $self->{_filters} }, @{ $widget->{_filters} }
if $widget->{_filters};
}
return $self;
}
=head2 method
Arguments: $method
Return Value: $method
Set/Get the method used to submit the form. Can be set to either "post" or
"get". The default is "post".
=head2 result
=head2 process
Arguments: $query, \@uploads
Return Value: $result
After finishing setting up the widget and all its elements, call to create
an L<HTML::Widget::Result>. If passed a C<$query> it will run filters and
validation on the parameters. The L<Result|HTML::Widget::Result> object can
then be used to produce the HTML.
L</result> is an alias for L</process>.
=cut
sub process {
my ( $self, $query, $uploads ) = @_;
my $errors = {};
$query ||= $self->query;
$uploads ||= $self->uploads;
# Some sane defaults
if ( $self->container eq 'form' ) {
$self->attributes->{method} ||= 'post';
}
for my $element ( @{ $self->{_elements} } ) {
$element->prepare($self);
$element->init($self) unless $element->{_initialized};
$element->{_initialized}++;
}
for my $filter ( @{ $self->{_filters} } ) {
$filter->prepare($self);
$filter->init($self) unless $filter->{_initialized};
$filter->{_initialized}++;
}
for my $constraint ( @{ $self->{_constraints} } ) {
$constraint->prepare($self);
$constraint->init($self) unless $constraint->{_initialized};
$constraint->{_initialized}++;
}
my @js_callbacks;
for my $constraint ( @{ $self->{_constraints} } ) {
push @js_callbacks, sub { $constraint->process_js( $_[0] ) };
}
my %params;
if ($query) {
croak "Invalid query object"
unless blessed($query)
and $query->can('param');
my @params = $query->param;
for my $param (@params) {
my @values = $query->param($param);
$params{$param} = @values > 1 ? \@values : $values[0];
}
for my $filter ( @{ $self->{_filters} } ) {
$filter->process( \%params, $uploads );
}
for my $element ( @{ $self->{_elements} } ) {
my $results = $element->process( \%params, $uploads );
for my $result ( @{$results} ) {
my $name = $result->name;
my $class = ref $element;
$class =~ s/^HTML::Widget::Element:://;
$class =~ s/::/_/g;
$result->type($class) if not defined $result->type;
push @{ $errors->{$name} }, $result;
}
}
for my $constraint ( @{ $self->{_constraints} } ) {
my $results = $constraint->process( $self, \%params, $uploads );
my $render = $constraint->render_errors;
my @render =
ref $render ? @{$render}
: defined $render ? $render
: ();
for my $result ( @{$results} ) {
my $name = $result->name;
my $class = ref $constraint;
$class =~ s/^HTML::Widget::Constraint:://;
$class =~ s/::/_/g;
$result->type($class);
$result->no_render(1)
if @render && !grep { $name eq $_ } @render;
push @{ $errors->{$name} }, $result;
}
}
}
return HTML::Widget::Result->new( {
attributes => $self->attributes,
container => $self->container,
_constraints => $self->{_constraints},
_elements => $self->{_elements},
_errors => $errors,
_js_callbacks => \@js_callbacks,
_params => \%params,
subcontainer => $self->subcontainer,
strict => $self->strict,
empty_errors => $self->empty_errors,
submitted => ( $query ? 1 : 0 ),
element_container_class => $self->element_container_class,
implicit_subcontainer => $self->implicit_subcontainer,
explicit_ids => $self->explicit_ids,
} );
}
=head2 query
Arguments: $query
Return Value: $query
Set/Get the query object to use for validation input. The query object can also
be passed to the process method directly.
=head2 strict
Arguments: $bool
Return Value: $bool
Only consider parameters that pass at least one constraint valid.
=head2 subcontainer
Arguments: $tag
Return Value: $tag
Set/Get the subcontainer tag to use.
Defaults to C<fieldset>.
=head2 uploads
Arguments: \@uploads
Return Value: \@uploads
Contains an arrayref of L<Apache2::Upload> compatible objects.
=head2 xhtml_strict
Arguments: $bool
Return Value: $bool
When C<true>, it is an error to have any element at the top-level of the
widget which is not derived from L<HTML::Widget::Element::Block>.
Currently, the only valid element supplied is the
L<HTML::Widget::Element::Fieldset>.
When C<true>, the top-level widget may not have a L/legend>.
=head1 Frequently Asked Questions (FAQ)
=head2 How do I add an onSubmit handler to my form?
$widget->attributes( onsubmit => $javascript );
See L<HTML::Widget/attributes>.
=head2 How do I add an onChange handler to my form field?
$element->attributes( onchange => $javascript );
See L<HTML::Widget::Element/attributes>.
=head2 Element X does not have an accessor for Y!
You can add any arbitrary attributes with
L<HTML::Widget::Element/attributes>.
=head2 How can I add a tag which isn't included?
You can either create your own element module files, and use them as you
would a standard element, or alternatively...
You can call L<type|HTML::Widget::Element::Block/type> on a
L<HTML::Widget::Element::Block> element to change the rendered tag.
$w->element('Block')->type('br');
# will render as
<br />
=head2 How can I render some elements in a HTML list?
my $ul = $w->element('Block')->type('ul');
$ul->element('Block')->type('li')
->element( Textfield => foo' );
$ul->element('Block')->type('li')
->element( Textfield => 'bar' );
# will render as
<ul>
<li>
<input class="textfield" id="widget_foo" name="foo" type="text" />
</li>
<li>
<input class="textfield" id="widget_bar" name="bar" type="text" />
</li>
</ul>
=head1 SUPPORT
Mailing list:
L<http://lists.rawmode.org/cgi-bin/mailman/listinfo/html-widget>
=head1 SUBVERSION REPOSITORY
The publicly viewable subversion code repository is at
L<http://dev.catalyst.perl.org/repos/Catalyst/trunk/HTML-Widget/>.
=head1 SEE ALSO
L<Catalyst> L<Catalyst::Plugin::HTML::Widget> L<HTML::Element>
=head1 AUTHOR
Sebastian Riedel, C<sri@oook.de>
=head1 LICENSE
This library is free software, you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
1;
|