Commit c7fc3b45 authored by Nguyen Van Anh's avatar Nguyen Van Anh Committed by GitHub

Merge pull request #125 from marivaldojr/master

displaySubmit with custom data-callback
parents a5aefd96 159f260e
...@@ -82,16 +82,20 @@ class NoCaptcha ...@@ -82,16 +82,20 @@ class NoCaptcha
*/ */
public function displaySubmit($formIdentifier, $text = 'submit', $attributes = []) public function displaySubmit($formIdentifier, $text = 'submit', $attributes = [])
{ {
$functionName = 'onSubmit' . str_replace(['-', '=', '\'', '"', '<', '>', '`'], '', $formIdentifier); $javascript = '';
$attributes['data-callback'] = $functionName; if (!isset($attributes['data-callback'])) {
$functionName = 'onSubmit' . str_replace(['-', '=', '\'', '"', '<', '>', '`'], '', $formIdentifier);
$attributes['data-callback'] = $functionName;
$javascript = sprintf(
'<script>function %s(){document.getElementById("%s").submit();}</script>',
$functionName,
$formIdentifier
);
}
$attributes = $this->prepareAttributes($attributes); $attributes = $this->prepareAttributes($attributes);
$button = sprintf('<button%s><span>%s</span></button>', $this->buildAttributes($attributes), $text); $button = sprintf('<button%s><span>%s</span></button>', $this->buildAttributes($attributes), $text);
$javascript = sprintf(
'<script>function %s(){document.getElementById("%s").submit();}</script>',
$functionName,
$formIdentifier
);
return $button . $javascript; return $button . $javascript;
} }
......
...@@ -23,9 +23,9 @@ class NoCaptchaTest extends PHPUnit_Framework_TestCase ...@@ -23,9 +23,9 @@ class NoCaptchaTest extends PHPUnit_Framework_TestCase
$withLang = '<script src="https://www.google.com/recaptcha/api.js?hl=vi" async defer></script>'."\n"; $withLang = '<script src="https://www.google.com/recaptcha/api.js?hl=vi" async defer></script>'."\n";
$withCallback = '<script src="https://www.google.com/recaptcha/api.js?render=explicit&onload=myOnloadCallback" async defer></script>'."\n"; $withCallback = '<script src="https://www.google.com/recaptcha/api.js?render=explicit&onload=myOnloadCallback" async defer></script>'."\n";
$this->assertEquals($this->captcha->renderJs(), $simple); $this->assertEquals($simple, $this->captcha->renderJs());
$this->assertEquals($this->captcha->renderJs('vi'), $withLang); $this->assertEquals($withLang, $this->captcha->renderJs('vi'));
$this->assertEquals($this->captcha->renderJs(null, true, 'myOnloadCallback'), $withCallback); $this->assertEquals($withCallback, $this->captcha->renderJs(null, true, 'myOnloadCallback'));
} }
public function testDisplay() public function testDisplay()
...@@ -35,8 +35,8 @@ class NoCaptchaTest extends PHPUnit_Framework_TestCase ...@@ -35,8 +35,8 @@ class NoCaptchaTest extends PHPUnit_Framework_TestCase
$simple = '<div data-sitekey="{site-key}" class="g-recaptcha"></div>'; $simple = '<div data-sitekey="{site-key}" class="g-recaptcha"></div>';
$withAttrs = '<div data-theme="light" data-sitekey="{site-key}" class="g-recaptcha"></div>'; $withAttrs = '<div data-theme="light" data-sitekey="{site-key}" class="g-recaptcha"></div>';
$this->assertEquals($this->captcha->display(), $simple); $this->assertEquals($simple, $this->captcha->display());
$this->assertEquals($this->captcha->display(['data-theme' => 'light']), $withAttrs); $this->assertEquals($withAttrs, $this->captcha->display(['data-theme' => 'light']));
} }
public function testdisplaySubmit() public function testdisplaySubmit()
...@@ -47,8 +47,18 @@ class NoCaptchaTest extends PHPUnit_Framework_TestCase ...@@ -47,8 +47,18 @@ class NoCaptchaTest extends PHPUnit_Framework_TestCase
$simple = '<button data-callback="onSubmittest" data-sitekey="{site-key}" class="g-recaptcha"><span>submit</span></button>'; $simple = '<button data-callback="onSubmittest" data-sitekey="{site-key}" class="g-recaptcha"><span>submit</span></button>';
$withAttrs = '<button data-theme="light" class="g-recaptcha 123" data-callback="onSubmittest" data-sitekey="{site-key}"><span>submit123</span></button>'; $withAttrs = '<button data-theme="light" class="g-recaptcha 123" data-callback="onSubmittest" data-sitekey="{site-key}"><span>submit123</span></button>';
$this->assertEquals($this->captcha->displaySubmit('test'), $simple . $javascript); $this->assertEquals($simple . $javascript, $this->captcha->displaySubmit('test'));
$withAttrsResult = $this->captcha->displaySubmit('test','submit123',['data-theme' => 'light', 'class' => '123']); $withAttrsResult = $this->captcha->displaySubmit('test','submit123',['data-theme' => 'light', 'class' => '123']);
$this->assertEquals($withAttrsResult, $withAttrs . $javascript); $this->assertEquals($withAttrs . $javascript, $withAttrsResult);
}
public function testdisplaySubmitWithCustomCallback()
{
$this->assertTrue($this->captcha instanceof NoCaptcha);
$withAttrs = '<button data-theme="light" class="g-recaptcha 123" data-callback="onSubmitCustomCallback" data-sitekey="{site-key}"><span>submit123</span></button>';
$withAttrsResult = $this->captcha->displaySubmit('test-custom','submit123',['data-theme' => 'light', 'class' => '123', 'data-callback' => 'onSubmitCustomCallback']);
$this->assertEquals($withAttrs, $withAttrsResult);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment