FormComponent.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\Driver;
  13. use FormBuilder\Contract\FormComponentInterface;
  14. use FormBuilder\Contract\ValidateInterface;
  15. abstract class FormComponent extends CustomComponent implements FormComponentInterface
  16. {
  17. protected $defaultValue = '';
  18. protected $selectComponent = false;
  19. /**
  20. * FormComponentDriver constructor.
  21. *
  22. * @param string $field 字段名
  23. * @param string $title 字段昵称
  24. * @param mixed $value 字段值
  25. */
  26. public function __construct($field, $title, $value = null)
  27. {
  28. parent::__construct();
  29. $this->field($field)->title($title)->value(is_null($value) ? $this->defaultValue : $value);
  30. if (isset(static::$propsRule['placeholder']))
  31. $this->placeholder($this->getPlaceHolder());
  32. $this->init();
  33. }
  34. protected function init()
  35. {
  36. }
  37. /**
  38. * @return ValidateInterface
  39. */
  40. abstract public function createValidate();
  41. /**
  42. * @param null|string $message
  43. * @return $this
  44. */
  45. public function required($message = null)
  46. {
  47. if (is_null($message)) $message = $this->getPlaceHolder();
  48. $this->appendValidate($this->createValidate()->message($message)->required());
  49. return $this;
  50. }
  51. /**
  52. * @return string
  53. */
  54. protected function getPlaceHolder()
  55. {
  56. return ($this->selectComponent ? '请选择' : '请输入') . $this->title;
  57. }
  58. }