Radio.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. *
  15. * @package FormBuilder\components
  16. * @method $this size(String $size) 单选框的尺寸,可选值为 large、small、default 或者不设置
  17. * @method $this vertical(Boolean $bool) 是否垂直排列,按钮样式下无效
  18. */
  19. class Radio extends FormComponentDriver
  20. {
  21. use ComponentOptionsTrait;
  22. /**
  23. * @var string
  24. */
  25. protected $name = 'radio';
  26. /**
  27. * @var array
  28. */
  29. protected static $propsRule = [
  30. 'size' => 'string',
  31. 'vertical' => 'boolean'
  32. ];
  33. /**
  34. * 使用按钮样式
  35. *
  36. * @return $this
  37. */
  38. public function button()
  39. {
  40. $this->props['type'] = 'button';
  41. return $this;
  42. }
  43. public function getValidateHandler()
  44. {
  45. return Validate::str();
  46. }
  47. /**
  48. * @return array
  49. */
  50. public function build()
  51. {
  52. $options = [];
  53. foreach ($this->options as $option) {
  54. if ($option instanceof Option)
  55. $options[] = $option->build();
  56. }
  57. return [
  58. 'type' => $this->name,
  59. 'field' => $this->field,
  60. 'title' => $this->title,
  61. 'value' => $this->value,
  62. 'props' => (object)$this->props,
  63. 'options' => $options,
  64. 'validate' => $this->validate,
  65. 'col' => $this->col
  66. ];
  67. }
  68. }