Upload.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * PHP表单生成器
  4. *
  5. * @package FormBuilder
  6. * @author xaboy <xaboy2005@qq.com>
  7. * @version 2.0
  8. * @license MIT
  9. * @link https://github.com/xaboy/form-builder
  10. * @document http://php.form-create.com
  11. */
  12. namespace FormBuilder\UI\Elm\Components;
  13. use FormBuilder\Driver\FormComponent;
  14. use FormBuilder\Factory\Elm;
  15. /**
  16. * 上传组件
  17. * Class Upload
  18. *
  19. * @method $this uploadType(string $uploadType) 上传文件类型,可选值为 image(图片上传),file(文件上传)
  20. * @method $this action(string $action) 必选参数,上传的地址
  21. * @method $this multiple(bool $multiple) 是否支持多选文件
  22. * @method $this uploadName(string $name) 上传的文件字段名, 默认值: file
  23. * @method $this withCredentials(bool $withCredentials) 支持发送 cookie 凭证信息, 默认值: false
  24. * @method $this accept(string $accept) 接受上传的文件类型(thumbnail-mode 模式下此参数无效)
  25. * @method $this listType(string $listType) 文件列表的类型, 可选值: text/picture/picture-card, 默认值: text
  26. * @method $this autoUpload(bool $autoUpload) 是否在选取文件后立即进行上传, 默认值: true
  27. * @method $this disabled(bool $disabled) 是否禁用, 默认值: false
  28. * @method $this limit(float $limit) 最大允许上传个数
  29. *
  30. */
  31. class Upload extends FormComponent
  32. {
  33. /**
  34. * file类型
  35. */
  36. const TYPE_FILE = 'file';
  37. /**
  38. * image类型
  39. */
  40. const TYPE_IMAGE = 'image';
  41. protected $defaultProps = [
  42. 'limit' => 0,
  43. 'uploadType' => self::TYPE_FILE,
  44. 'headers' => [],
  45. 'data' => []
  46. ];
  47. protected static $propsRule = [
  48. 'uploadType' => 'string',
  49. 'action' => 'string',
  50. 'multiple' => 'bool',
  51. 'uploadName' => ['string', 'name'],
  52. 'withCredentials' => 'bool',
  53. 'accept' => 'string',
  54. 'listType' => 'string',
  55. 'autoUpload' => 'bool',
  56. 'disabled' => 'bool',
  57. 'limit' => 'float',
  58. ];
  59. protected function init()
  60. {
  61. $this->name($this->field);
  62. }
  63. /**
  64. * 设置上传的请求头部
  65. *
  66. * @param array $headers
  67. * @return $this
  68. */
  69. public function headers(array $headers)
  70. {
  71. $this->props['headers'] = array_merge($this->props['headers'], $headers);
  72. return $this;
  73. }
  74. /**
  75. * 上传时附带的额外参数
  76. *
  77. * @param array $data
  78. * @return $this
  79. */
  80. public function data(array $data)
  81. {
  82. $this->props['data'] = array_merge($this->props['data'], $data);
  83. return $this;
  84. }
  85. protected function getPlaceHolder()
  86. {
  87. return '请上传' . $this->title;
  88. }
  89. public function createValidate()
  90. {
  91. return $this->props['limit'] == 1 ? Elm::validateStr() : Elm::validateArr();
  92. }
  93. }