Giter Site home page Giter Site logo

recaptchavalidator's Introduction

Simple Google reCAPTCHA FormType and Validator Component for Symfony2 applications

SensioLabsInsight Latest Stable Version Build Status Scrutinizer Code Quality Coverage Status License

Really light and simple reCAPTCHA component for Symfony Frameworks, it's not a Bundle, you can reconfigure all components whatever you like.

You can find full documentation about Google reCAPTCHA API v2 here.

Installation

You can install this package with Composer. Add next lines to your composer.json file:

{
    "require": {
        "dario_swain/re-captcha-validator": "dev-master"
    },
    "scripts": {
        "post-install-cmd": [
            "DS\\Component\\ReCaptchaValidator\\Composer\\ScriptHandler::replaceViews"
        ],
        "post-update-cmd": [
            "DS\\Component\\ReCaptchaValidator\\Composer\\ScriptHandler::replaceViews"
        ]
    },
}

To use this package with php version ~ 5.3 use version ~ 1.0

Usage Example

Add public and private keys, and configure reCAPTCHA Form Type like a service, provide next configuration to application bundle service configuration (service.yml):

parameters:
    ds_recaptcha_public_key:  #YOUR_PUBLIC_KEY#
    ds_recaptcha_private_key: #YOUR_PRIVATE_KEY#

services:
    ds.form.type.recaptcha:
            class: DS\Component\ReCaptchaValidator\Form\ReCaptchaType
            arguments: ['@request_stack', %ds_recaptcha_public_key%, %ds_recaptcha_private_key%, %locale%]
            tags:
                - { name: form.type, alias: ds_recaptcha }

After this you can add reCAPTCHA type to your custom form:

<?php

namespace AcmeBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('message', 'textarea')
            /** In type add your form alias **/
			->add('captcha', 'ds_recaptcha', array('mapped' => false))
			->add('send', 'submit');
    }

	public function setDefaultOptions(OptionsResolverInterface $resolver)
	{
		$resolver->setDefaults(array(
		    /** This option is require, because reCaptcha api.js add extra field "g-recaptcha-response" to form **/
			'allow_extra_fields' => true,
		));
	}
}

Next step, you need to add form_theme to your form view, it seems like that:

{% extends 'AcmeBundle::layout.html.twig' %}
{% form_theme form 'DS/ReCaptcha/views/form_div_layout.html.twig' %}
{% block content %}
    {{ form_start(form) }}
    {{ form_widget(form) }}
    {{ form_end(form) }}
{% endblock %}

If you need to customize from widget, feel free to change template in %kernel_root%/Resources/DS/ReCaptcha/views/form_div_layout.twig

Copyright

Copyright (c) 2015 Ilya Pokamestov [email protected].

recaptchavalidator's People

Contributors

ilyapokamestov avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.