Form.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Closure;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Contracts\LazyRenderable;
  6. use Dcat\Admin\Exception\RuntimeException;
  7. use Dcat\Admin\Form\Concerns\HandleCascadeFields;
  8. use Dcat\Admin\Form\Concerns\HasLayout;
  9. use Dcat\Admin\Form\Concerns\HasRows;
  10. use Dcat\Admin\Form\Concerns\HasTabs;
  11. use Dcat\Admin\Form\Field;
  12. use Dcat\Admin\Support\Helper;
  13. use Dcat\Admin\Traits\HasAuthorization;
  14. use Dcat\Admin\Traits\HasFormResponse;
  15. use Dcat\Admin\Traits\HasHtmlAttributes;
  16. use Illuminate\Contracts\Support\Arrayable;
  17. use Illuminate\Contracts\Support\Renderable;
  18. use Illuminate\Http\Request;
  19. use Illuminate\Support\Arr;
  20. use Illuminate\Support\Collection;
  21. use Illuminate\Support\Fluent;
  22. use Illuminate\Support\MessageBag;
  23. use Illuminate\Support\Str;
  24. use Illuminate\Support\Traits\Macroable;
  25. use Illuminate\Validation\Validator;
  26. /**
  27. * Class Form.
  28. *
  29. * @method Field\Text text($column, $label = '')
  30. * @method Field\Checkbox checkbox($column, $label = '')
  31. * @method Field\Radio radio($column, $label = '')
  32. * @method Field\Select select($column, $label = '')
  33. * @method Field\MultipleSelect multipleSelect($column, $label = '')
  34. * @method Field\Textarea textarea($column, $label = '')
  35. * @method Field\Hidden hidden($column, $label = '')
  36. * @method Field\Id id($column, $label = '')
  37. * @method Field\Ip ip($column, $label = '')
  38. * @method Field\Url url($column, $label = '')
  39. * @method Field\Email email($column, $label = '')
  40. * @method Field\Mobile mobile($column, $label = '')
  41. * @method Field\Slider slider($column, $label = '')
  42. * @method Field\Map map($latitude, $longitude, $label = '')
  43. * @method Field\Editor editor($column, $label = '')
  44. * @method Field\Date date($column, $label = '')
  45. * @method Field\Datetime datetime($column, $label = '')
  46. * @method Field\Time time($column, $label = '')
  47. * @method Field\Year year($column, $label = '')
  48. * @method Field\Month month($column, $label = '')
  49. * @method Field\DateRange dateRange($start, $end, $label = '')
  50. * @method Field\DateTimeRange datetimeRange($start, $end, $label = '')
  51. * @method Field\TimeRange timeRange($start, $end, $label = '')
  52. * @method Field\Number number($column, $label = '')
  53. * @method Field\Currency currency($column, $label = '')
  54. * @method Field\SwitchField switch($column, $label = '')
  55. * @method Field\Display display($column, $label = '')
  56. * @method Field\Rate rate($column, $label = '')
  57. * @method Field\Divide divider(string $title = null)
  58. * @method Field\Password password($column, $label = '')
  59. * @method Field\Decimal decimal($column, $label = '')
  60. * @method Field\Html html($html, $label = '')
  61. * @method Field\Tags tags($column, $label = '')
  62. * @method Field\Icon icon($column, $label = '')
  63. * @method Field\Embeds embeds($column, $label = '')
  64. * @method Field\Captcha captcha($column, $label = '')
  65. * @method Field\Listbox listbox($column, $label = '')
  66. * @method Field\File file($column, $label = '')
  67. * @method Field\Image image($column, $label = '')
  68. * @method Field\MultipleFile multipleFile($column, $label = '')
  69. * @method Field\MultipleImage multipleImage($column, $label = '')
  70. * @method Field\Tree tree($column, $label = '')
  71. * @method Field\Table table($column, $callback)
  72. * @method Field\ListField list($column, $label = '')
  73. * @method Field\Timezone timezone($column, $label = '')
  74. * @method Field\KeyValue keyValue($column, $label = '')
  75. * @method Field\Tel tel($column, $label = '')
  76. * @method Field\Markdown markdown($column, $label = '')
  77. * @method Field\Range range($start, $end, $label = '')
  78. * @method Field\Color color($column, $label = '')
  79. * @method Field\ArrayField array($column, $labelOrCallback, $callback = null)
  80. * @method Field\SelectTable selectTable($column, $label = '')
  81. * @method Field\MultipleSelectTable multipleSelectTable($column, $label = '')
  82. * @method Field\Button button(string $html = null)
  83. */
  84. class Form implements Renderable
  85. {
  86. use HasHtmlAttributes;
  87. use HasAuthorization;
  88. use HandleCascadeFields;
  89. use HasRows;
  90. use HasTabs;
  91. use HasLayout;
  92. use HasFormResponse {
  93. setCurrentUrl as defaultSetCurrentUrl;
  94. }
  95. use Macroable {
  96. __call as macroCall;
  97. }
  98. const REQUEST_NAME = '_form_';
  99. const CURRENT_URL_NAME = '_current_';
  100. const LAZY_PAYLOAD_NAME = '_payload_';
  101. /**
  102. * @var string
  103. */
  104. protected $view = 'admin::widgets.form';
  105. /**
  106. * @var Field[]|Collection
  107. */
  108. protected $fields;
  109. /**
  110. * @var array
  111. */
  112. protected $variables = [];
  113. /**
  114. * @var bool
  115. */
  116. protected $ajax = true;
  117. /**
  118. * @var Fluent
  119. */
  120. protected $data;
  121. /**
  122. * @var mixed
  123. */
  124. protected $primaryKey;
  125. /**
  126. * Available buttons.
  127. *
  128. * @var array
  129. */
  130. protected $buttons = ['reset' => true, 'submit' => true];
  131. /**
  132. * @var bool
  133. */
  134. protected $useFormTag = true;
  135. /**
  136. * @var string
  137. */
  138. protected $elementId;
  139. /**
  140. * @var array
  141. */
  142. protected $width = [
  143. 'label' => 2,
  144. 'field' => 8,
  145. ];
  146. /**
  147. * @var array
  148. */
  149. protected $confirm = [];
  150. /**
  151. * @var bool
  152. */
  153. protected $validationErrorToastr = true;
  154. /**
  155. * Form constructor.
  156. *
  157. * @param array $data
  158. * @param mixed $key
  159. */
  160. public function __construct($data = [], $key = null)
  161. {
  162. if ($data) {
  163. $this->fill($data);
  164. }
  165. $this->setKey($key);
  166. $this->setUp();
  167. }
  168. protected function setUp()
  169. {
  170. $this->initFields();
  171. $this->initFormAttributes();
  172. $this->initCurrentUrl();
  173. $this->initPayload();
  174. }
  175. /**
  176. * Initialize the form fields.
  177. */
  178. protected function initFields()
  179. {
  180. $this->fields = new Collection();
  181. }
  182. /**
  183. * Initialize the form attributes.
  184. */
  185. protected function initFormAttributes()
  186. {
  187. $this->setHtmlAttribute([
  188. 'method' => 'POST',
  189. 'action' => '',
  190. 'class' => 'form-horizontal',
  191. 'accept-charset' => 'UTF-8',
  192. 'pjax-container' => true,
  193. ]);
  194. }
  195. protected function initCurrentUrl()
  196. {
  197. if ($this instanceof LazyRenderable) {
  198. $this->setCurrentUrl($this->getCurrentUrl());
  199. }
  200. }
  201. protected function initPayload()
  202. {
  203. if ($payload = \request(static::LAZY_PAYLOAD_NAME)) {
  204. $this->payload(json_decode($payload, true) ?? []);
  205. }
  206. }
  207. /**
  208. * Action uri of the form.
  209. *
  210. * @param string $action
  211. *
  212. * @return $this|string
  213. */
  214. public function action($action = null)
  215. {
  216. if ($action === null) {
  217. return $this->getHtmlAttribute('action');
  218. }
  219. return $this->setHtmlAttribute('action', admin_url($action));
  220. }
  221. /**
  222. * Method of the form.
  223. *
  224. * @param string $method
  225. *
  226. * @return $this
  227. */
  228. public function method(string $method = 'POST')
  229. {
  230. return $this->setHtmlAttribute('method', strtoupper($method));
  231. }
  232. /**
  233. * @param string $title
  234. * @param string $content
  235. *
  236. * @return $this
  237. */
  238. public function confirm(?string $title = null, ?string $content = null)
  239. {
  240. $this->confirm['title'] = $title;
  241. $this->confirm['content'] = $content;
  242. return $this;
  243. }
  244. /**
  245. * 设置使用 Toastr 展示字段验证信息.
  246. *
  247. * @param bool $value
  248. *
  249. * @return $this
  250. */
  251. public function validationErrorToastr(bool $value = true)
  252. {
  253. $this->validationErrorToastr = $value;
  254. return $this;
  255. }
  256. /**
  257. * Set primary key.
  258. *
  259. * @param mixed $value
  260. *
  261. * @return $this
  262. */
  263. public function setKey($value)
  264. {
  265. $this->primaryKey = $value;
  266. return $this;
  267. }
  268. /**
  269. * Get primary key.
  270. *
  271. * @return mixed
  272. */
  273. public function getKey()
  274. {
  275. return $this->primaryKey;
  276. }
  277. /**
  278. * @param array|Arrayable|Closure $data
  279. *
  280. * @return Fluent
  281. */
  282. public function data()
  283. {
  284. if (! $this->data) {
  285. $this->fill([]);
  286. }
  287. return $this->data;
  288. }
  289. /**
  290. * @param array|Arrayable|Closure $data
  291. *
  292. * @return $this
  293. */
  294. public function fill($data)
  295. {
  296. $this->data = new Fluent(Helper::array($data));
  297. return $this;
  298. }
  299. /**
  300. * @return Fluent
  301. */
  302. public function model()
  303. {
  304. return $this->data();
  305. }
  306. /**
  307. * Add a fieldset to form.
  308. *
  309. * @param string $title
  310. * @param Closure $setCallback
  311. *
  312. * @return Field\Fieldset
  313. */
  314. public function fieldset(string $title, Closure $setCallback)
  315. {
  316. $fieldset = new Field\Fieldset();
  317. $this->html($fieldset->start($title))->plain();
  318. $setCallback($this);
  319. $this->html($fieldset->end())->plain();
  320. return $fieldset;
  321. }
  322. /**
  323. * Get specify field.
  324. *
  325. * @param string|Field $name
  326. *
  327. * @return Field|null
  328. */
  329. public function field($name)
  330. {
  331. foreach ($this->fields as $field) {
  332. if (is_array($field->column())) {
  333. $result = in_array($name, $field->column(), true) || $field->column() === $name ? $field : null;
  334. if ($result) {
  335. return $result;
  336. }
  337. }
  338. if ($field === $name || $field->column() === $name) {
  339. return $field;
  340. }
  341. }
  342. }
  343. /**
  344. * @return Field[]|Collection
  345. */
  346. public function fields()
  347. {
  348. return $this->fields;
  349. }
  350. /**
  351. * Validate this form fields.
  352. *
  353. * @param Request $request
  354. *
  355. * @return bool|MessageBag
  356. */
  357. public function validate(Request $request)
  358. {
  359. $failedValidators = [];
  360. /** @var \Dcat\Admin\Form\Field $field */
  361. foreach ($this->fields() as $field) {
  362. if (! $validator = $field->getValidator($request->all())) {
  363. continue;
  364. }
  365. if (($validator instanceof Validator) && ! $validator->passes()) {
  366. $failedValidators[] = $validator;
  367. }
  368. }
  369. $message = $this->mergeValidationMessages($failedValidators);
  370. return $message->any() ? $message : false;
  371. }
  372. /**
  373. * Merge validation messages from input validators.
  374. *
  375. * @param \Illuminate\Validation\Validator[] $validators
  376. *
  377. * @return MessageBag
  378. */
  379. protected function mergeValidationMessages($validators)
  380. {
  381. $messageBag = new MessageBag();
  382. foreach ($validators as $validator) {
  383. $messageBag = $messageBag->merge($validator->messages());
  384. }
  385. return $messageBag;
  386. }
  387. public function useFormTag(bool $tag = true)
  388. {
  389. $this->useFormTag = $tag;
  390. return $this;
  391. }
  392. /**
  393. * @param bool $value
  394. *
  395. * @return $this
  396. */
  397. public function submitButton(bool $value = true)
  398. {
  399. $this->buttons['submit'] = $value;
  400. return $this;
  401. }
  402. /**
  403. * @param bool $value
  404. *
  405. * @return $this
  406. */
  407. public function resetButton(bool $value = true)
  408. {
  409. $this->buttons['reset'] = $value;
  410. return $this;
  411. }
  412. /**
  413. * Disable reset button.
  414. *
  415. * @param bool $value
  416. *
  417. * @return $this
  418. */
  419. public function disableResetButton(bool $value = true)
  420. {
  421. return $this->resetButton(! $value);
  422. }
  423. /**
  424. * Disable submit button.
  425. *
  426. * @param bool $value
  427. *
  428. * @return $this
  429. */
  430. public function disableSubmitButton(bool $value = true)
  431. {
  432. return $this->submitButton(! $value);
  433. }
  434. /**
  435. * Set field and label width in current form.
  436. *
  437. * @param int $fieldWidth
  438. * @param int $labelWidth
  439. *
  440. * @return $this
  441. */
  442. public function width($fieldWidth = 8, $labelWidth = 2)
  443. {
  444. $this->width = [
  445. 'label' => $labelWidth,
  446. 'field' => $fieldWidth,
  447. ];
  448. $this->fields->each(function ($field) use ($fieldWidth, $labelWidth) {
  449. /* @var Field $field */
  450. $field->width($fieldWidth, $labelWidth);
  451. });
  452. return $this;
  453. }
  454. /**
  455. * Find field class with given name.
  456. *
  457. * @param string $method
  458. *
  459. * @return bool|string
  460. */
  461. public static function findFieldClass($method)
  462. {
  463. $class = Arr::get(\Dcat\Admin\Form::extensions(), $method);
  464. if (class_exists($class)) {
  465. return $class;
  466. }
  467. return false;
  468. }
  469. /**
  470. * Add a form field to form.
  471. *
  472. * @param Field $field
  473. *
  474. * @return $this
  475. */
  476. public function pushField(Field $field)
  477. {
  478. $this->fields->push($field);
  479. if ($this->layout()->hasColumns()) {
  480. $this->layout()->addField($field);
  481. }
  482. $field->setForm($this);
  483. $field->width($this->width['field'], $this->width['label']);
  484. $this->setFileUploadUrl($field);
  485. $field::requireAssets();
  486. return $this;
  487. }
  488. protected function setFileUploadUrl(Field $field)
  489. {
  490. if ($field instanceof Field\File && method_exists($this, 'form')) {
  491. $formData = [static::REQUEST_NAME => get_called_class()];
  492. $field->url(route(admin_api_route_name('form.upload')));
  493. $field->deleteUrl(route(admin_api_route_name('form.destroy-file'), $formData));
  494. $field->withFormData($formData);
  495. }
  496. }
  497. /**
  498. * Get variables for render form.
  499. *
  500. * @return array
  501. */
  502. protected function variables()
  503. {
  504. $this->setHtmlAttribute('id', $this->getElementId());
  505. $this->fillFields($this->model()->toArray());
  506. return array_merge([
  507. 'start' => $this->open(),
  508. 'end' => $this->close(),
  509. 'fields' => $this->fields,
  510. 'method' => $this->getHtmlAttribute('method'),
  511. 'rows' => $this->rows(),
  512. 'layout' => $this->layout(),
  513. 'elementId' => $this->getElementId(),
  514. 'ajax' => $this->ajax,
  515. 'footer' => $this->renderFooter(),
  516. ], $this->variables);
  517. }
  518. /**
  519. * 表单底部内容.
  520. *
  521. * @return string
  522. */
  523. protected function renderFooter()
  524. {
  525. if (empty($this->buttons['reset']) && empty($this->buttons['submit'])) {
  526. return;
  527. }
  528. return <<<HTML
  529. <div class="box-footer row d-flex">
  530. <div class="col-md-2"> &nbsp;</div>
  531. <div class="col-md-8">{$this->renderResetButton()}{$this->renderSubmitButton()}</div>
  532. </div>
  533. HTML;
  534. }
  535. protected function renderResetButton()
  536. {
  537. if (! empty($this->buttons['reset'])) {
  538. $reset = trans('admin.reset');
  539. return "<button type=\"reset\" class=\"btn btn-white pull-left\"><i class=\"feather icon-rotate-ccw\"></i> {$reset}</button>";
  540. }
  541. }
  542. protected function renderSubmitButton()
  543. {
  544. if (! empty($this->buttons['submit'])) {
  545. return "<button type=\"submit\" class=\"btn btn-primary pull-right\"><i class=\"feather icon-save\"></i> {$this->getSubmitButtonLabel()}</button>";
  546. }
  547. }
  548. /**
  549. * 提交按钮文本.
  550. *
  551. * @return string
  552. */
  553. protected function getSubmitButtonLabel()
  554. {
  555. return trans('admin.submit');
  556. }
  557. /**
  558. * 设置视图变量.
  559. *
  560. * @param array $variables
  561. *
  562. * @return $this
  563. */
  564. public function addVariables(array $variables)
  565. {
  566. $this->variables = array_merge($this->variables, $variables);
  567. return $this;
  568. }
  569. public function fillFields(array $data)
  570. {
  571. foreach ($this->fields as $field) {
  572. if (! $field->hasAttribute(Field::BUILD_IGNORE)) {
  573. $field->fill($data);
  574. }
  575. }
  576. }
  577. /**
  578. * @return string
  579. */
  580. protected function open()
  581. {
  582. if (! $this->useFormTag) {
  583. return;
  584. }
  585. return <<<HTML
  586. <form {$this->formatHtmlAttributes()}>
  587. HTML;
  588. }
  589. /**
  590. * @return string
  591. */
  592. protected function close()
  593. {
  594. if (! $this->useFormTag) {
  595. return;
  596. }
  597. return '</form>';
  598. }
  599. /**
  600. * Determine if form fields has files.
  601. *
  602. * @return bool
  603. */
  604. public function hasFile()
  605. {
  606. foreach ($this->fields as $field) {
  607. if ($field instanceof Field\File) {
  608. return true;
  609. }
  610. }
  611. return false;
  612. }
  613. /**
  614. * @param $id
  615. *
  616. * @return $this
  617. */
  618. public function setFormId($id)
  619. {
  620. $this->elementId = $id;
  621. return $this;
  622. }
  623. /**
  624. * @return string
  625. */
  626. public function getElementId()
  627. {
  628. return $this->elementId ?: ($this->elementId = 'form-'.Str::random(8));
  629. }
  630. /**
  631. * {@inheritdoc}
  632. */
  633. public function setCurrentUrl($url)
  634. {
  635. if ($this instanceof LazyRenderable) {
  636. $this->payload([static::CURRENT_URL_NAME => $url]);
  637. }
  638. return $this->defaultSetCurrentUrl($url);
  639. }
  640. /**
  641. * @param bool $disable
  642. *
  643. * @return $this
  644. */
  645. public function ajax(bool $value = true)
  646. {
  647. $this->ajax = $value;
  648. return $this;
  649. }
  650. /**
  651. * @return bool
  652. */
  653. public function allowAjaxSubmit()
  654. {
  655. return $this->ajax === true;
  656. }
  657. /**
  658. * @return string|void
  659. */
  660. protected function savedScript()
  661. {
  662. }
  663. /**
  664. * @return string|void
  665. */
  666. protected function errorScript()
  667. {
  668. }
  669. /**
  670. * @param array $input
  671. *
  672. * @return array
  673. */
  674. public function sanitize(array $input)
  675. {
  676. Arr::forget($input, [static::REQUEST_NAME, '_token', static::CURRENT_URL_NAME]);
  677. return $this->prepareInput($input);
  678. }
  679. public function prepareInput(array $input)
  680. {
  681. Helper::prepareHasOneRelation($this->fields, $input);
  682. foreach ($input as $column => $value) {
  683. $field = $this->field($column);
  684. if (! $field instanceof Field) {
  685. unset($input[$column]);
  686. continue;
  687. }
  688. $input[$column] = $field->prepare($value);
  689. }
  690. $prepared = [];
  691. foreach ($input as $key => $value) {
  692. Arr::set($prepared, $key, $value);
  693. }
  694. return $prepared;
  695. }
  696. protected function prepareForm()
  697. {
  698. if (method_exists($this, 'form')) {
  699. $this->form();
  700. }
  701. if (! $this->data && method_exists($this, 'default')) {
  702. $data = $this->default();
  703. if (is_array($data)) {
  704. $this->fill($data);
  705. }
  706. }
  707. }
  708. protected function prepareHandler()
  709. {
  710. if (method_exists($this, 'handle')) {
  711. $addHiddenFields = function () {
  712. $this->method('POST');
  713. $this->action(route(admin_api_route_name('form')));
  714. $this->hidden(static::REQUEST_NAME)->default(get_called_class());
  715. $this->hidden(static::CURRENT_URL_NAME)->default($this->getCurrentUrl());
  716. if (! empty($this->payload) && is_array($this->payload)) {
  717. $this->hidden(static::LAZY_PAYLOAD_NAME)->default(json_encode($this->payload));
  718. }
  719. };
  720. $this->layout()->hasColumns() ? $this->column(1, $addHiddenFields) : $addHiddenFields();
  721. }
  722. }
  723. /**
  724. * Render the form.
  725. *
  726. * @return string
  727. */
  728. public function render()
  729. {
  730. $this->prepareForm();
  731. $this->prepareHandler();
  732. if ($this->allowAjaxSubmit()) {
  733. $this->addAjaxScript();
  734. }
  735. $tabObj = $this->getTab();
  736. if (! $tabObj->isEmpty()) {
  737. $tabObj->addScript();
  738. }
  739. $this->addVariables([
  740. 'tabObj' => $tabObj,
  741. ]);
  742. return view($this->view, $this->variables())->render();
  743. }
  744. protected function addAjaxScript()
  745. {
  746. $confirm = admin_javascript_json($this->confirm);
  747. $toastr = $this->validationErrorToastr ? 'true' : 'false';
  748. Admin::script(
  749. <<<JS
  750. $('#{$this->getElementId()}').form({
  751. validate: true,
  752. confirm: {$confirm},
  753. validationErrorToastr: $toastr,
  754. success: function (data) {
  755. {$this->savedScript()}
  756. },
  757. error: function (response) {
  758. {$this->errorScript()}
  759. }
  760. });
  761. JS
  762. );
  763. }
  764. /**
  765. * Generate a Field object and add to form builder if Field exists.
  766. *
  767. * @param string $method
  768. * @param array $arguments
  769. *
  770. * @return Field|null
  771. */
  772. public function __call($method, $arguments)
  773. {
  774. if ($className = static::findFieldClass($method)) {
  775. $name = Arr::get($arguments, 0, '');
  776. $element = new $className($name, array_slice($arguments, 1));
  777. $this->pushField($element);
  778. return $element;
  779. }
  780. if (static::hasMacro($method)) {
  781. return $this->macroCall($method, $arguments);
  782. }
  783. throw new RuntimeException("Field [{$method}] does not exist.");
  784. }
  785. /**
  786. * Output as string.
  787. *
  788. * @return string
  789. */
  790. public function __toString()
  791. {
  792. return $this->render();
  793. }
  794. /**
  795. * @param mixed ...$params
  796. *
  797. * @return $this
  798. */
  799. public static function make(...$params)
  800. {
  801. return new static(...$params);
  802. }
  803. }