Skip to content

Commit

Permalink
add enabled option to form
Browse files Browse the repository at this point in the history
  • Loading branch information
liverbool committed Dec 25, 2017
1 parent 210a75e commit 529b025
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
9 changes: 8 additions & 1 deletion Form/Type/RecaptchaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ class RecaptchaType extends AbstractType
*/
private $theme;

public function __construct($siteKey, $theme)
/**
* @var string
*/
private $enabled;

public function __construct($siteKey, $theme, $enabled = true)
{
$this->siteKey = $siteKey;
$this->theme = $theme;
$this->enabled = $enabled;
}

/**
Expand All @@ -54,6 +60,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['site_key'] = $this->siteKey;
$view->vars['theme'] = $this->theme;
$view->vars['enabled'] = $this->enabled;
}

/**
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<service class="PhpMob\ReCaptchaBundle\Form\Type\RecaptchaType">
<argument>%phpmob.recaptcha.site_key%</argument>
<argument>%phpmob.recaptcha.theme%</argument>
<argument>%phpmob.recaptcha.enabled%</argument>
<tag name="form.type"/>
</service>
<service class="PhpMob\ReCaptchaBundle\Validator\Constraints\IsValidValidator">
Expand Down
36 changes: 19 additions & 17 deletions Resources/views/form.html.twig
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
{% block phpmob_recaptcha_widget %}
{% spaceless %}
<script type="text/javascript">
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://www.google.com/recaptcha/api.js?onload=onRecaptchaloadCallback&render=explicit";
script.defer = true;
script.async = true;
{% if form.vars.enabled %}
<script type="text/javascript">
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://www.google.com/recaptcha/api.js?onload=onRecaptchaloadCallback&render=explicit";
script.defer = true;
script.async = true;
document.getElementsByTagName("head")[0].appendChild(script);
})();
document.getElementsByTagName("head")[0].appendChild(script);
})();
var onRecaptchaloadCallback = function () {
grecaptcha.render('{{ form.vars.id }}', {
'sitekey': '{{ form.vars.site_key }}',
'theme': '{{ form.vars.theme }}'
});
};
</script>
<div id="{{ form.vars.id }}"></div>
var onRecaptchaloadCallback = function () {
grecaptcha.render('{{ form.vars.id }}', {
'sitekey': '{{ form.vars.site_key }}',
'theme': '{{ form.vars.theme }}'
});
};
</script>
<div id="{{ form.vars.id }}"></div>
{% endif %}
{% endspaceless %}
{% endblock %}

0 comments on commit 529b025

Please sign in to comment.