Radio.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * FormBuilder表单生成器
  4. * Author: xaboy
  5. * Github: https://github.com/xaboy/form-builder
  6. */
  7. namespace FormBuilder\components;
  8. use FormBuilder\FormComponentDriver;
  9. use FormBuilder\Helper;
  10. use FormBuilder\traits\component\ComponentOptionsTrait;
  11. /**
  12. * 单选框组件
  13. * Class Radio
  14. * @package FormBuilder\components
  15. * @method $this size(String $size) 单选框的尺寸,可选值为 large、small、default 或者不设置
  16. * @method $this vertical(Boolean $bool) 是否垂直排列,按钮样式下无效
  17. */
  18. class Radio extends FormComponentDriver
  19. {
  20. use ComponentOptionsTrait;
  21. /**
  22. * @var string
  23. */
  24. protected $name = 'radio';
  25. /**
  26. * @var array
  27. */
  28. protected static $propsRule = [
  29. 'size' => 'string',
  30. 'vertical' => 'boolean'
  31. ];
  32. /**
  33. * 使用按钮样式
  34. * @return $this
  35. */
  36. public function button()
  37. {
  38. $this->props['type'] = 'button';
  39. return $this;
  40. }
  41. protected function getValidateHandler()
  42. {
  43. return Validate::str();
  44. }
  45. /**
  46. * @return array
  47. */
  48. public function build()
  49. {
  50. $options = [];
  51. foreach ($this->options as $option) {
  52. if ($option instanceof Option)
  53. $options[] = $option->build();
  54. }
  55. return [
  56. 'type' => $this->name,
  57. 'field' => $this->field,
  58. 'title' => $this->title,
  59. 'value' => $this->value,
  60. 'props' => (object)$this->props,
  61. 'options' => $options,
  62. 'validate' => $this->validate,
  63. 'col'=>$this->col
  64. ];
  65. }
  66. }