注意:这些示例利用HTML :: FormFu :: Model :: DBIC。从HTML::FormFu V02.005开始,HTML :: FormFu :: Model :: DBIC模块与HTML::FormFu捆绑在一起,并且可以在独立的分布中使用。
use HTML::FormFu;
my $form = HTML::FormFu->new;
$form->load_config_file('form.yml');
$form->process( $cgi_query );
if ( $form->submitted_and_valid ) {
# do something with $form->params
}
else {
# display the form
$template->param( form => $form );
}如果您使用的是催化剂,则可能是一个更合适的例子:
package MyApp::Controller::User;
use Moose;
extends 'Catalyst::Controller::HTML::FormFu';
sub user : Chained CaptureArgs(1) {
my ( $self, $c, $id ) = @_;
my $rs = $c->model('Schema')->resultset('User');
$c->stash->{user} = $rs->find( $id );
return;
}
sub edit : Chained('user') Args(0) FormConfig {
my ( $self, $c ) = @_;
my $form = $c->stash->{form};
my $user = $c->stash->{user};
if ( $form->submitted_and_valid ) {
$form->model->update( $user );
$c->res->redirect( $c->uri_for( "/user/$id" ) );
return;
}
$form->model->default_values( $user )
if ! $form->submitted;
}注意:因为催化剂控制器会自动为您呼吁“过程”;如果您对操作方法中的表单进行任何修改,例如添加或更改元素,添加约束等;您必须在使用“提交的_and_valid”之前再次调用“进程”,即“提交表单值和错误”或“修改提交表单”或呈现表单下列出的任何方法。
这是一个配置文件的示例来创建基本的登录表单(这里的所有示例均为yaml,但是您可以使用Config :: Any支持的任何格式),您还可以在Perl代码中直接创建表单,而不是使用外部配置文件。
---
action: /login
indicator: submit
auto_fieldset: 1
elements:
- type: Text
name: user
constraints:
- Required
- type: Password
name: pass
constraints:
- Required
- type: Submit
name: submit
constraints:
- SingleValueHTML :: FormFu是一个HTML表单框架,其目的是尽可能容易地用于基本的Web表单,但具有执行您可能想做的其他任何操作的力量和灵活性(只要它涉及表单)。
您可以配置FormFu行为和输出的几乎任何部分。默认情况下,FormFu渲染“ XHTML 1.0严格”合规标记,并具有尽可能少的额外标记,但是具有足够的CSS类名称,以允许仅通过更改CSS来生成大量的输出样式。
下面列出的所有方法(“新”除外)可以作为$form对象上的普通方法或配置文件中的选项称为普通方法。示例将主要显示在YAML配置语法中。
该文档遵循以下惯例,即被方[]所包围的方法是可选的,并且需要所有其他参数。
参数:[%选项]
返回值:$表格
创建一个新的HTML :: FormFu对象。
可以将可以在HTML :: FormFu对象上调用的任何方法作为参数传递给“新”。
my $form = HTML::FormFu->new({
action => '/search',
method => 'GET',
auto_fieldset => 1,
}); 参数:$ filename
参数: @filenames
返回值:$表格
接受文件名或文件名列表,该文件名称应为Config :: Any识别的任何格式。
每个配置文件的内容传递给“填充”,因此将其添加到表单中。
可以在配置文件本身中调用“ load_config_file”,以便允许将共同的设置保存在任何形式可以加载的单个配置文件中。
---
load_config_file:
- file1
- file2单个文件中的yaml多个文档。文档启动标记是一条包含3个破折号的行。将按顺序应用多个文档,就像给出了多个文件名一样。
在下面的示例中,添加元素后,利用多个文档来加载另一个配置文件。 (如果这是一个文档,则load_config_file将在elements之前调用,无论其在文件中的位置如何)。
---
elements:
- name: one
- name: two
---
load_config_file: ext.yml如果设置了“ config_file_path”目录,从“ config_file_path”目录解析相对路径,否则从当前的工作目录中解析。
有关组织配置文件的建议,请参见“最佳实践”。
参数:%选项
如果定义,则将参数用于创建data :: visitor ::回调对象在“ load_config_file”期间,该对象可用于在将其发送给“填充”之前预先处理配置。
例如,下面的代码将回调添加到一个表单中,该表单将动态更改“ .yml”以“ .yaml”结尾的任何配置值在.yaml“”呼叫“ load_config_file”:
$form->config_callback({
plain_value => sub {
my( $visitor, $data ) = @_;
s/.yml/.yaml/;
}
});默认值:未定义
此方法是一种特殊的“继承访问者”,这意味着它可以在表单,块元素或单个元素上设置。当读取值时,如果未定义值,则它会自动穿越父母的元素层次结构,通过任何块元素并直至表单,从而搜索定义的值。
参数:%选项
返回值:$表格
每个选项键/值传递的可能是任何HTML :: FormFu方法名称和参数。
提供了一种设置多个值的简单方法,或在单个方法呼叫的表单中添加多个元素。
尝试以半智能顺序调用方法名称(有关详细信息,请参见HTML::FormFu::ObjectUtil中的pupulate()源)。
参数:%默认值
返回值:$表格
从单个Hash-ref设置多个字段的默认值。
Hash-ref的键对应于表单字段的名称,该值将传递给字段的默认方法。
在将所有字段添加到表单中,然后在“过程”之前(否则,在渲染表单之前再次调用“ process”),应调用此功能。
参数:$ Directory_name
“ config_file_path”定义了将在何处搜索配置文件,如果未给出“ load_config_file”的绝对路径。
默认值:未定义
此方法是一种特殊的“继承访问者”,这意味着它可以在表单,块元素或单个元素上设置。当读取值时,如果未定义值,则它会自动穿越父母的元素层次结构,通过任何块元素并直至表单,从而搜索定义的值。
是继承的登录器。
参数:$ field_name
参数:&coderef
如果“指示器”设置为字段名称,则提交的“指标”将返回True,如果该字段名称的值已提交。
如果将“指示器”设置为代码-RF,则将其称为带有两个参数$form和$query子例程,其返回值将用作“已提交”的返回值。
如果未设置“指示器”,则提交的“指标”将返回True,如果提交任何已知字段名称的值。
参数:1
参数:%选项
返回值:$ fieldSet
此设置适用于大多数基本表单,这意味着您通常可以忽略自己添加字段集。
调用$form->auto_fieldset(1)立即将字段集元素添加到表单中。此后, $form->elements()将将所有元素(字段集除外)添加到该字段集,而不是直接到表单中。
具体而言,将元素添加到表单上的最后一个字段集中,因此,如果添加其他字段集,则将添加任何其他元素。
另外,您可以将hashref传递给auto_fieldset(),这将用于为创建的第一个fieldset设置默认设置。
一些例子及其输出,以证明:
2个没有现场集的元素。
---
elements:
- type: Text
name: foo
- type: Text
name: bar
<form action="" method="post">
<div class="text">
<input name="foo" type="text" />
</div>
<div class="text">
<input name="bar" type="text" />
</div>
</form>带有“ auto_fieldset”的2个元素。
---
auto_fieldset: 1
elements:
- type: Text
name: foo
- type: Text
name: bar
<form action="" method="post">
<fieldset>
<div class="text">
<input name="foo" type="text" />
</div>
<div class="text">
<input name="bar" type="text" />
</div>
</fieldset>
</form>第三元素在新的字段集中
---
auto_fieldset: { id: fs }
elements:
- type: Text
name: foo
- type: Text
name: bar
- type: Fieldset
- type: Text
name: baz
<form action="" method="post">
<fieldset id="fs">
<div class="text">
<input name="foo" type="text" />
</div>
<div class="text">
<input name="bar" type="text" />
</div>
</fieldset>
<fieldset>
<div class="text">
<input name="baz" type="text" />
</div>
</fieldset>
</form>由于这种行为,如果您想要嵌套字段,则必须将每个嵌套的字段集直接添加到其预期的父。
my $parent = $form->get_element({ type => 'Fieldset' });
$parent->element('fieldset'); 参数:$ string
通常,输入错误会导致与适当表单字段一起显示错误消息。如果您还希望将一般错误消息显示在表单的顶部,则可以使用“ Form_error_message”设置消息。
要为消息设置CSS类,请参见“ form_error_message_class”。
要更改用于显示消息的标记,请编辑form_error_message模板文件。请参阅“ Render_Method”。
是输出登录器。
如果为true,则迫使“ form_error_message”即使没有字段错误,也要显示。
参数:%默认值
设置默认值将添加到给定类型的每个元素,约束等中,然后将其添加到表单中。
例如,使每个Text元素自动具有10个大小,并使每个Strftime Feflator自动将其strftime设置为%d/%m/%Y :
default_args:
elements:
Text:
size: 10
deflators:
Strftime:
strftime: '%d/%m/%Y'使所有DateTime元素自动获得适当的Strftime Feeflator和DateTime Adverator的一个示例:
default_args:
elements:
DateTime:
deflators:
type: Strftime
strftime: '%d-%m-%Y'
inflators:
type: DateTime
parser:
strptime: '%d-%m-%Y'作为一种特殊情况,您还可以使用elements键Block , Field和Input来匹配从html :: formfu :: element :: block继承的任何元素, does html :: formfu :: formfu :: prole :: element :: element :: field或html :: field或html :: formfu :: formfu :: fore :: cole :: remo :: ream :: emelt :: emelt :: emelt :: emption :: input。
每个elements密钥any使用|分隔线:例如
# apply the given class to any Element of type Password or Button
default_args:
elements:
'Password|Button':
attrs:
class: novalidate每个elements键列表可以包含一个以+为 +的类型,仅与给定类型的祖先匹配元素:例如
# only apple the given class to an Input field within a Multi block
default_args:
elements:
'Input|+Multi':
attrs:
class: novalidate每个elements键列表可以包含一个类型开头的类型-仅匹配没有给定类型的祖先的元素:例如
# apply the given class only to Input fields that are not in a Multi block
default_args:
elements:
'Input|-Multi':
attrs:
class: validate这些参数以最少特定的顺序应用: Block , Field , Input , $type 。在其中的每一个中,都按最短到最长的时间顺序应用论点。
type键必须匹配按type返回的值,例如html :: formfu :: element中的“类型”。例如,如果您在HTML::FormFu::Element::* namespace之外有一个自定义元素,则通过$form->element({ type => '+My::Custom::Element' })加载它们,则给出的“ default_args”的键不应包括引导+ ,因为它删除了返回的type()值。例子:
# don't include the leading '+' here
default_args:
elements:
'My::Custom::Element':
attrs:
class: whatever
# do include the leading '+' here
elements:
- type: +My::Custom::Element“ default_args”生成一个单个哈希夫来传递给“填充”,依次合并每种类型的参数 - 含义为“填充”,仅调用一次 - 每种类型不是一次。因为不合并标量值 - 这意味着以后的值将覆盖较早的值:例如
# Normally, calling $field->add_attrs({ class => 'input' })
# then calling $field->add_attrs({ class => 'not-in-multi' })
# would result in both values being retained:
# class="input not-in-multi"
#
# However, default_args() creates a single data-structure to pass once
# to populate(), so any scalar values will overwrite earlier ones
# before they reach populate().
#
# The below example would result in the longest-matching key
# overwriting any others:
# class="not-in-multi"
#
default_args:
elements:
Input:
add_attrs:
class: input
'Input:-Multi':
add_attrs:
class: not-in-multi注意:与具有别名的适当方法不同,例如“元素”,这是“元素”的别名 - 给出default_args密钥必须为复数形式,例如:
default_args:
elements: {}
deflators: {}
filters: {}
constraints: {}
inflators: {}
validators: {}
transformers: {}
output_processors: {} 如果设置,则内容将在script标签中呈现,在表单的顶部内。
参数:$ url
参数: @urls
在任何“ JavaScript”部分之前,都会为每个URL添加一个script标签。
参数:[%private_stash]
返回值:%存放
提供了一个哈希-REF,您可以在其中存储任何可能要与表单关联的数据。
---
stash:
foo: value
bar: value 参数:$类型
参数:%选项
返回值:$元素
参数: @arrayref_of_types_or_options
返回值:@Elements
向表单添加一个新元素。请参阅HTML :: FormFu :: element中的“核心表单”字段和html :: formfu :: element中的“其他核心元素”,以获取核心元素列表。
如果您想从HTML::FormFu::Element:: ,可以从命名空间中加载元素,则可以通过将其在+前缀中使用完全合格的软件包名。
---
elements:
- type: +MyApp::CustomElement
name: foo如果%options中未提供type ,则将使用默认Text 。
“元素”是“元素”的别名。
参数:$类型
参数:%选项
返回值:$ DEFLATOR
参数: @arrayref_of_types_or_options
返回值:@deflators
缩放器可以与任何表单字段关联,并允许您提供$ field->默认值,其中可能是对象的值。
如果对象不将其串起到适当的显示值,则缩放器可以确保表单字段取代接收合适的字符串值。
请参阅HTML :: FormFu :: Deflator中的“核心放流器”,以获取核心缩小器列表。
如果未提供name属性,则为表单上的每个字段创建并添加了一个新的缩写器。
如果要在HTML::FormFu::Deflator:: ,可以将缩放器加载到命名空间中,则可以通过将其前缀 + +使用+来使用完全合格的软件包名称。
“ Deflator”是“缩进器”的别名。
参数:$ new_element,$ asuth_element
返回值:$ new_element
第一个参数必须是您要添加的元素,第二个参数必须是应该放置新元素的现有元素。
my $new = $form->element(%specs);
my $position = $form->get_element({ type => $type, name => $name });
$form->insert_before( $new, $position );在上面示例的第一行中,最初将$new元素添加到表单的末尾。但是, insert_before将换算$new元素,因此它将不再在表单的末尾。因此,如果您尝试将一个元素从一种形式复制到另一种形式,它将“窃取”该元素,而不是复制它。在这种情况下,您必须使用clone :
my $new = $form1->get_element({ type => $type1, name => $name1 })
->clone;
my $position = $form2->get_element({ type => $type2, name => $name2 });
$form2->insert_before( $new, $position ); 参数:$ new_element,$ asuth_element
返回值:$ new_element
第一个参数必须是您要添加的元素,第二个参数必须是应将新元素放置的现有元素。
my $new = $form->element(%specs);
my $position = $form->get_element({ type => $type, name => $name });
$form->insert_after( $new, $position );在上面示例的第一行中,最初将$new元素添加到表单的末尾。但是, insert_after方法尊重$new元素,因此它将不再在表单的末尾。因此,如果您尝试将一个元素从一种形式复制到另一种形式,它将“窃取”该元素,而不是复制它。在这种情况下,您必须使用clone :
my $new = $form1->get_element({ type => $type1, name => $name1 })
->clone;
my $position = $form2->get_element({ type => $type2, name => $name2 });
$form2->insert_after( $new, $position ); 参数:$元素
返回值:$元素
从表格或街区的儿童阵列中删除$element 。
$form->remove_element( $element );孤立的元素在用“ insert_before”或“ insert_after”重新连接到表单或块之前,无法将其用于任何物体。
HTML :: FormFu为传统上描述为验证提供了几个阶段。这些都是:
第一阶段,过滤器允许清理用户输入,例如编码或删除领先/尾随的空格,或从信用卡号码中删除非数字字符。
以下所有阶段都允许更复杂的处理,并且每个阶段都有一个机制可以允许异常的机制来表示输入错误。在每个阶段,所有表单字段都必须在下一阶段进行而不会出错。如果有任何错误,则应将表格重新启动给用户,以允许他们输入正确的值。
限制用于低级验证值的限制,例如“这是整数吗?”,“这个值在范围内吗?”或“这是有效的电子邮件地址吗?”。
充气器旨在允许一个值变成适当的对象。结果对象将传递给后续验证器和变形金刚,也将通过“ params”和“ param”返回。
验证者旨在用于高级验证,例如业务逻辑和数据库约束,例如“该用户名独特吗?”。仅当所有约束和充气机都没有错误运行时,验证器才能运行。预计大多数验证器将是特定于应用程序的,因此每个验证者都将作为由HTML :: FormFu用户编写的单独类实现。
参数:$类型
参数:%选项
返回值:$过滤器
参数: @arrayref_of_types_or_options
返回值:@filters
如果提供name或names值,则仅将过滤器添加到该命名字段中。如果您不提供name或names值,则将过滤器添加到已经连接到表单上的所有字段中。
有关核心过滤器列表,请参见HTML :: FormFu ::过滤器中的“核心过滤器”。
如果要在HTML::FormFu::Filter:: ,可以将过滤器加载到命名空间中,则可以通过在+之前使用完全合格的软件包名称。
“过滤器”是“过滤器”的别名。
参数:$类型
参数:%选项
返回值:$约束
参数: @arrayref_of_types_or_options
返回值:@constraints
请参阅html :: formfu ::约束核心约束列表中的“核心约束”。
如果没有提供name属性,则为表单上的每个字段创建新约束。
如果要在HTML::FormFu::Constraint:: ,可以将约束加载在命名空间中,则可以通过将其在 +之前使用+来使用完全限定的软件包名称。
“约束”是“约束”的别名。
参数:$类型
参数:%选项
返回值:$充气器
参数: @arrayref_of_types_or_options
返回值:@Inflators
请参阅HTML :: FormFu ::充气机中的“核心充气器”,以获取核心充气机的列表。
如果没有提供name属性,则为表单上的每个字段创建并添加了一个新的充气器。
如果您想将充气机加载到HTML::FormFu::Inflator:: ,可以通过将其 + +前缀使用+来使用完全合格的软件包名称。
“充气器”是“充气机”的别名。
参数:$类型
参数:%选项
返回值:$验证器
参数: @arrayref_of_types_or_options
返回值:@Validators
有关核心验证器列表,请参见HTML :: FormFu ::验证器中的“核心验证器”。
如果未提供name属性,则将为表单上的每个字段创建新的验证器。
如果要在HTML::FormFu::Validator:: ,可以将验证器加载到命名空间中,则可以通过将其在+前缀中使用完全限定的软件包名。
“验证器”是“验证者”的别名。
参数:$类型
参数:%选项
返回值:$变压器
参数: @arrayref_of_types_or_options
返回值: @transformers
有关核心变压器列表,请参见HTML :: Formfu :: Transformer中的“核心变压器”。
如果未提供name属性,则为表单上的每个字段创建并添加了一个新的变压器。
如果要在命名空间中加载变压器,除了HTML::FormFu::Transformer:: ,您可以通过将其前缀 + +使用+来使用完全限定的软件包名。
“变压器”是“变压器”的别名。
提交后重新显示表单时的默认行为是该字段包含原始的未更改的用户提取的值。
如果“ render_processed_value”为真,则在所有过滤器,充气器和变压器都运行后,字段值将是最终结果。缩水器也将以该值运行。
如果您将其设置在带有充气机的字段上,但是没有等效的缩写器,则应确保充气机将其串起为可用的值,以免混淆 /惹恼用户。
默认值:false
此方法是一种特殊的“继承访问者”,这意味着它可以在表单,块元素或单个元素上设置。当读取值时,如果未定义值,则它会自动穿越父母的元素层次结构,通过任何块元素并直至表单,从而搜索定义的值。
是继承的登录器。
无论用户输入如何,都会强制限制失败。
如果在运行时调用此表格,则在处理表单后,必须在将表单重新播放给用户之前再次称为html :: formfu中的“进程”。
默认值:false
此方法是一种特殊的“继承访问者”,这意味着可以将其设置在表单,块元素,元素或单个约束上。当读取值时,如果未定义值,则它会自动穿越父母的元素层次结构,通过任何块元素并直至表单,从而搜索定义的值。
是继承的登录器。
如果为true,则导致“ params”,“ param”和“有效”以忽略任何名称以下划线_开头的字段。
该字段仍是正常处理的,错误将导致“提交” _and_valid返回false。
默认值:false
所有属性都添加到渲染表格的开始标签中。
# Example
---
attributes:
id: form
class: fancy_form是属性登录器。
是属性的捷径。
默认值:“”
获取或设置与表格相关的操作。默认值不是动作,这会导致大多数浏览器提交到当前的URI。
是属性的捷径。
获取或设置表单的编码类型。有效值是application/x-www-form-urlencoded和multipart/form-data 。
如果表单包含文件元素,则将自动设置为multipart/form-data 。
是属性的捷径。
默认值:“ post”
获取或设置用于提交表格的方法。可以设置为“发布”或“获取”。
是属性的捷径。
获取或设置表单的标题属性。
是属性的捷径。
在表单顶部显示的错误消息的类属性。
请参阅“ form_error_message”
参数:[ @languages]
将传递给本地化对象的语言列表。
默认值:['en']
参数:[$ class_name]
className用于默认本地化对象。
默认值:'html :: formfu :: i18n'
参数:[$ key, @arguments]
与Locale :: makeText中的maketext方法兼容。
争论:$ locale
目前仅由HTML :: FormFu :: Deflator :: FormatNumber和HTML :: FormFu :: FormFu :: Felter :: FormatNumber使用。
此方法是一种特殊的“继承访问者”,这意味着它可以在表单,块元素或单个元素上设置。当读取值时,如果未定义值,则它会自动穿越父母的元素层次结构,通过任何块元素并直至表单,从而搜索定义的值。
是继承的登录器。
参数:[$ query_object]
参数:%参数
提供CGI兼容的查询对象或提交名称/值的哈希REF。或者,查询对象可以直接传递到“过程”对象。
参数:[$ query_type]
设置哪个模块用于提供“查询”。
Catalyst ::控制器:: HTML :: FormFu自动将其设置为Catalyst 。
有效的值是CGI , Catalyst和CGI::Simple 。
默认值:'CGI'
参数:[$ query_object]
参数:[%params]
处理提供的查询对象或输入值。在调用“提交表单值和错误”和“修改提交表单”下列出的任何方法之前,必须调用process 。
在打印表单或调用“渲染”或“ render_data”之前,还必须至少调用一个process 。
给Catalyst :: Controller :: HTML :: FormFu的用户注释:因为Catalyst Controller会自动为您拨打“过程”;如果您对操作方法中的表单进行任何修改,例如添加或更改元素,添加约束等;您必须在使用“提交的_and_valid”之前再次调用“进程”,即“提交表单值和错误”或“修改提交表单”或呈现表单下列出的任何方法。
如果已提交表格,则返回为true。有关如何计算的详细信息,请参见“指标”。
$form->submitted && !$form->has_errors
返回值:%参数
返回所有有效输入的哈希 - ref,没有错误。
参数:$ field_name
更可靠的,推荐的版本的“ param”。保证始终返回单个值,无论是否在列表上下文中调用。如果提交了多个值,则仅返回第一个值。如果该值无效或未提交表格,则返回undef 。这使其适合在需要单个值的列表上下文中使用。
$db->update({
name => $form->param_value('name'),
address => $form->param_value('address),
}); 参数:$ field_name
无论上下文如何,都可以保证始终返回一个值的数组ref,而不管是否提交多个值。如果该值无效或未提交表单,则将返回一个空数组ref。
参数:$ field_name
无论上下文如何,保证始终返回值列表。如果该值无效或未提交表单,则将返回一个空列表。
参数:[$ field_name]
返回值:$ input_value
返回值:@valid_names
不再建议使用,因为它的行为很难预测。使用“ param_value”,“ param_array”或“ param_list”。
一种类似于CGI的方法。
如果给出了字段名称,则在List-context返回该字段提交的任何有效值,而在标量字段中仅返回仅提交该字段的任何有效值中的第一个。
如果没有给出参数,请返回所有有效输入字段名称的列表,没有错误。
通过超过1个参数是致命的错误。
参数:[$ field_name]
返回值:@valid_names
返回值:$ bool
如果字段名称如果给出,则如果该字段没有错误,则返回true ,如果有错误,则返回false 。
如果没有给出参数,请返回所有有效输入字段名称的列表,没有错误。
参数:[$ field_name]
返回值:@names
返回值:$ bool
如果给出字段名称,如果该字段有错误,如果没有false ,则返回true 。
如果没有给出参数,请返回带有错误的所有输入字段名称的列表。
参数:[%选项]
参数:[%选项]
返回值:@errors
从表格中所有字段中返回一个异常对象的数组ref。
接受name , type和stage参数以缩小返回的结果。
$form->get_errors({
name => 'foo',
type => 'Regex',
stage => 'constraint'
}); 参数:[%选项]
参数:[%选项]
返回值:$错误
接受与“ get_errors”相同的参数,但仅返回第一个错误。
有关更多详细信息和可用模型,请参见HTML :: FormFu ::模型。
参数:$ model_name
默认值:'dbic'
参数:[$ model_name]
返回值:$模型
参数:%config
参数:$ name,$ value
返回值:$值
提供的值替换了指定字段的任何当前值。此值将在随后的“ params”和“ param”的呼叫中返回,命名字段将包含在“有效”的计算中。
从提交的表格中删除所有错误。
返回值:$ string
构建表格后,在调用“渲染”之前,必须调用一次“过程”。
返回值:$ string
返回表单启动标签,以及“ form_error_message”和“ javaScript”的任何输出。
返回值:$ string
返回表单结束标签。
返回值:$ string
返回所有隐藏的表单字段。
HTML::FormFu提供了一个插件系统,该系统允许插件轻松添加到表单或元素中,以更改默认的行为或输出。
有关详细信息,请参见HTML :: FormFu ::插件。
默认情况下,FormFu呈现“ XHTML 1.0严格”兼容标记,并尽可能少。提供了许多钩子来添加编程生成的CSS类名称,以允许仅通过更改CSS生成大量的输出样式。
通过布局和Multi_layout方法,可以对标记的基本自定义化。这使您可以重新排序每个字段的各个部分的位置(例如标签,注释,错误消息和输入标签),并插入您可能希望的任何其他任意标签。
如果这还不够,您可以通过告诉HTML :: FormFu使用外部渲染引擎(例如Template Toolplate或Template :: Alloy)来完全个性化标记。有关详细信息,请参见“ Render_Method”和“ TT_Module”。
即使您将HTML :: FormFu设置为使用模板::工具包进行渲染,也可以将html :: formfu与您更喜欢用于自己的页面布局使用的任何其他模板系统一起使用,无论是html :: html :: template:template :: template: <TMPL_VAR form> plate <form tal:replace="form"></form> :: <!-- {form} --> 。
从HTML::FormFu v1.00开始,不再列出TT列出所需的先决条件 - 因此,如果您想使用模板文件,则需要手动安装它。
默认值: string
可以将其设置为tt以使用外部模板文件生成表单。
要自定义标记,您需要您应用程序本地的模板文件的副本。有关更多详细信息,请参见HTML :: Formfu :: Manual :: Manual ::食谱中的“安装TT模板”。
您可以通过将该元素的“ Render_Method”设置为tt来自定义单个元素的标记,而其余表单则使用默认string Render-Method。但是请注意,如果您尝试将表单或块的“ render_method”设置为tt ,然后将子元素的“ render_method”设置为string ,则将忽略该设置,并且子元素仍将使用tt Render-method。
---
elements:
- name: foo
render_method: tt
filename: custom_field
- name: bar
# in this example, 'foo' will use a custom template,
# while bar will use the default 'string' rendering method此方法是一种特殊的“继承访问者”,这意味着它可以在表单,块元素或单个元素上设置。当读取值时,如果未定义值,则它会自动穿越父母的元素层次结构,通过任何块元素并直至表单,从而搜索定义的值。
是继承的登录器。
更改用于表单的模板文件名。
默认值:“形式”
参数:[%structor_arguments]
接受传递给“ render_method”的论点的哈希 - 列表,该论点在内部以“ render”称为。
在tt_args中,钥匙RELATIVE和RECURSION被覆盖为始终为真,因为这些是模板引擎的基本要求。
包含HTML :: FormFu的模板文件的系统目录始终添加到INCLUDE_PATH的末尾,以便找到核心模板文件。只有出于自定义目的,您只需要自己设置此模板文件的副本即可。
此方法是一种特殊的“继承访问者”,这意味着它可以在表单,块元素或单个元素上设置。当读取值时,如果未定义值,则它会自动穿越父母的元素层次结构,通过任何块元素并直至表单,从而搜索定义的值。
参数:[%structor_arguments]
确保将HASH-REF参数与任何“ tt_args”的现有hash-ref值合并。
默认值:模板
将“ Render_Method”设置为tt时使用的模块。应提供与模板兼容的接口。
此方法是一种特殊的“继承访问者”,这意味着它可以在表单,块元素或单个元素上设置。当读取值时,如果未定义值,则它会自动穿越父母的元素层次结构,通过任何块元素并直至表单,从而搜索定义的值。
通常由“渲染”隐式称为。返回通常将通常传递到string或tt渲染方法的数据结构。
与“渲染”一样,您必须在构建表单后和调用“ render_data”之前一次调用“进程”。
例如“ render_data”,但不包括任何子元素的数据。
参数:[%选项]
参数:[%选项]
返回值: @elements
返回表单中的所有字段(具体来说,在html :: formfu :: element value中具有真实“ is_field”的所有元素)。
接受name和type参数以缩小返回的结果。
$form->get_fields({
name => 'foo',
type => 'Radio',
});还接受纠正措施以搜索结果。
$form->get_elements({
name => qr/oo/,
}); 参数:[%选项]
参数:[%选项]
返回值:$元素
接受与“ get_fields”相同的参数,但仅返回找到的第一个字段。
参数:[%选项]
参数:[%选项]
返回值: @elements
返回形式中的所有顶级元素(不是递归)。有关递归版本,请参见“ get_all_elements”。
接受name和type参数以缩小返回的结果。
$form->get_elements({
name => 'foo',
type => 'Radio',
});还接受纠正措施以搜索结果。
$form->get_elements({
name => qr/oo/,
}); 参数:[%选项]
参数:[%选项]
返回值:$元素
接受与“ get_lements”相同的参数,但仅返回第一个元素。
有关递归版本,请参见“ get_all_element”。
参数:[%选项]
参数:[%选项]
返回值: @elements
递归返回形式的所有元素。
可选地接受name和type参数以缩小返回的结果。
# return all Text elements
$form->get_all_elements({
type => 'Text',
});还接受纠正措施以搜索结果。
$form->get_elements({
name => qr/oo/,
});有关非恢复版本,请参见“ get_lements”。
参数:[%选项]
参数:[%选项]
返回值:$元素
接受与“ get_all_elements”相同的参数,但仅返回第一个元素。
# return the first Text field found, regardless of whether it's
# within a fieldset or not
$form->get_all_element({
type => 'Text',
});还接受纠正措施以搜索结果。
$form->get_elements({
name => qr/oo/,
});有关非恢复版本,请参见“ get_all_elements”。
参数:[%选项]
参数:[%选项]
返回值: @deflators
从所有字段返回所有顶级缩进器。
接受name和type参数以缩小返回的结果。
$form->get_deflators({
name => 'foo',
type => 'Strftime',
}); 参数:[%选项]
参数:[%选项]
返回值:$元素
接受与“ get_deflators”相同的参数,但仅返回第一个缩写器。
参数:[%选项]
参数:[%选项]
返回值: @filters
从所有字段返回所有顶级过滤器。
Accepts both name and type arguments to narrow the returned results.
$form->get_filters({
name => 'foo',
type => 'LowerCase',
}); Arguments: [%options]
Arguments: [%options]
Return Value: $filter
Accepts the same arguments as "get_filters", but only returns the first filter found.
Arguments: [%options]
Arguments: [%options]
Return Value: @constraints
Returns all constraints from all fields.
Accepts both name and type arguments to narrow the returned results.
$form->get_constraints({
name => 'foo',
type => 'Equal',
}); Arguments: [%options]
Arguments: [%options]
Return Value: $constraint
Accepts the same arguments as "get_constraints", but only returns the first constraint found.
Arguments: [%options]
Arguments: [%options]
Return Value: @inflators
Returns all inflators from all fields.
Accepts both name and type arguments to narrow the returned results.
$form->get_inflators({
name => 'foo',
type => 'DateTime',
}); Arguments: [%options]
Arguments: [%options]
Return Value: $inflator
Accepts the same arguments as "get_inflators", but only returns the first inflator found.
Arguments: [%options]
Arguments: [%options]
Return Value: @validators
Returns all validators from all fields.
Accepts both name and type arguments to narrow the returned results.
$form->get_validators({
name => 'foo',
type => 'Callback',
}); Arguments: [%options]
Arguments: [%options]
Return Value: $validator
Accepts the same arguments as "get_validators", but only returns the first validator found.
Arguments: [%options]
Arguments: [%options]
Return Value: @transformers
Returns all transformers from all fields.
Accepts both name and type arguments to narrow the returned results.
$form->get_transformers({
name => 'foo',
type => 'Callback',
}); Arguments: [%options]
Arguments: [%options]
Return Value: $transformer
Accepts the same arguments as "get_transformers", but only returns the first transformer found.
Returns a deep clone of the $form object.
Because of scoping issues, code references (such as in Callback constraints) are copied instead of cloned.
For the basic method, eg /attributes :
Arguments: [%attributes]
Arguments: [%attributes]
Return Value: $form
As a special case, if no arguments are passed, the attributes hash-ref is returned. This allows the following idioms.
# set a value
$form->attributes->{id} = 'form';
# delete all attributes
%{ $form->attributes } = ();All methods documented as 'attribute accessors' also have the following variants generated:
*_xml can be used as a setter, and ensures that its argument is not XML-escaped in the rendered form.
*_loc can he used as a setter, and passes the arguments through "localize".
add_* can be used to append a word to an attribute without overwriting any already-existing value.
# Example
$form->attributes({ class => 'fancy' });
$form->add_attributes({ class => 'pants' });
# class="fancy pants" add_*_xml , like add_* , but ensures it doesn't get XML-escaped.
add_*_loc , like add_* , but passing the arguments through "localize".
del_* can be used to remove a word from an attribute value.
# Example
$form->attributes({ class => 'fancy pants' });
$form->del_attributes({ class => 'pants' });
# class="fancy" del_*_xml , like del_* , but ensures it doesn't get XML-escaped.
del_*_loc , like del_* , but passing the arguments through "localize".
Also, any attribute method-name which contains the word attributes also has aliases created for all these variants, with the word attributes replaced by attrs .
# For example, the attributes() method would have all these variant
# methods available
$form->attributes({ class => 'fancy' });
$form->attributes_xml({ title => '<b>fancy</b>' });
$form->attributes_loc({ title => 'fancy' });
$form->add_attributes({ class => 'fancy' });
$form->add_attributes_xml({ title => '<b>fancy</b>' });
$form->add_attributes_loc({ title => 'fancy' });
$form->del_attributes({ class => 'fancy' });
$form->del_attributes_xml({ title => '<b>fancy</b>' });
$form->del_attributes_loc({ title => 'fancy' });
# Because the method contains the word 'attributes', it also gets the
# following short-forms
$form->attrs({ class => 'fancy' });
$form->attrs_xml({ title => '<b>fancy</b>' });
$form->attrs_loc({ title => 'fancy' });
$form->add_attrs({ class => 'fancy' });
$form->add_attrs_xml({ title => '<b>fancy</b>' });
$form->add_attrs_loc({ title => 'fancy' });
$form->del_attrs({ class => 'fancy' });
$form->del_attrs_xml({ title => '<b>fancy</b>' });
$form->del_attrs_loc({ title => 'fancy' });All methods documented as 'attribute short-cuts' are short-cuts to directly access individual attribute key/values.
# Example
$form->id( 'login' );
$id = $form->id;
# is equivalent to:
$form->attributes({ id => 'login' });
$id = $form->attributes->{id}; All attribute short-cuts also have a *_xml variant.
# Example
$form->id_xml( $xml );
# is equivalent to:
$form->attributes_xml({ id => $xml }); All attribute short-cuts also have a *_loc variant.
# Example
$form->title_loc( $key );
# is equivalent to:
$form->attributes_loc({ title => $key });All methods documented as 'inheriting accessors' can be set on the form, a block element or a single field element. When the value is read, if no value is defined it automatically traverses the element's hierarchy of parents, searching for a defined value.
All inherited accessors also have a *_no_inherit variant, which can be used as a getter to fetch any defined value, without traversing the hierarchy of parents. This variant cannot be used as a setter.
Eg, the "auto_id" has a variant named auto_id_no_inherit .
All methods documented as 'output accessors' also have *_xml and *_loc variants.
The *_xml variant can be used as a setter, and ensures that its argument is not XML-escaped in the rendered form.
The *_loc variant can be used as a setter, and passes the arguments through "localize".
Eg, the label method has variants named label_xml and label_loc .
To support boolean attributes, whose value should either be equal to the attribute name, or empty. Any true value will switch the attribute 'on', any false value will remove the attribute.
# Example
$field->autofocus(1);
# equivalent to:
$field->attributes({ autofocus => 'autofocus' });
$field->autofocus(0);;
# equivalent to:
delete $field->attributes->{autofocus};Some attributes support character substitutions: the following substitutions are possible:
%f # $form->id
%n # $field->name
%t # lc( $field->type )
%r # $block->repeatable_count
%s # $error->stageThese allow each field to have consistent attributes, while remaining unique.
We try our best to not make incompatible changes, but if they're required we'll make every effort possible to provide backwards compatibility for several release-cycles, issuing a warnings about the changes, before removing the legacy features.
v1.00 dropped most of the default HTML class-names, with the intention that each application should define just what it needs, without needing to reset unwanted options first. We also gain the benefit of less markup being generated, speeding up both render and HTTP transfers.
To restore the previous behaviour, set the following options.
If you're using best practices, you'll only need to set these once per-application in your app-wide config file.
---
auto_container_class: '%t'
auto_container_label_class: 'label'
auto_container_comment_class: 'comment'
auto_comment_class: 'comment'
auto_container_error_class: 'error'
auto_container_per_error_class: 'error_%s_%t'
auto_error_class: 'error_message error_%s_%t'See "DEPRECATED METHODS" in HTML::FormFu::Role::Element::Field.
See also "REMOVED METHODS" in HTML::FormFu::Element.
Has been removed; see "default_args" instead.
Has been removed; use "default_model" instead.
Has been removed; use "default_values" in HTML::FormFu::Model instead.
Has been removed; use "update" in HTML::FormFu::Model instead.
It is advisable to keep application-wide (or global) settings in a single config file, which should be loaded by each form.
See "load_config_file".
HTML::FormFu::Manual::Cookbook
HTML::FormFu::Manual::Unicode
The distribution directory examples/vertically-aligned contains a form with example CSS for a "vertically aligned" theme.
This can be viewed by opening the file vertically-aligned.html in a web-browser.
If you wish to experiment with making changes, the form is defined in file vertically-aligned.yml , and the HTML file can be updated with any changes by running the following command (while in the distribution root directory).
perl examples/vertically-aligned/vertically-aligned.pl This uses the Template Toolkit file vertically-aligned.tt , and the CSS is defined in files vertically-aligned.css and vertically-aligned-ie.css .
HTML::FormFu::Imager
Catalyst::Controller::HTML::FormFu
HTML::FormFu::Model::DBIC
Brian Cassidy
Ozum Eldogan
Ruben Fonseca
Ronald Kimball
Daisuke Maki
Andreas Marienborg
Mario Minati
Steve Nolte
Moritz Onken
Doug Orleans
Matthias Dietrich
Dean Hamstead
Karen Etheridge
Nigel Metheringham
Based on the original source code of HTML::Widget, by Sebastian Riedel, [email protected] .
Carl Franks <[email protected]>
This software is copyright (c) 2018 by Carl Franks.
这是免费软件; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.