Captcha.php 1002 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. class Captcha extends Text
  4. {
  5. protected $rules = ['required', 'captcha'];
  6. protected $view = 'admin::form.captcha';
  7. public function __construct()
  8. {
  9. if (! class_exists(\Mews\Captcha\Captcha::class)) {
  10. throw new \Exception('To use captcha field, please install [mews/captcha] first.');
  11. }
  12. $this->column = '__captcha__';
  13. $this->label = trans('admin.captcha');
  14. }
  15. public function setForm($form = null)
  16. {
  17. parent::setForm($form);
  18. if (method_exists($this->form, 'ignore')) {
  19. $this->form->ignore($this->column);
  20. }
  21. return $this;
  22. }
  23. public function render()
  24. {
  25. $this->script = <<<'JS'
  26. $('.field-refresh-captcha').off('click').on('click', function () {
  27. $(this).attr('src', $(this).attr('data-url')+'?'+Math.random());
  28. });
  29. JS;
  30. $this->addVariables(['captchaSrc' => captcha_src()]);
  31. return parent::render();
  32. }
  33. }