Skip to content

Commit

Permalink
added a route to refresh captcha gd
Browse files Browse the repository at this point in the history
  • Loading branch information
bamarni committed Sep 5, 2012
1 parent f38b1c4 commit 4c38534
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Controller/Base64Controller.php
Expand Up @@ -21,6 +21,15 @@
*/
class Base64Controller extends ContainerAware
{
public function refreshCaptchaAction()
{
$captcha = $this->container->get('genemu.gd.captcha');
$captcha->setOptions(array());
$datas = preg_split('([;,]{1})', substr($captcha->getBase64(), 5));

return new Response(base64_decode($datas[2]), 200, array('Content-Type' => $datas[0]));
}

public function base64Action()
{
$query = $this->container->get('request')->server->get('QUERY_STRING');
Expand Down
4 changes: 4 additions & 0 deletions Resources/config/routing/base64.xml
Expand Up @@ -4,6 +4,10 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="genemu_captcha_refresh" pattern="/genemu-captcha-refresh">
<default key="_controller">GenemuFormBundle:Base64:refreshCaptcha</default>
</route>

<route id="genemu_base64" pattern="/genemu_base64">
<default key="_controller">GenemuFormBundle:Base64:base64</default>
</route>
Expand Down
42 changes: 41 additions & 1 deletion Resources/doc/captcha_gd/index.md
Expand Up @@ -10,6 +10,46 @@ genemu_form:
captcha: ~
```

## Allowing users to refresh the captcha

In order to allow the captcha refresh, there is a route to call, but you have to use custom form templates, here is a ready-to-use example :

1) Add the route :
``` yml
# app/config/routing.yml
genemu_base64:
resource: "@GenemuFormBundle/Resources/config/routing/base64.xml"
```

2) Customize the templates

``` jinja
{# ... #}
{% form_theme form _self %}
{% block genemu_captcha_widget %}
<img id="{{ id }}_image" src="{{ src }}" width="{{ width }}" height="{{ height }}" title="{{ name|trans }}" />
{# We're putting a link there #}
<a id="{{ id }}_refresh">Refresh</a>
{{ block("field_widget") }}
{% endblock %}
{% block genemu_captcha_javascript %}
<script type="text/javascript">
$(function () {
{# Image will be refreshed when the link is clicked #}
$('#{{ id }}_refresh').click(function() {
$('#{{ id }}_image').attr('src', '{{ path('genemu_captcha_refresh') }}?' + Math.random());
});
});
</script>
{{ parent() }}
{% endblock %}
```

## Fix Bug to IE6 and IE7

* add in your routing.yml
Expand All @@ -33,7 +73,7 @@ public function buildForm(FormBuilder $builder, array $options)
$builder
// ...
->add('captcha', 'genemu_captcha');

// If you are using form for adding/editing entity (for example with FOSUserBundle user registration form)
// you may need to mark field as "not a property" by using code

Expand Down

0 comments on commit 4c38534

Please sign in to comment.