Form.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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()
  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. /**
  388. * Disable Pjax.
  389. *
  390. * @return $this
  391. */
  392. public function disablePjax()
  393. {
  394. $this->forgetHtmlAttribute('pjax-container');
  395. return $this;
  396. }
  397. public function useFormTag(bool $tag = true)
  398. {
  399. $this->useFormTag = $tag;
  400. return $this;
  401. }
  402. /**
  403. * @param bool $value
  404. *
  405. * @return $this
  406. */
  407. public function submitButton(bool $value = true)
  408. {
  409. $this->buttons['submit'] = $value;
  410. return $this;
  411. }
  412. /**
  413. * @param bool $value
  414. *
  415. * @return $this
  416. */
  417. public function resetButton(bool $value = true)
  418. {
  419. $this->buttons['reset'] = $value;
  420. return $this;
  421. }
  422. /**
  423. * Disable reset button.
  424. *
  425. * @param bool $value
  426. *
  427. * @return $this
  428. *
  429. * @deprecated 即将废弃,请使用 resetButton 代替
  430. */
  431. public function disableResetButton(bool $value = true)
  432. {
  433. $this->buttons['reset'] = ! $value;
  434. return $this;
  435. }
  436. /**
  437. * Disable submit button.
  438. *
  439. * @param bool $value
  440. *
  441. * @return $this
  442. *
  443. * @deprecated 即将废弃,请使用 submitButton 代替
  444. */
  445. public function disableSubmitButton(bool $value = true)
  446. {
  447. $this->buttons['submit'] = ! $value;
  448. return $this;
  449. }
  450. /**
  451. * Set field and label width in current form.
  452. *
  453. * @param int $fieldWidth
  454. * @param int $labelWidth
  455. *
  456. * @return $this
  457. */
  458. public function width($fieldWidth = 8, $labelWidth = 2)
  459. {
  460. $this->width = [
  461. 'label' => $labelWidth,
  462. 'field' => $fieldWidth,
  463. ];
  464. $this->fields->each(function ($field) use ($fieldWidth, $labelWidth) {
  465. /* @var Field $field */
  466. $field->width($fieldWidth, $labelWidth);
  467. });
  468. return $this;
  469. }
  470. /**
  471. * Find field class with given name.
  472. *
  473. * @param string $method
  474. *
  475. * @return bool|string
  476. */
  477. public static function findFieldClass($method)
  478. {
  479. $class = Arr::get(\Dcat\Admin\Form::extensions(), $method);
  480. if (class_exists($class)) {
  481. return $class;
  482. }
  483. return false;
  484. }
  485. /**
  486. * Add a form field to form.
  487. *
  488. * @param Field $field
  489. *
  490. * @return $this
  491. */
  492. public function pushField(Field $field)
  493. {
  494. $this->fields->push($field);
  495. if ($this->layout()->hasColumns()) {
  496. $this->layout()->addField($field);
  497. }
  498. $field->setForm($this);
  499. $field->width($this->width['field'], $this->width['label']);
  500. $this->setFileUploadUrl($field);
  501. $field::requireAssets();
  502. return $this;
  503. }
  504. protected function setFileUploadUrl(Field $field)
  505. {
  506. if ($field instanceof Field\File && method_exists($this, 'form')) {
  507. $formData = [static::REQUEST_NAME => get_called_class()];
  508. $field->url(route(admin_api_route('form.upload')));
  509. $field->deleteUrl(route(admin_api_route('form.destroy-file'), $formData));
  510. $field->withFormData($formData);
  511. }
  512. }
  513. /**
  514. * Get variables for render form.
  515. *
  516. * @return array
  517. */
  518. protected function variables()
  519. {
  520. $this->setHtmlAttribute('id', $this->getElementId());
  521. $this->fillFields($this->model()->toArray());
  522. return array_merge([
  523. 'start' => $this->open(),
  524. 'end' => $this->close(),
  525. 'fields' => $this->fields,
  526. 'method' => $this->getHtmlAttribute('method'),
  527. 'rows' => $this->rows(),
  528. 'layout' => $this->layout(),
  529. 'elementId' => $this->getElementId(),
  530. 'ajax' => $this->ajax,
  531. 'footer' => $this->renderFooter(),
  532. ], $this->variables);
  533. }
  534. /**
  535. * 表单底部内容.
  536. *
  537. * @return string
  538. */
  539. protected function renderFooter()
  540. {
  541. if (empty($this->buttons['reset']) && empty($this->buttons['submit'])) {
  542. return;
  543. }
  544. $buttons = '';
  545. if (! empty($this->buttons['reset'])) {
  546. $reset = trans('admin.reset');
  547. $buttons .= "<button type=\"reset\" class=\"btn btn-white pull-left\"><i class=\"feather icon-rotate-ccw\"></i> {$reset}</button>";
  548. }
  549. if (! empty($this->buttons['submit'])) {
  550. $submit = $this->getSubmitButtonLabel();
  551. $buttons .= "<button type=\"submit\" class=\"btn btn-primary pull-right\"><i class=\"feather icon-save\"></i> {$submit}</button>";
  552. }
  553. return <<<HTML
  554. <div class="box-footer row d-flex">
  555. <div class="col-md-2"> &nbsp;</div>
  556. <div class="col-md-8">{$buttons}</div>
  557. </div>
  558. HTML;
  559. }
  560. /**
  561. * 提交按钮文本.
  562. *
  563. * @return string
  564. */
  565. protected function getSubmitButtonLabel()
  566. {
  567. return trans('admin.submit');
  568. }
  569. /**
  570. * 设置视图变量.
  571. *
  572. * @param array $variables
  573. *
  574. * @return $this
  575. */
  576. public function addVariables(array $variables)
  577. {
  578. $this->variables = array_merge($this->variables, $variables);
  579. return $this;
  580. }
  581. public function fillFields(array $data)
  582. {
  583. foreach ($this->fields as $field) {
  584. if (! $field->hasAttribute(Field::BUILD_IGNORE)) {
  585. $field->fill($data);
  586. }
  587. }
  588. }
  589. /**
  590. * @return string
  591. */
  592. protected function open()
  593. {
  594. if (! $this->useFormTag) {
  595. return;
  596. }
  597. return <<<HTML
  598. <form {$this->formatHtmlAttributes()}>
  599. HTML;
  600. }
  601. /**
  602. * @return string
  603. */
  604. protected function close()
  605. {
  606. if (! $this->useFormTag) {
  607. return;
  608. }
  609. return '</form>';
  610. }
  611. /**
  612. * Determine if form fields has files.
  613. *
  614. * @return bool
  615. */
  616. public function hasFile()
  617. {
  618. foreach ($this->fields as $field) {
  619. if ($field instanceof Field\File) {
  620. return true;
  621. }
  622. }
  623. return false;
  624. }
  625. /**
  626. * @param $id
  627. *
  628. * @return $this
  629. */
  630. public function setFormId($id)
  631. {
  632. $this->elementId = $id;
  633. return $this;
  634. }
  635. /**
  636. * @return string
  637. */
  638. public function getElementId()
  639. {
  640. return $this->elementId ?: ($this->elementId = 'form-'.Str::random(8));
  641. }
  642. /**
  643. * {@inheritdoc}
  644. */
  645. public function setCurrentUrl($url)
  646. {
  647. if ($this instanceof LazyRenderable) {
  648. $this->payload([static::CURRENT_URL_NAME => $url]);
  649. }
  650. return $this->defaultSetCurrentUrl($url);
  651. }
  652. /**
  653. * @param bool $disable
  654. *
  655. * @return $this
  656. */
  657. public function ajax(bool $value = true)
  658. {
  659. $this->ajax = $value;
  660. return $this;
  661. }
  662. /**
  663. * @return bool
  664. */
  665. public function allowAjaxSubmit()
  666. {
  667. return $this->ajax === true;
  668. }
  669. /**
  670. * @return string|void
  671. */
  672. protected function savedScript()
  673. {
  674. }
  675. /**
  676. * @return string|void
  677. */
  678. protected function errorScript()
  679. {
  680. }
  681. /**
  682. * @param array $input
  683. *
  684. * @return array
  685. */
  686. public function sanitize(array $input)
  687. {
  688. Arr::forget($input, [static::REQUEST_NAME, '_token', static::CURRENT_URL_NAME]);
  689. return $this->prepareInput($input);
  690. }
  691. public function prepareInput(array $input)
  692. {
  693. Helper::prepareHasOneRelation($this->fields, $input);
  694. foreach ($input as $column => $value) {
  695. $field = $this->field($column);
  696. if (! $field instanceof Field) {
  697. unset($input[$column]);
  698. continue;
  699. }
  700. $input[$column] = $field->prepare($value);
  701. }
  702. $prepared = [];
  703. foreach ($input as $key => $value) {
  704. Arr::set($prepared, $key, $value);
  705. }
  706. return $prepared;
  707. }
  708. protected function prepareForm()
  709. {
  710. if (method_exists($this, 'form')) {
  711. $this->form();
  712. }
  713. if (! $this->data && method_exists($this, 'default')) {
  714. $data = $this->default();
  715. if (is_array($data)) {
  716. $this->fill($data);
  717. }
  718. }
  719. }
  720. protected function prepareHandler()
  721. {
  722. if (method_exists($this, 'handle')) {
  723. $addHiddenFields = function () {
  724. $this->method('POST');
  725. $this->action(route(admin_api_route('form')));
  726. $this->hidden(static::REQUEST_NAME)->default(get_called_class());
  727. $this->hidden(static::CURRENT_URL_NAME)->default($this->getCurrentUrl());
  728. if (! empty($this->payload) && is_array($this->payload)) {
  729. $this->hidden(static::LAZY_PAYLOAD_NAME)->default(json_encode($this->payload));
  730. }
  731. };
  732. $this->layout()->hasColumns() ? $this->column(1, $addHiddenFields) : $addHiddenFields();
  733. }
  734. }
  735. /**
  736. * Render the form.
  737. *
  738. * @return string
  739. */
  740. public function render()
  741. {
  742. $this->prepareForm();
  743. $this->prepareHandler();
  744. if ($this->allowAjaxSubmit()) {
  745. $this->addAjaxScript();
  746. }
  747. $tabObj = $this->getTab();
  748. if (! $tabObj->isEmpty()) {
  749. $tabObj->addScript();
  750. }
  751. $this->addVariables([
  752. 'tabObj' => $tabObj,
  753. ]);
  754. return view($this->view, $this->variables())->render();
  755. }
  756. protected function addAjaxScript()
  757. {
  758. $confirm = admin_javascript_json($this->confirm);
  759. $toastr = $this->validationErrorToastr ? 'true' : 'false';
  760. Admin::script(
  761. <<<JS
  762. $('#{$this->getElementId()}').form({
  763. validate: true,
  764. confirm: {$confirm},
  765. validationErrorToastr: $toastr,
  766. success: function (data) {
  767. {$this->savedScript()}
  768. },
  769. error: function (response) {
  770. {$this->errorScript()}
  771. }
  772. });
  773. JS
  774. );
  775. }
  776. /**
  777. * Generate a Field object and add to form builder if Field exists.
  778. *
  779. * @param string $method
  780. * @param array $arguments
  781. *
  782. * @return Field|null
  783. */
  784. public function __call($method, $arguments)
  785. {
  786. if ($className = static::findFieldClass($method)) {
  787. $name = Arr::get($arguments, 0, '');
  788. $element = new $className($name, array_slice($arguments, 1));
  789. $this->pushField($element);
  790. return $element;
  791. }
  792. if (static::hasMacro($method)) {
  793. return $this->macroCall($method, $arguments);
  794. }
  795. throw new RuntimeException("Field [{$method}] does not exist.");
  796. }
  797. /**
  798. * Output as string.
  799. *
  800. * @return string
  801. */
  802. public function __toString()
  803. {
  804. return $this->render();
  805. }
  806. /**
  807. * @param mixed ...$params
  808. *
  809. * @return $this
  810. */
  811. public static function make(...$params)
  812. {
  813. return new static(...$params);
  814. }
  815. }