链街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.

96 lines
2.6 KiB

5 years ago
5 years ago
5 years ago
5 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. Form\Field\Map::collectAssets();
  28. // 官方例子,验证字符长度
  29. Field\Text::macro('len', function (int $length, ?string $error = null) {
  30. // 前端验证逻辑扩展
  31. Admin::script(
  32. <<<'JS'
  33. Dcat.validator.extend('len', function ($el) {
  34. console.log($el.val().length , $el.attr('data-len'));
  35. return $el.val().length != $el.attr('data-len');
  36. });
  37. JS
  38. );
  39. // 同时添加后端验证逻辑,这个可以看需要
  40. $this->rules('size:'.$length);
  41. return $this->attribute([
  42. 'data-len' => $length,
  43. 'data-len-error' => str_replace(
  44. [':attribute', ':len'],
  45. [$this->label, $length],
  46. $error ?: "只能输入:len个字符"
  47. ),
  48. ]);
  49. });
  50. /* 验证最多包含两位小数的浮点数,可不带小数 */
  51. Field\Text::macro('floatTwo', function (int $bit = 2, ?string $error = null) {
  52. // 前端验证逻辑扩展
  53. Admin::script(
  54. <<<'JS'
  55. Dcat.validator.extend('floatTwo', function ($el) {
  56. return !(/^(([1-9]{1}\d*)|(0{1}))(\.\d{0,2})?$/.test($el.val()));
  57. });
  58. JS
  59. );
  60. // 同时添加后端验证逻辑,这个可以看需要
  61. // $this->rules('size:'.$length);
  62. return $this->attribute([
  63. 'data-floatTwo' => '^(([1-9]{1}\d*)|(0{1}))(\.\d{0,2})?$',
  64. 'data-floatTwo-error' => "只能输入数字(最多包含两位小数)"
  65. ]);
  66. });
  67. /* 验证-选择不同优惠类型时的优惠金额验证 */
  68. Field\Text::macro('discount', function ( ?string $error = null) {
  69. // 前端验证逻辑扩展
  70. Admin::script(
  71. <<<'JS'
  72. Dcat.validator.extend('discount', function ($el) {
  73. $discount_type = $('');
  74. return !(/^(([1-9]{1}\d*)|(0{1}))(\.\d{0,2})?$/.test($el.val()));
  75. });
  76. JS
  77. );
  78. // 同时添加后端验证逻辑,这个可以看需要
  79. // $this->rules('size:'.$length);
  80. return $this->attribute([
  81. 'data-discount' => '1',
  82. 'data-discount-error' => "只能输入数字"
  83. ]);
  84. });