定制 simplethings/form-extra-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

simplethings/form-extra-bundle

Composer 安装命令:

composer require simplethings/form-extra-bundle

包简介

This bundles provides extra FormType's for Symfony2

README 文档

README

Build Status

This bundle contains additional functionality for Symfony2's Form Component.

Currently this Bundle provides the following:

  • RecaptchaFieldType for using Google's reCAPTCHA service.
  • ImageType for showing the previously uploaded image
  • FileSet for showing a list of previously uploaded files
  • FieldTypeExtension for allowing setting attr when building a Form.

Current this Bundle does not provide the following:

  • In depth documentation.
  • Unicorns or other fairytale creatures.

Contributors

Thanks to all who have helped make this bundle awesome. For a list of people who have helped you should visit this page: https://github.com/SimpleThings/SimpleThingsFormExtraBundle/contributors.

Contributing

If you want to help create a fork of this repository do some coding and send a Pull Request.

Installing

Fire up a terminal and either clone this repository or add it as a submodule both are shown here by example.

$ 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

for symfony 2.0 use the deps file by adding this in it and running php bin/vendors install

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

or for symfony 2.1 add this to your composer.json and run composer install

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

the enable to the bundle inside your kernel class normally called AppKernel.php

<?php

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

Usage

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"

When doing functional testing it is not possible to use the real Google Recaptcha API therefor there is a SimpleThings\FormExtraBundle\FunctionalTest\Recaptcha which always returns true.

It can be used by overwriting the DependencyInjection parameter in app/config/config_test.yml

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

The formextra_recaptcha form type takes a widget_options setting which is encoded as json and set to the configuration javascript variable Recaptcha needs. This allows you to change the theme for the widget or roll your own. For more information about what is possible check here 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

Sometimes it is needed to show the value of a field without having it be an input box. This is where PlainType comes in handy. It will render a simple p tags with the fields value and also prevent that field from being bound if the form is tampered with.

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

FileSetType

FileSetType allows you to incrementally add more files to a collection of files by extending the FileType. It renders an unordered list of all the previously uploaded base filenames.

Instead of returning the previously added file you return an array of all file names from the fields getter method and in the setter method you append the newly uploaded file to the collection:

<?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.
    }
}

Using the builder to create a field for this type would then look like:

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

There are optional parameters 'delete_route' and 'delete_id' which are then used with twigs path method to generate a route with parameters "id" and "file", to delete the listed file. If the information passed is not enough you should overwrite the twig template with your own logic to implement deleting.

TranslationDomainExtension

This field extension provides the forward compatibility for the support of translation domains added in Symfony 2.1. So activating it is useful only when using Symfony 2.0 (activating it in Symfony 2.1 will add useless overhead)

To use it, you need to activate it and to register the form theme using the translation domain.

# 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

You can now provide the translation domain used for labels and choice options when building the form:

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

HelpExtension

This field extension provides help message option.

To use it, you need to activate it and to register the form theme using the translation domain.

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

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

You can also load the help extension form theme into your own form theme.

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

You can now provide the help message for field when building the form:

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

HtmlEntitiesTransformer

Converts html code into entites. Also extends htmlentities function to auto guess the used charset if mbstring extension is availible.

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

StripTagsTransformer

Provides easy tag stripping capabilities for your forms to reduce xss attacks. You note it is not the best solution.

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

simplethings/form-extra-bundle 适用场景与选型建议

simplethings/form-extra-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 84.13k 次下载、GitHub Stars 达 99, 最近一次更新时间为 2012 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「Symfony2」 「form」 「recaptcha」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 simplethings/form-extra-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 simplethings/form-extra-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 simplethings/form-extra-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 84.13k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 101
  • 点击次数: 17
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 99
  • Watchers: 6
  • Forks: 26
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2012-01-03