Form.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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. * @return Fluent|\Illuminate\Database\Eloquent\Model
  279. */
  280. public function data()
  281. {
  282. if (! $this->data) {
  283. $this->fill([]);
  284. }
  285. return $this->data;
  286. }
  287. /**
  288. * @param array|Arrayable|Closure $data
  289. *
  290. * @return $this
  291. */
  292. public function fill($data)
  293. {
  294. if ($data instanceof \Closure) {
  295. $data = $data($this);
  296. }
  297. if (is_array($data)) {
  298. $this->data = new Fluent($data);
  299. } elseif ($data instanceof Arrayable) {
  300. $this->data = $data;
  301. }
  302. return $this;
  303. }
  304. /**
  305. * @return Fluent|\Illuminate\Database\Eloquent\Model
  306. */
  307. public function model()
  308. {
  309. return $this->data();
  310. }
  311. /**
  312. * Add a fieldset to form.
  313. *
  314. * @param string $title
  315. * @param Closure $setCallback
  316. *
  317. * @return Field\Fieldset
  318. */
  319. public function fieldset(string $title, Closure $setCallback)
  320. {
  321. $fieldset = new Field\Fieldset();
  322. $this->html($fieldset->start($title))->plain();
  323. $setCallback($this);
  324. $this->html($fieldset->end())->plain();
  325. return $fieldset;
  326. }
  327. /**
  328. * Get specify field.
  329. *
  330. * @param string|Field $name
  331. *
  332. * @return Field|null
  333. */
  334. public function field($name)
  335. {
  336. foreach ($this->fields as $field) {
  337. if (is_array($field->column())) {
  338. $result = in_array($name, $field->column(), true) || $field->column() === $name ? $field : null;
  339. if ($result) {
  340. return $result;
  341. }
  342. }
  343. if ($field === $name || $field->column() === $name) {
  344. return $field;
  345. }
  346. }
  347. }
  348. /**
  349. * @return Field[]|Collection
  350. */
  351. public function fields()
  352. {
  353. return $this->fields;
  354. }
  355. /**
  356. * Validate this form fields.
  357. *
  358. * @param Request $request
  359. *
  360. * @return bool|MessageBag
  361. */
  362. public function validate(Request $request)
  363. {
  364. $failedValidators = [];
  365. /** @var \Dcat\Admin\Form\Field $field */
  366. foreach ($this->fields() as $field) {
  367. if (! $validator = $field->getValidator($request->all())) {
  368. continue;
  369. }
  370. if (($validator instanceof Validator) && ! $validator->passes()) {
  371. $failedValidators[] = $validator;
  372. }
  373. }
  374. $message = $this->mergeValidationMessages($failedValidators);
  375. return $message->any() ? $message : false;
  376. }
  377. /**
  378. * Merge validation messages from input validators.
  379. *
  380. * @param \Illuminate\Validation\Validator[] $validators
  381. *
  382. * @return MessageBag
  383. */
  384. protected function mergeValidationMessages($validators)
  385. {
  386. $messageBag = new MessageBag();
  387. foreach ($validators as $validator) {
  388. $messageBag = $messageBag->merge($validator->messages());
  389. }
  390. return $messageBag;
  391. }
  392. public function useFormTag(bool $tag = true)
  393. {
  394. $this->useFormTag = $tag;
  395. return $this;
  396. }
  397. /**
  398. * @param bool $value
  399. *
  400. * @return $this
  401. */
  402. public function submitButton(bool $value = true)
  403. {
  404. $this->buttons['submit'] = $value;
  405. return $this;
  406. }
  407. /**
  408. * @param bool $value
  409. *
  410. * @return $this
  411. */
  412. public function resetButton(bool $value = true)
  413. {
  414. $this->buttons['reset'] = $value;
  415. return $this;
  416. }
  417. /**
  418. * Disable reset button.
  419. *
  420. * @param bool $value
  421. *
  422. * @return $this
  423. */
  424. public function disableResetButton(bool $value = true)
  425. {
  426. return $this->resetButton(! $value);
  427. }
  428. /**
  429. * Disable submit button.
  430. *
  431. * @param bool $value
  432. *
  433. * @return $this
  434. */
  435. public function disableSubmitButton(bool $value = true)
  436. {
  437. return $this->submitButton(! $value);
  438. }
  439. /**
  440. * Set field and label width in current form.
  441. *
  442. * @param int $fieldWidth
  443. * @param int $labelWidth
  444. *
  445. * @return $this
  446. */
  447. public function width($fieldWidth = 8, $labelWidth = 2)
  448. {
  449. $this->width = [
  450. 'label' => $labelWidth,
  451. 'field' => $fieldWidth,
  452. ];
  453. $this->fields->each(function ($field) use ($fieldWidth, $labelWidth) {
  454. /* @var Field $field */
  455. $field->width($fieldWidth, $labelWidth);
  456. });
  457. return $this;
  458. }
  459. /**
  460. * Find field class with given name.
  461. *
  462. * @param string $method
  463. *
  464. * @return bool|string
  465. */
  466. public static function findFieldClass($method)
  467. {
  468. $class = Arr::get(\Dcat\Admin\Form::extensions(), $method);
  469. if (class_exists($class)) {
  470. return $class;
  471. }
  472. return false;
  473. }
  474. /**
  475. * Add a form field to form.
  476. *
  477. * @param Field $field
  478. *
  479. * @return $this
  480. */
  481. public function pushField(Field $field)
  482. {
  483. $this->fields->push($field);
  484. if ($this->layout()->hasColumns()) {
  485. $this->layout()->addField($field);
  486. }
  487. $field->setForm($this);
  488. $field->width($this->width['field'], $this->width['label']);
  489. $this->setFileUploadUrl($field);
  490. $field::requireAssets();
  491. return $this;
  492. }
  493. protected function setFileUploadUrl(Field $field)
  494. {
  495. if ($field instanceof Field\File && method_exists($this, 'form')) {
  496. $formData = [static::REQUEST_NAME => get_called_class()];
  497. $field->url(route(admin_api_route_name('form.upload')));
  498. $field->deleteUrl(route(admin_api_route_name('form.destroy-file'), $formData));
  499. $field->withFormData($formData);
  500. }
  501. }
  502. /**
  503. * Get variables for render form.
  504. *
  505. * @return array
  506. */
  507. protected function variables()
  508. {
  509. $this->setHtmlAttribute('id', $this->getElementId());
  510. $this->fillFields($this->model()->toArray());
  511. return array_merge([
  512. 'start' => $this->open(),
  513. 'end' => $this->close(),
  514. 'fields' => $this->fields,
  515. 'method' => $this->getHtmlAttribute('method'),
  516. 'rows' => $this->rows(),
  517. 'layout' => $this->layout(),
  518. 'elementId' => $this->getElementId(),
  519. 'ajax' => $this->ajax,
  520. 'footer' => $this->renderFooter(),
  521. ], $this->variables);
  522. }
  523. /**
  524. * 表单底部内容.
  525. *
  526. * @return string
  527. */
  528. protected function renderFooter()
  529. {
  530. if (empty($this->buttons['reset']) && empty($this->buttons['submit'])) {
  531. return;
  532. }
  533. return <<<HTML
  534. <div class="box-footer row d-flex">
  535. <div class="col-md-2"> &nbsp;</div>
  536. <div class="col-md-8">{$this->renderResetButton()}{$this->renderSubmitButton()}</div>
  537. </div>
  538. HTML;
  539. }
  540. protected function renderResetButton()
  541. {
  542. if (! empty($this->buttons['reset'])) {
  543. $reset = trans('admin.reset');
  544. return "<button type=\"reset\" class=\"btn btn-white pull-left\"><i class=\"feather icon-rotate-ccw\"></i> {$reset}</button>";
  545. }
  546. }
  547. protected function renderSubmitButton()
  548. {
  549. if (! empty($this->buttons['submit'])) {
  550. return "<button type=\"submit\" class=\"btn btn-primary pull-right\"><i class=\"feather icon-save\"></i> {$this->getSubmitButtonLabel()}</button>";
  551. }
  552. }
  553. /**
  554. * 提交按钮文本.
  555. *
  556. * @return string
  557. */
  558. protected function getSubmitButtonLabel()
  559. {
  560. return trans('admin.submit');
  561. }
  562. /**
  563. * 设置视图变量.
  564. *
  565. * @param array $variables
  566. *
  567. * @return $this
  568. */
  569. public function addVariables(array $variables)
  570. {
  571. $this->variables = array_merge($this->variables, $variables);
  572. return $this;
  573. }
  574. public function fillFields(array $data)
  575. {
  576. foreach ($this->fields as $field) {
  577. if (! $field->hasAttribute(Field::BUILD_IGNORE)) {
  578. $field->fill($data);
  579. }
  580. }
  581. }
  582. /**
  583. * @return string
  584. */
  585. protected function open()
  586. {
  587. if (! $this->useFormTag) {
  588. return;
  589. }
  590. return <<<HTML
  591. <form {$this->formatHtmlAttributes()}>
  592. HTML;
  593. }
  594. /**
  595. * @return string
  596. */
  597. protected function close()
  598. {
  599. if (! $this->useFormTag) {
  600. return;
  601. }
  602. return '</form>';
  603. }
  604. /**
  605. * Determine if form fields has files.
  606. *
  607. * @return bool
  608. */
  609. public function hasFile()
  610. {
  611. foreach ($this->fields as $field) {
  612. if ($field instanceof Field\File) {
  613. return true;
  614. }
  615. }
  616. return false;
  617. }
  618. /**
  619. * @param $id
  620. *
  621. * @return $this
  622. */
  623. public function setFormId($id)
  624. {
  625. $this->elementId = $id;
  626. return $this;
  627. }
  628. /**
  629. * @return string
  630. */
  631. public function getElementId()
  632. {
  633. return $this->elementId ?: ($this->elementId = 'form-'.Str::random(8));
  634. }
  635. /**
  636. * {@inheritdoc}
  637. */
  638. public function setCurrentUrl($url)
  639. {
  640. if ($this instanceof LazyRenderable) {
  641. $this->payload([static::CURRENT_URL_NAME => $url]);
  642. }
  643. return $this->defaultSetCurrentUrl($url);
  644. }
  645. /**
  646. * @param bool $disable
  647. *
  648. * @return $this
  649. */
  650. public function ajax(bool $value = true)
  651. {
  652. $this->ajax = $value;
  653. return $this;
  654. }
  655. /**
  656. * @return bool
  657. */
  658. public function allowAjaxSubmit()
  659. {
  660. return $this->ajax === true;
  661. }
  662. /**
  663. * @return string|void
  664. */
  665. protected function savedScript()
  666. {
  667. }
  668. /**
  669. * @return string|void
  670. */
  671. protected function errorScript()
  672. {
  673. }
  674. /**
  675. * @param array $input
  676. *
  677. * @return array
  678. */
  679. public function sanitize(array $input)
  680. {
  681. Arr::forget($input, [static::REQUEST_NAME, '_token', static::CURRENT_URL_NAME]);
  682. return $this->prepareInput($input);
  683. }
  684. public function prepareInput(array $input)
  685. {
  686. Helper::prepareHasOneRelation($this->fields, $input);
  687. foreach ($input as $column => $value) {
  688. $field = $this->field($column);
  689. if (! $field instanceof Field) {
  690. unset($input[$column]);
  691. continue;
  692. }
  693. $input[$column] = $field->prepare($value);
  694. }
  695. $prepared = [];
  696. foreach ($input as $key => $value) {
  697. Arr::set($prepared, $key, $value);
  698. }
  699. return $prepared;
  700. }
  701. protected function prepareForm()
  702. {
  703. if (method_exists($this, 'form')) {
  704. $this->form();
  705. }
  706. if (! $this->data && method_exists($this, 'default')) {
  707. $data = $this->default();
  708. if (is_array($data)) {
  709. $this->fill($data);
  710. }
  711. }
  712. }
  713. protected function prepareHandler()
  714. {
  715. if ($this->allowAjaxSubmit() && method_exists($this, 'handle')) {
  716. $addHiddenFields = function () {
  717. $this->method('POST');
  718. $this->action(route(admin_api_route_name('form')));
  719. $this->hidden(static::REQUEST_NAME)->default(get_called_class());
  720. $this->hidden(static::CURRENT_URL_NAME)->default($this->getCurrentUrl());
  721. if (! empty($this->payload) && is_array($this->payload)) {
  722. $this->hidden(static::LAZY_PAYLOAD_NAME)->default(json_encode($this->payload));
  723. }
  724. };
  725. $this->layout()->hasColumns() ? $this->column(1, $addHiddenFields) : $addHiddenFields();
  726. }
  727. }
  728. /**
  729. * Render the form.
  730. *
  731. * @return string
  732. */
  733. public function render()
  734. {
  735. $this->prepareForm();
  736. $this->prepareHandler();
  737. if ($this->allowAjaxSubmit()) {
  738. $this->addAjaxScript();
  739. }
  740. $tabObj = $this->getTab();
  741. if (! $tabObj->isEmpty()) {
  742. $tabObj->addScript();
  743. }
  744. $this->addVariables([
  745. 'tabObj' => $tabObj,
  746. ]);
  747. return view($this->view, $this->variables())->render();
  748. }
  749. protected function addAjaxScript()
  750. {
  751. $confirm = admin_javascript_json($this->confirm);
  752. $toastr = $this->validationErrorToastr ? 'true' : 'false';
  753. Admin::script(
  754. <<<JS
  755. $('#{$this->getElementId()}').form({
  756. validate: true,
  757. confirm: {$confirm},
  758. validationErrorToastr: $toastr,
  759. success: function (data) {
  760. {$this->savedScript()}
  761. },
  762. error: function (response) {
  763. {$this->errorScript()}
  764. }
  765. });
  766. JS
  767. );
  768. }
  769. /**
  770. * Generate a Field object and add to form builder if Field exists.
  771. *
  772. * @param string $method
  773. * @param array $arguments
  774. *
  775. * @return Field|null
  776. */
  777. public function __call($method, $arguments)
  778. {
  779. if ($className = static::findFieldClass($method)) {
  780. $name = Arr::get($arguments, 0, '');
  781. $element = new $className($name, array_slice($arguments, 1));
  782. $this->pushField($element);
  783. return $element;
  784. }
  785. if (static::hasMacro($method)) {
  786. return $this->macroCall($method, $arguments);
  787. }
  788. throw new RuntimeException("Field [{$method}] does not exist.");
  789. }
  790. /**
  791. * Output as string.
  792. *
  793. * @return string
  794. */
  795. public function __toString()
  796. {
  797. return $this->render();
  798. }
  799. /**
  800. * @param mixed ...$params
  801. *
  802. * @return $this
  803. */
  804. public static function make(...$params)
  805. {
  806. return new static(...$params);
  807. }
  808. }