Permalink
Browse files

added a route to refresh captcha gd

  • Loading branch information...
bamarni committed Sep 5, 2012
1 parent f38b1c4 commit 4c385342507fbb36b8455f97d6a958ac6e516b96
Showing with 54 additions and 1 deletion.
  1. +9 −0 Controller/Base64Controller.php
  2. +4 −0 Resources/config/routing/base64.xml
  3. +41 −1 Resources/doc/captcha_gd/index.md
@@ -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');
@@ -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>
@@ -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
@@ -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

0 comments on commit 4c38534

Please sign in to comment.