链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.7 KiB

6 years ago
6 years ago
  1. <?php
  2. use Dcat\Admin\Admin;
  3. use Dcat\Admin\Grid;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Grid\Filter;
  6. use Dcat\Admin\Show;
  7. use Dcat\Admin\Form\Field;
  8. /**
  9. * Dcat-admin - admin builder based on Laravel.
  10. * @author jqh <https://github.com/jqhph>
  11. *
  12. * Bootstraper for Admin.
  13. *
  14. * Here you can remove builtin form field:
  15. *
  16. * extend custom field:
  17. * Dcat\Admin\Form::extend('php', PHPEditor::class);
  18. * Dcat\Admin\Grid\Column::extend('php', PHPEditor::class);
  19. * Dcat\Admin\Grid\Filter::extend('php', PHPEditor::class);
  20. *
  21. * Or require js and css assets:
  22. * Admin::css('/packages/prettydocs/css/styles.css');
  23. * Admin::js('/packages/prettydocs/js/main.js');
  24. *
  25. */
  26. // 官方例子,验证字符长度
  27. Field\Text::macro('len', function (int $length, ?string $error = null) {
  28. // 前端验证逻辑扩展
  29. Admin::script(
  30. <<<'JS'
  31. Dcat.validator.extend('len', function ($el) {
  32. console.log($el.val().length , $el.attr('data-len'));
  33. return $el.val().length != $el.attr('data-len');
  34. });
  35. JS
  36. );
  37. // 同时添加后端验证逻辑,这个可以看需要
  38. $this->rules('size:'.$length);
  39. return $this->attribute([
  40. 'data-len' => $length,
  41. 'data-len-error' => str_replace(
  42. [':attribute', ':len'],
  43. [$this->label, $length],
  44. $error ?: "只能输入:len个字符"
  45. ),
  46. ]);
  47. });
  48. /* 验证最多包含两位小数的浮点数,可不带小数 */
  49. Field\Text::macro('floatTwo', function (int $bit = 2, ?string $error = null) {
  50. // 前端验证逻辑扩展
  51. Admin::script(
  52. <<<'JS'
  53. Dcat.validator.extend('floatTwo', function ($el) {
  54. return !(/^(([1-9]{1}\d*)|(0{1}))(\.\d{0,2})?$/.test($el.val()));
  55. });
  56. JS
  57. );
  58. // 同时添加后端验证逻辑,这个可以看需要
  59. // $this->rules('size:'.$length);
  60. return $this->attribute([
  61. 'data-floatTwo' => '^(([1-9]{1}\d*)|(0{1}))(\.\d{0,2})?$',
  62. 'data-floatTwo-error' => "只能输入数字(最多包含两位小数)"
  63. ]);
  64. });
  65. /* 验证-选择不同优惠类型时的优惠金额验证 */
  66. Field\Text::macro('discount', function ( ?string $error = null) {
  67. // 前端验证逻辑扩展
  68. Admin::script(
  69. <<<'JS'
  70. Dcat.validator.extend('discount', function ($el) {
  71. $discount_type = $('');
  72. return !(/^(([1-9]{1}\d*)|(0{1}))(\.\d{0,2})?$/.test($el.val()));
  73. });
  74. JS
  75. );
  76. // 同时添加后端验证逻辑,这个可以看需要
  77. // $this->rules('size:'.$length);
  78. return $this->attribute([
  79. 'data-discount' => '1',
  80. 'data-discount-error' => "只能输入数字"
  81. ]);
  82. });