Field.php 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Form\Concerns;
  6. use Illuminate\Contracts\Support\Arrayable;
  7. use Illuminate\Contracts\Support\Renderable;
  8. use Illuminate\Support\Arr;
  9. use Illuminate\Support\Fluent;
  10. use Illuminate\Support\Traits\Macroable;
  11. /**
  12. * Class Field.
  13. */
  14. class Field implements Renderable
  15. {
  16. use Macroable, Concerns\FieldValidator;
  17. const FILE_DELETE_FLAG = '_file_del_';
  18. /**
  19. * Element id.
  20. *
  21. * @var array|string
  22. */
  23. protected $id;
  24. /**
  25. * Element value.
  26. *
  27. * @var mixed
  28. */
  29. protected $value;
  30. /**
  31. * Data of all original columns of value.
  32. *
  33. * @var mixed
  34. */
  35. protected $data;
  36. /**
  37. * Field original value.
  38. *
  39. * @var mixed
  40. */
  41. protected $original;
  42. /**
  43. * Field default value.
  44. *
  45. * @var mixed
  46. */
  47. protected $default;
  48. /**
  49. * Element label.
  50. *
  51. * @var string
  52. */
  53. protected $label = '';
  54. /**
  55. * Column name.
  56. *
  57. * @var string|array
  58. */
  59. protected $column = '';
  60. /**
  61. * Form element name.
  62. *
  63. * @var string
  64. */
  65. protected $elementName = [];
  66. /**
  67. * Form element classes.
  68. *
  69. * @var array
  70. */
  71. protected $elementClass = [];
  72. /**
  73. * Variables of elements.
  74. *
  75. * @var array
  76. */
  77. protected $variables = [];
  78. /**
  79. * Options for specify elements.
  80. *
  81. * @var array
  82. */
  83. protected $options = [];
  84. /**
  85. * Checked for specify elements.
  86. *
  87. * @var array
  88. */
  89. protected $checked = [];
  90. /**
  91. * Css required by this field.
  92. *
  93. * @var array
  94. */
  95. protected static $css = [];
  96. /**
  97. * Js required by this field.
  98. *
  99. * @var array
  100. */
  101. protected static $js = [];
  102. /**
  103. * Script for field.
  104. *
  105. * @var string
  106. */
  107. protected $script = '';
  108. /**
  109. * Element attributes.
  110. *
  111. * @var array
  112. */
  113. protected $attributes = [];
  114. /**
  115. * Parent form.
  116. *
  117. * @var Form
  118. */
  119. protected $form = null;
  120. /**
  121. * View for field to render.
  122. *
  123. * @var string
  124. */
  125. protected $view = '';
  126. /**
  127. * Help block.
  128. *
  129. * @var array
  130. */
  131. protected $help = [];
  132. /**
  133. * Key for errors.
  134. *
  135. * @var mixed
  136. */
  137. protected $errorKey;
  138. /**
  139. * Placeholder for this field.
  140. *
  141. * @var string|array
  142. */
  143. protected $placeholder;
  144. /**
  145. * Width for label and field.
  146. *
  147. * @var array
  148. */
  149. protected $width = [
  150. 'label' => 2,
  151. 'field' => 8,
  152. ];
  153. /**
  154. * If the form horizontal layout.
  155. *
  156. * @var bool
  157. */
  158. protected $horizontal = true;
  159. /**
  160. * column data format.
  161. *
  162. * @var \Closure
  163. */
  164. protected $customFormat = null;
  165. /**
  166. * @var bool
  167. */
  168. protected $display = true;
  169. /**
  170. * @var array
  171. */
  172. protected $labelClass = [];
  173. /**
  174. * @var \Closure
  175. */
  176. protected $prepare;
  177. /**
  178. * Field constructor.
  179. *
  180. * @param $column
  181. * @param array $arguments
  182. */
  183. public function __construct($column, $arguments = [])
  184. {
  185. $this->column = $column;
  186. $this->label = $this->formatLabel($arguments);
  187. $this->id = $this->formatId($column);
  188. }
  189. /**
  190. * Get the field element id.
  191. *
  192. * @return string
  193. */
  194. public function getElementId()
  195. {
  196. return $this->id;
  197. }
  198. /**
  199. * Format the field element id.
  200. *
  201. * @param string|array $column
  202. *
  203. * @return string|array
  204. */
  205. protected function formatId($column)
  206. {
  207. if (is_array($column)) {
  208. return str_replace('.', '-', $column);
  209. }
  210. return 'form-field-'.str_replace('.', '-', $column);
  211. }
  212. /**
  213. * Format the label value.
  214. *
  215. * @param array $arguments
  216. *
  217. * @return string
  218. */
  219. protected function formatLabel($arguments = [])
  220. {
  221. $column = is_array($this->column) ? current($this->column) : $this->column;
  222. $label = isset($arguments[0]) ? $arguments[0] : ucfirst(admin_trans_field($column));
  223. return str_replace(['.', '_'], ' ', $label);
  224. }
  225. /**
  226. * Format the name of the field.
  227. *
  228. * @param string $column
  229. *
  230. * @return array|mixed|string
  231. */
  232. protected function formatName($column)
  233. {
  234. if (is_string($column)) {
  235. $name = explode('.', $column);
  236. if (count($name) == 1) {
  237. return $name[0];
  238. }
  239. $html = array_shift($name);
  240. foreach ($name as $piece) {
  241. $html .= "[$piece]";
  242. }
  243. return $html;
  244. }
  245. if (is_array($this->column)) {
  246. $names = [];
  247. foreach ($this->column as $key => $name) {
  248. $names[$key] = $this->formatName($name);
  249. }
  250. return $names;
  251. }
  252. return '';
  253. }
  254. /**
  255. * Set form element name.
  256. *
  257. * @param string $name
  258. *
  259. * @return $this
  260. *
  261. * @author Edwin Hui
  262. */
  263. public function setElementName($name)
  264. {
  265. $this->elementName = $name;
  266. return $this;
  267. }
  268. /**
  269. * Fill data to the field.
  270. *
  271. * @param array $data
  272. *
  273. * @return void
  274. */
  275. final public function fill($data)
  276. {
  277. $this->data($data);
  278. $this->value = $this->formatFieldData($data);
  279. $this->callCustomFormatter();
  280. }
  281. /**
  282. * Format field data.
  283. *
  284. * @param array $data
  285. * @return mixed
  286. */
  287. protected function formatFieldData($data)
  288. {
  289. if (is_array($this->column)) {
  290. $value = [];
  291. foreach ($this->column as $key => $column) {
  292. $value[$key] = Arr::get($data, $column);
  293. }
  294. return $value;
  295. }
  296. return Arr::get($data, $this->column, $this->value);
  297. }
  298. /**
  299. * custom format form column data when edit.
  300. *
  301. * @param \Closure $call
  302. *
  303. * @return $this
  304. */
  305. public function customFormat(\Closure $call)
  306. {
  307. $this->customFormat = $call;
  308. return $this;
  309. }
  310. /**
  311. * Set original value to the field.
  312. *
  313. * @param array $data
  314. * @return void
  315. */
  316. final public function setOriginal($data)
  317. {
  318. $this->original = $this->formatFieldData($data);
  319. $this->callCustomFormatter('original', new Fluent($data));
  320. }
  321. /**
  322. * @param string $key
  323. * @param Fluent|null $dataremoveField
  324. */
  325. protected function callCustomFormatter($key = 'value', Fluent $data = null)
  326. {
  327. if ($this->customFormat) {
  328. $this->{$key} = $this->customFormat
  329. ->call(
  330. $data ?: $this->data(),
  331. $this->{$key},
  332. $this->column,
  333. $this
  334. );
  335. }
  336. }
  337. /**
  338. * @param Form $form
  339. *
  340. * @return $this
  341. */
  342. public function setForm(Form $form = null)
  343. {
  344. $this->form = $form;
  345. return $this;
  346. }
  347. /**
  348. * @return Fluent
  349. */
  350. public function getFormModel()
  351. {
  352. return $this->form ? $this->form->model() : new Fluent;
  353. }
  354. /**
  355. * Set width for field and label.
  356. *
  357. * @param int $field
  358. * @param int $label
  359. *
  360. * @return $this
  361. */
  362. public function setWidth($field = 8, $label = 2)
  363. {
  364. $this->width = [
  365. 'label' => $label,
  366. 'field' => $field,
  367. ];
  368. return $this;
  369. }
  370. /**
  371. * Set the field options.
  372. *
  373. * @param array $options
  374. *
  375. * @return $this
  376. */
  377. public function options($options = [])
  378. {
  379. if ($options instanceof Arrayable) {
  380. $options = $options->toArray();
  381. }
  382. $this->options = array_merge($this->options, $options);
  383. return $this;
  384. }
  385. /**
  386. * Set the field option checked.
  387. *
  388. * @param array $checked
  389. *
  390. * @return $this
  391. */
  392. public function checked($checked = [])
  393. {
  394. if ($checked instanceof Arrayable) {
  395. $checked = $checked->toArray();
  396. }
  397. $this->checked = array_merge($this->checked, (array)$checked);
  398. return $this;
  399. }
  400. /**
  401. * Get key for error message.
  402. *
  403. * @return string
  404. */
  405. public function getErrorKey()
  406. {
  407. return $this->errorKey ?: $this->column;
  408. }
  409. /**
  410. * Set key for error message.
  411. *
  412. * @param string $key
  413. *
  414. * @return $this
  415. */
  416. public function setErrorKey($key)
  417. {
  418. $this->errorKey = $key;
  419. return $this;
  420. }
  421. /**
  422. * Set or get value of the field.
  423. *
  424. * @param null $value
  425. *
  426. * @return mixed
  427. */
  428. public function value($value = null)
  429. {
  430. if (is_null($value)) {
  431. return is_null($this->value) ? $this->getDefault() : $this->value;
  432. }
  433. $this->value = $value;
  434. return $this;
  435. }
  436. /**
  437. * Set or get data.
  438. *
  439. * @param array $data
  440. *
  441. * @return $this
  442. */
  443. public function data(array $data = null)
  444. {
  445. if (is_null($data)) {
  446. return $this->data ?: ($this->data = new Fluent);
  447. }
  448. $this->data = new Fluent($data);
  449. return $this;
  450. }
  451. /**
  452. * Set default value for field.
  453. *
  454. * @param $default
  455. *
  456. * @return $this
  457. */
  458. public function default($default)
  459. {
  460. $this->default = $default;
  461. return $this;
  462. }
  463. /**
  464. * Get default value.
  465. *
  466. * @return mixed
  467. */
  468. public function getDefault()
  469. {
  470. if ($this->default instanceof \Closure) {
  471. return call_user_func($this->default, $this->form);
  472. }
  473. return $this->default;
  474. }
  475. /**
  476. * Set help block for current field.
  477. *
  478. * @param string $text
  479. * @param string $icon
  480. *
  481. * @return $this
  482. */
  483. public function help($text = '', $icon = 'fa-info-circle')
  484. {
  485. $this->help = compact('text', 'icon');
  486. return $this;
  487. }
  488. /**
  489. * Get column of the field.
  490. *
  491. * @return string|array
  492. */
  493. public function column()
  494. {
  495. return $this->column;
  496. }
  497. /**
  498. * Get or set label of the field.
  499. *
  500. * @param null $label
  501. * @return $this|string
  502. */
  503. public function label($label = null)
  504. {
  505. if ($label == null) {
  506. return $this->label;
  507. }
  508. if ($label instanceof \Closure) {
  509. $label = $label($this->label);
  510. }
  511. $this->label = $label;
  512. return $this;
  513. }
  514. public function old()
  515. {
  516. return old($this->column, $this->value());
  517. }
  518. /**
  519. * Get original value of the field.
  520. *
  521. * @return mixed
  522. */
  523. public function original()
  524. {
  525. return $this->original;
  526. }
  527. /**
  528. * Sanitize input data.
  529. *
  530. * @param array $input
  531. * @param string $column
  532. *
  533. * @return array
  534. */
  535. protected function sanitizeInput($input, $column)
  536. {
  537. if ($this instanceof Field\MultipleSelect) {
  538. $value = Arr::get($input, $column);
  539. Arr::set($input, $column, array_filter($value));
  540. }
  541. return $input;
  542. }
  543. /**
  544. * Add html attributes to elements.
  545. *
  546. * @param array|string $attribute
  547. * @param mixed $value
  548. *
  549. * @return $this
  550. */
  551. public function attribute($attribute, $value = null)
  552. {
  553. if (is_array($attribute)) {
  554. $this->attributes = array_merge($this->attributes, $attribute);
  555. } else {
  556. $this->attributes[$attribute] = (string) $value;
  557. }
  558. return $this;
  559. }
  560. /**
  561. * Specifies a regular expression against which to validate the value of the input.
  562. *
  563. * @param string $regexp
  564. *
  565. * @return $this
  566. */
  567. public function pattern($regexp)
  568. {
  569. return $this->attribute('pattern', $regexp);
  570. }
  571. /**
  572. * set the input filed required.
  573. *
  574. * @param bool $isLabelAsterisked
  575. *
  576. * @return $this
  577. */
  578. public function required($isLabelAsterisked = true)
  579. {
  580. if ($isLabelAsterisked) {
  581. $this->setLabelClass(['asterisk']);
  582. }
  583. $this->rules('required');
  584. return $this->attribute('required', true);
  585. }
  586. /**
  587. * Set the field automatically get focus.
  588. *
  589. * @return $this
  590. */
  591. public function autofocus()
  592. {
  593. return $this->attribute('autofocus', true);
  594. }
  595. /**
  596. * Set the field as readonly mode.
  597. *
  598. * @return $this
  599. */
  600. public function readOnly()
  601. {
  602. return $this->attribute('readonly', true);
  603. }
  604. /**
  605. * Set field as disabled.
  606. *
  607. * @return $this
  608. */
  609. public function disable()
  610. {
  611. return $this->attribute('disabled', true);
  612. }
  613. /**
  614. * Set field placeholder.
  615. *
  616. * @param string $placeholder
  617. *
  618. * @return $this
  619. */
  620. public function placeholder($placeholder = '')
  621. {
  622. $this->placeholder = $placeholder;
  623. return $this;
  624. }
  625. /**
  626. * Get placeholder.
  627. *
  628. * @return string
  629. */
  630. public function getPlaceholder()
  631. {
  632. return $this->placeholder ?: trans('admin.input').' '.$this->label;
  633. }
  634. /**
  635. * Prepare for a field value before update or insert.
  636. *
  637. * @param mixed $value
  638. * @return mixed
  639. */
  640. public function prepare($value)
  641. {
  642. return $value;
  643. }
  644. /**
  645. * @param \Closure $closure
  646. * @return $this
  647. */
  648. public function prepareForSave(\Closure $closure)
  649. {
  650. $this->prepare = $closure;
  651. return $this;
  652. }
  653. /**
  654. * Prepare for a field value before update or insert.
  655. *
  656. * @param mixed $value
  657. * @return mixed
  658. */
  659. final public function prepareInputValue($value)
  660. {
  661. $value = $this->prepare($value);
  662. if ($handler = $this->prepare) {
  663. $handler->bindTo($this->data);
  664. return $handler($value);
  665. }
  666. return $value;
  667. }
  668. /**
  669. * Format the field attributes.
  670. *
  671. * @return string
  672. */
  673. protected function formatAttributes()
  674. {
  675. $html = [];
  676. foreach ($this->attributes as $name => $value) {
  677. $html[] = $name.'="'.e($value).'"';
  678. }
  679. return implode(' ', $html);
  680. }
  681. /**
  682. * @return $this
  683. */
  684. public function disableHorizontal()
  685. {
  686. $this->horizontal = false;
  687. return $this;
  688. }
  689. /**
  690. * @return array
  691. */
  692. public function getViewElementClasses()
  693. {
  694. if ($this->horizontal) {
  695. return [
  696. 'label' => "col-sm-{$this->width['label']} {$this->getLabelClass()}",
  697. 'field' => "col-sm-{$this->width['field']}",
  698. 'form-group' => 'form-group ',
  699. ];
  700. }
  701. return ['label' => $this->getLabelClass(), 'field' => '', 'form-group' => ''];
  702. }
  703. /**
  704. * Set form element class.
  705. *
  706. * @param string|array $class
  707. *
  708. * @return $this
  709. */
  710. public function setElementClass($class)
  711. {
  712. $this->elementClass = array_merge($this->elementClass, (array) $class);
  713. return $this;
  714. }
  715. /**
  716. * Get element class.
  717. *
  718. * @return array
  719. */
  720. protected function getElementClass()
  721. {
  722. if (!$this->elementClass) {
  723. $name = $this->elementName ?: $this->formatName($this->column);
  724. $this->elementClass = (array) str_replace(['[', ']'], '_', $name);
  725. }
  726. return $this->elementClass;
  727. }
  728. /**
  729. * Get element class string.
  730. *
  731. * @return mixed
  732. */
  733. protected function getElementClassString()
  734. {
  735. $elementClass = $this->getElementClass();
  736. if (Arr::isAssoc($elementClass)) {
  737. $classes = [];
  738. foreach ($elementClass as $index => $class) {
  739. $classes[$index] = is_array($class) ? implode(' ', $class) : $class;
  740. }
  741. return $classes;
  742. }
  743. return implode(' ', $elementClass);
  744. }
  745. /**
  746. * Get element class selector.
  747. *
  748. * @return string|array
  749. */
  750. protected function getElementClassSelector()
  751. {
  752. $elementClass = $this->getElementClass();
  753. $formId = $this->getFormId();
  754. $formId = $formId ? '#'.$formId : '';
  755. if (Arr::isAssoc($elementClass)) {
  756. $classes = [];
  757. foreach ($elementClass as $index => $class) {
  758. $classes[$index] = $formId . ' .'.(is_array($class) ? implode('.', $class) : $class);
  759. }
  760. return $classes;
  761. }
  762. return $formId . ' .'.implode('.', $elementClass);
  763. }
  764. /**
  765. * Remove the field in dialog.
  766. *
  767. * @return $this
  768. */
  769. public function hideInDialog()
  770. {
  771. if (Form::isDialogFormPage()) {
  772. $this->setDisplay(false);
  773. }
  774. return $this;
  775. }
  776. /**
  777. * @return string|null
  778. */
  779. protected function getFormId()
  780. {
  781. return $this->form ? $this->form->getFormId() : null;
  782. }
  783. /**
  784. * Add the element class.
  785. *
  786. * @param $class
  787. *
  788. * @return $this
  789. */
  790. public function addElementClass($class)
  791. {
  792. if (is_array($class) || is_string($class)) {
  793. $this->elementClass = array_merge($this->elementClass, (array) $class);
  794. $this->elementClass = array_unique($this->elementClass);
  795. }
  796. return $this;
  797. }
  798. /**
  799. * Remove element class.
  800. *
  801. * @param $class
  802. *
  803. * @return $this
  804. */
  805. public function removeElementClass($class)
  806. {
  807. $delClass = [];
  808. if (is_string($class) || is_array($class)) {
  809. $delClass = (array) $class;
  810. }
  811. foreach ($delClass as $del) {
  812. if (($key = array_search($del, $this->elementClass))) {
  813. unset($this->elementClass[$key]);
  814. }
  815. }
  816. return $this;
  817. }
  818. /**
  819. * Add variables to field view.
  820. *
  821. * @param array $variables
  822. *
  823. * @return $this
  824. */
  825. protected function addVariables(array $variables = [])
  826. {
  827. $this->variables = array_merge($this->variables, $variables);
  828. return $this;
  829. }
  830. /**
  831. * @return string
  832. */
  833. public function getLabelClass(): string
  834. {
  835. return implode(' ', $this->labelClass);
  836. }
  837. /**
  838. * @param array $labelClass
  839. *
  840. * @return $this
  841. */
  842. public function setLabelClass(array $labelClass)
  843. {
  844. $this->labelClass = $labelClass;
  845. return $this;
  846. }
  847. /**
  848. * @return string
  849. */
  850. public function getElementName()
  851. {
  852. return $this->elementName ?: $this->formatName($this->column);
  853. }
  854. /**
  855. * Get the view variables of this field.
  856. *
  857. * @return array
  858. */
  859. public function variables()
  860. {
  861. return array_merge($this->variables, [
  862. 'id' => $this->id,
  863. 'name' => $this->getElementName(),
  864. 'help' => $this->help,
  865. 'class' => $this->getElementClassString(),
  866. 'value' => $this->value(),
  867. 'label' => $this->label,
  868. 'viewClass' => $this->getViewElementClasses(),
  869. 'column' => $this->column,
  870. 'errorKey' => $this->getErrorKey(),
  871. 'attributes' => $this->formatAttributes(),
  872. 'placeholder' => $this->getPlaceholder(),
  873. 'disabled' => $this->attributes['disabled'] ?? false,
  874. 'formId' => $this->getFormId(),
  875. ]);
  876. }
  877. /**
  878. * Get view of this field.
  879. *
  880. * @return string
  881. */
  882. public function getView()
  883. {
  884. return $this->view ?: 'admin::form.'.strtolower(class_basename(static::class));
  885. }
  886. /**
  887. * Set view of current field.
  888. *
  889. * @return string
  890. */
  891. public function setView($view)
  892. {
  893. $this->view = $view;
  894. return $this;
  895. }
  896. /**
  897. * Get script of current field.
  898. *
  899. * @return string
  900. */
  901. public function getScript()
  902. {
  903. return $this->script;
  904. }
  905. /**
  906. * Set script of current field.
  907. *
  908. * @return self
  909. */
  910. public function setScript($script)
  911. {
  912. $this->script = $script;
  913. return $this;
  914. }
  915. /**
  916. * To set this field should render or not.
  917. *
  918. * @return self
  919. */
  920. public function setDisplay(bool $display)
  921. {
  922. $this->display = $display;
  923. return $this;
  924. }
  925. /**
  926. * If this field should render.
  927. *
  928. * @return bool
  929. */
  930. protected function shouldRender()
  931. {
  932. return $this->display;
  933. }
  934. /**
  935. * Collect assets required by this field.
  936. */
  937. public static function collectAssets()
  938. {
  939. static::$js && Admin::js(static::$js);
  940. static::$css && Admin::css(static::$css);
  941. }
  942. /**
  943. * Render this filed.
  944. *
  945. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
  946. */
  947. public function render()
  948. {
  949. if (!$this->shouldRender()) {
  950. return '';
  951. }
  952. Admin::script($this->script);
  953. return view($this->getView(), $this->variables());
  954. }
  955. /**
  956. * @return string
  957. */
  958. public function __toString()
  959. {
  960. return $this->render()->render();
  961. }
  962. }