unitkit/uk-hash-bundle

library for form antispam protection

v0.0.3 2017-09-12 12:10 UTC

This package is auto-updated.

Last update: 2020-07-19 11:53:25 UTC


README

Bundle for form antispam protection.

Requires

  • php: >=5.3.9
  • symfony/symfony: 2.8.*

Install

Composer

#!console

> composer require unitkit/uk-hash-bundle

Setup

app/config.yml

#!yaml

# Twig Configuration
twig:
    # ...
    paths:
        '%kernel.root_dir%/../vendor/unitkit/uk-hash-bundle/src/UnitKit/Hash/Resources/views/form': uk_hash

app/routing.yml

#!yaml

# Routing Resource
uk_hash:
    resource: "@UkHashBundle/Controller"
    type:     annotation

app/AppKernel.php

#!php
<?php 

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
   public function registerBundles()
   {
       $bundles = array(           
           new UnitKit\Hash\UkHashBundle()
       );
       return $bundles;
   }
}

?>

Usage

FormType

#!php
<?php 

class CustomFormType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {     
        /* .... build form .... */

        $builder->add('hash', $options['hash_type'], array('label' => false));        
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'hash_type' => 'UnitKit\Form\Type\UkHashType'
        ));
    }

?>

FormTemplate

#!twig

{% form_theme form '@uk_hash/hash_fields.html.twig' %} 
{# Можно также создать свою тему оформления форм и унаследовать ее от указанной выше #}

{{form_errors(form)}}
{{form_start(form)}}
{{form_end(form)}}

Controller

#!php
<?php

    /**
     * @Route("/form/")
     */    
    public function getForm(Request $request)
    {
        $form = $this->createForm(new CustomFormType(), null, array('hash_type' => $this->get('uk.hash_type')));
        return $this->render('AppBundle:Default:index.html.twig', array('form' => $form->createView()));
    }

?>

Note

Do not use include for rendering form

#!php
<?php

{% include 'AppBundle:Form:getForm.html.twig' %}

?>

Use render controller instead

#!php
<?php

{{ render(controller('AppBundle:Form:getForm')) }}

?>