Date.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Admin;
  4. class Date extends Text
  5. {
  6. protected $format = 'YYYY-MM-DD';
  7. public function format($format)
  8. {
  9. $this->format = $format;
  10. return $this;
  11. }
  12. protected function prepareToSave($value)
  13. {
  14. if ($value === '') {
  15. $value = null;
  16. }
  17. return $value;
  18. }
  19. public function render()
  20. {
  21. $this->options['format'] = $this->format;
  22. $this->options['locale'] = config('app.locale');
  23. $this->options['allowInputToggle'] = true;
  24. $this->script = "$('{$this->getElementClassSelector()}').parent().datetimepicker(".json_encode($this->options).');';
  25. $this->prepend('<i class="fa fa-calendar fa-fw"></i>')
  26. ->defaultAttribute('style', 'width: 200px');
  27. return parent::render();
  28. }
  29. public static function collectAssets()
  30. {
  31. Admin::collectComponentAssets('moment');
  32. Admin::collectComponentAssets('bootstrap-datetimepicker');
  33. }
  34. }