simplethings/form-extra-bundle

此扩展包为 Symfony2 提供额外的 FormType

v1.1.0 2014-04-17 08:30 UTC

This package is auto-updated.

Last update: 2024-09-11 01:51:50 UTC


README

Build Status

此扩展包包含为 Symfony2 的表单组件提供额外功能。

目前此扩展包提供以下功能

  • RecaptchaFieldType 用于使用 Google 的 reCAPTCHA 服务。
  • ImageType 用于显示之前上传的图片
  • FileSet 用于显示之前上传的文件列表
  • FieldTypeExtension 允许在构建表单时设置 attr

当前此扩展包 提供以下功能

  • 深入文档。
  • 独角兽或其他童话生物。

贡献者

感谢所有帮助使此扩展包变得出色的朋友们。有关帮助过你的人的列表,请访问此页面:https://github.com/SimpleThings/SimpleThingsFormExtraBundle/contributors

贡献

如果您 想要 帮助创建此存储库的分支,进行一些编码并发送拉取请求。

安装

打开终端并克隆此存储库或将其添加为子模块,这里通过示例演示这两种方法。

$ git clone git://github.com/simplethings/SimpleThingsFormExtraBundle.git vendor/bundles/SimpleThings/FormExtraBundle
$ git submodule add git://github.com/simplethings/SimpleThingsFormExtraBundle.git vendor/bundles/SimpleThings/FormExtraBundle

对于 symfony 2.0,使用 deps 文件,在其中添加以下内容并运行 php bin/vendors install

[FormExtraBundle]
    git=https://github.com/simplethings/SimpleThingsFormExtraBundle.git
    target=bundles/SimpleThings/FormExtraBundle
    version=v0.1

或者对于 symfony 2.1,将其添加到您的 composer.json 中并运行 composer install

{
    "require": {
        "simplethings/form-extra-bundle": "1.0.*"
    }
}

在您的内核类中启用此扩展包,通常称为 AppKernel.php

<?php

public function registerBundles()
{
    // ...
    new SimpleThings\FormExtraBundle\SimpleThingsFormExtraBundle(),
    // ...
}

用法

RecaptchaFieldType

<?php
// ...
$builder->add('recaptcha', 'formextra_recaptcha');
// ...
# app/config/config.yml
simple_things_form_extra:
    recaptcha:
        private_key: "your-private-key"
        public_key:  "your-public-key"

在进行功能测试时,无法使用真实的 Google Recaptcha API,因此存在一个 SimpleThings\FormExtraBundle\FunctionalTest\Recaptcha,它始终返回 true。

可以通过覆盖 app/config/config_test.yml 中的依赖注入参数来使用它。

parameters:
    simple_things_form_extra.service.recaptcha.class: SimpleThings\FormExtraBundle\FunctionalTest\Recaptcha

formextra_recaptcha 表单类型接受一个 widget_options 设置,该设置被编码为 json,并设置给 Recaptcha 需要的配置 JavaScript 变量。这允许您更改小部件的主题或自行实现。有关更多信息,请参阅http://code.google.com/apis/recaptcha/docs/customization.html

<?php
// ...
$builder->add('recaptcha', 'formextra_recaptcha', array(
    'widget_options' => array(
        'theme' => 'white', // blackglass, clean, red is the predefined themes.
    ),
));
// ...

PlainType

有时需要显示字段的值,而不用将其作为输入框。这就是 PlainType 发挥作用的地方。它将渲染一个简单的 p 标签,包含字段的值,并防止在表单被篡改时绑定该字段。

<?php
// ...
$builder->add('username', 'formextra_plain');
// ...

FileSetType

FileSetType 允许您通过扩展 FileType 来增量地向文件集合中添加更多文件。它渲染一个无序列表,显示所有之前上传的基本文件名。

您可以通过返回字段 getter 方法中的所有文件名来替代之前添加的文件,并在 setter 方法中将新上传的文件追加到集合中。

<?php
class Document
{
    // temporary field, used in the form, to move new attachments to persistence
    private $newAttachment;
    // persistent array with all attachments.
    private $attachments;

    public function getNewAttachment()
    {
        $files = array();
        foreach ($this->attachments AS $attachment) {
            $files[] = $attachment->getFilename();
        }
        return $files;
    }

    public function setNewAttachment(File $newAttachment = null)
    {
        $this->newAttachment = $newAttachment;
    }

    public function moveNewAttachment()
    {
        // code to move file and include in the attachments field.
    }
}

使用构建器创建此类型的字段将类似于

<?php
$builder->add('newAttachment', 'formextra_fileset', array(
    'type' => 'file',
));

存在可选参数 'delete_route' 和 'delete_id',它们随后将与 twigs path 方法一起使用来生成带有参数 "id" 和 "file" 的路由,以删除列出的文件。如果提供的信息不足,您应该覆盖 twig 模板以实现自己的逻辑来删除。

TranslationDomainExtension

此字段扩展提供了对在 Symfony 2.1 中添加的翻译域支持的向前兼容性。因此,仅在使用 Symfony 2.0 时激活它是有用的(在 Symfony 2.1 中激活它将添加无用的开销)。

要使用它,您需要激活它,并使用翻译域注册表单主题。

# app/config/config.yml
simple_things_form_extra:
    translation_domain_forward_compat: true

twig:
    form:
        resources:
            - SimpleThingsFormExtraBundle:Form:translation_domain.html.twig
            # eventually other themes here

现在,当构建表单时,您可以提供用于标签和选项的翻译域。

<?php
// ...
$builder->add('body', 'textarea', array(
    'label' => 'some message',
    'translation_domain' => 'form_extra',
));
// ...

帮助扩展

该字段扩展提供了帮助信息选项。

要使用它,您需要激活它,并使用翻译域注册表单主题。

# app/config/config.yml
simple_things_form_extra:
    help_extension: true

twig:
    form:
        resources:
            - SimpleThingsFormExtraBundle:Form:field_type_help.html.twig

您还可以将帮助扩展表单主题加载到自己的表单主题中。

{% use 'SimpleThingsFormExtraBundle:Form:field_type_help.html.twig' %}

现在,当构建表单时,您可以提供字段的帮助信息。

<?php
// ...
$builder->add('body', 'textarea', array(
    'label' => 'some message',
    'help'  => 'some usefull help message'
));
// ...

HtmlEntitiesTransformer

将HTML代码转换为实体。如果启用mbstring扩展,还扩展了htmlentities函数来自动猜测使用的字符集。

<?php
// ...
$builder->get('body')->prependNormTransformer(new HtmlEntitiesTransformer(ENT_COMPAT, true));
// ...

StripTagsTransformer

为您的表单提供简单的标签剥离功能,以减少XSS攻击。请注意,这不是最佳解决方案。

<?php
// ...
// This will allow <p> tags.
$builder->get('body')->prependNormTransformer(new StripTagsTransformer('<p>'));
// ...