Form.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Closure;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Form\Concerns\HandleCascadeFields;
  6. use Dcat\Admin\Form\Concerns\HasRows;
  7. use Dcat\Admin\Form\Concerns\HasTabs;
  8. use Dcat\Admin\Form\Field;
  9. use Dcat\Admin\Form\Layout;
  10. use Dcat\Admin\Support\Helper;
  11. use Dcat\Admin\Traits\HasAuthorization;
  12. use Dcat\Admin\Traits\HasFormResponse;
  13. use Dcat\Admin\Traits\HasHtmlAttributes;
  14. use Illuminate\Contracts\Support\Arrayable;
  15. use Illuminate\Contracts\Support\Renderable;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Arr;
  18. use Illuminate\Support\Collection;
  19. use Illuminate\Support\Fluent;
  20. use Illuminate\Support\MessageBag;
  21. use Illuminate\Support\Str;
  22. use Illuminate\Support\Traits\Macroable;
  23. use Illuminate\Validation\Validator;
  24. /**
  25. * Class Form.
  26. *
  27. * @method Field\Text text($column, $label = '')
  28. * @method Field\Checkbox checkbox($column, $label = '')
  29. * @method Field\Radio radio($column, $label = '')
  30. * @method Field\Select select($column, $label = '')
  31. * @method Field\MultipleSelect multipleSelect($column, $label = '')
  32. * @method Field\Textarea textarea($column, $label = '')
  33. * @method Field\Hidden hidden($column, $label = '')
  34. * @method Field\Id id($column, $label = '')
  35. * @method Field\Ip ip($column, $label = '')
  36. * @method Field\Url url($column, $label = '')
  37. * @method Field\Email email($column, $label = '')
  38. * @method Field\Mobile mobile($column, $label = '')
  39. * @method Field\Slider slider($column, $label = '')
  40. * @method Field\Map map($latitude, $longitude, $label = '')
  41. * @method Field\Editor editor($column, $label = '')
  42. * @method Field\Date date($column, $label = '')
  43. * @method Field\Datetime datetime($column, $label = '')
  44. * @method Field\Time time($column, $label = '')
  45. * @method Field\Year year($column, $label = '')
  46. * @method Field\Month month($column, $label = '')
  47. * @method Field\DateRange dateRange($start, $end, $label = '')
  48. * @method Field\DateTimeRange datetimeRange($start, $end, $label = '')
  49. * @method Field\TimeRange timeRange($start, $end, $label = '')
  50. * @method Field\Number number($column, $label = '')
  51. * @method Field\Currency currency($column, $label = '')
  52. * @method Field\SwitchField switch($column, $label = '')
  53. * @method Field\Display display($column, $label = '')
  54. * @method Field\Rate rate($column, $label = '')
  55. * @method Field\Divide divider()
  56. * @method Field\Password password($column, $label = '')
  57. * @method Field\Decimal decimal($column, $label = '')
  58. * @method Field\Html html($html, $label = '')
  59. * @method Field\Tags tags($column, $label = '')
  60. * @method Field\Icon icon($column, $label = '')
  61. * @method Field\Embeds embeds($column, $label = '')
  62. * @method Field\Captcha captcha($column, $label = '')
  63. * @method Field\Listbox listbox($column, $label = '')
  64. * @method Field\SelectResource selectResource($column, $label = '')
  65. * @method Field\File file($column, $label = '')
  66. * @method Field\Image image($column, $label = '')
  67. * @method Field\MultipleFile multipleFile($column, $label = '')
  68. * @method Field\MultipleImage multipleImage($column, $label = '')
  69. * @method Field\HasMany hasMany($column, \Closure $callback)
  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. */
  79. class Form implements Renderable
  80. {
  81. use HasHtmlAttributes,
  82. HasFormResponse,
  83. HasAuthorization,
  84. HandleCascadeFields,
  85. HasRows,
  86. HasTabs,
  87. Macroable {
  88. __call as macroCall;
  89. }
  90. const REQUEST_NAME = '_form_';
  91. /**
  92. * @var string
  93. */
  94. protected $view = 'admin::widgets.form';
  95. /**
  96. * @var Field[]|Collection
  97. */
  98. protected $fields;
  99. /**
  100. * @var Layout
  101. */
  102. protected $layout;
  103. /**
  104. * @var array
  105. */
  106. protected $variables = [];
  107. /**
  108. * @var bool
  109. */
  110. protected $useAjaxSubmit = true;
  111. /**
  112. * @var Fluent
  113. */
  114. protected $data;
  115. /**
  116. * @var mixed
  117. */
  118. protected $primaryKey;
  119. /**
  120. * Available buttons.
  121. *
  122. * @var array
  123. */
  124. protected $buttons = ['reset' => true, 'submit' => true];
  125. /**
  126. * @var bool
  127. */
  128. protected $useFormTag = true;
  129. /**
  130. * @var string
  131. */
  132. protected $elementId;
  133. /**
  134. * @var array
  135. */
  136. protected $width = [
  137. 'label' => 2,
  138. 'field' => 8,
  139. ];
  140. /**
  141. * Form constructor.
  142. *
  143. * @param array $data
  144. * @param mixed $key
  145. */
  146. public function __construct($data = [], $key = null)
  147. {
  148. if ($data) {
  149. $this->fill($data);
  150. }
  151. $this->setKey($key);
  152. $this->initFields();
  153. $this->initFormAttributes();
  154. }
  155. /**
  156. * Initialize the form fields.
  157. */
  158. protected function initFields()
  159. {
  160. $this->fields = new Collection();
  161. }
  162. /**
  163. * Initialize the form attributes.
  164. */
  165. protected function initFormAttributes()
  166. {
  167. $this->setHtmlAttribute([
  168. 'method' => 'POST',
  169. 'action' => '',
  170. 'class' => 'form-horizontal',
  171. 'accept-charset' => 'UTF-8',
  172. 'pjax-container' => true,
  173. ]);
  174. }
  175. /**
  176. * Action uri of the form.
  177. *
  178. * @param string $action
  179. *
  180. * @return $this|string
  181. */
  182. public function action($action = null)
  183. {
  184. if ($action === null) {
  185. return $this->getHtmlAttribute('action');
  186. }
  187. return $this->setHtmlAttribute('action', admin_url($action));
  188. }
  189. /**
  190. * Method of the form.
  191. *
  192. * @param string $method
  193. *
  194. * @return $this
  195. */
  196. public function method($method = 'POST')
  197. {
  198. return $this->setHtmlAttribute('method', strtoupper($method));
  199. }
  200. /**
  201. * Set primary key.
  202. *
  203. * @param mixed $value
  204. *
  205. * @return $this
  206. */
  207. public function setKey($value)
  208. {
  209. $this->primaryKey = $value;
  210. return $this;
  211. }
  212. /**
  213. * Get primary key.
  214. *
  215. * @return mixed
  216. */
  217. public function getKey()
  218. {
  219. return $this->primaryKey;
  220. }
  221. /**
  222. * @param array|Arrayable|Closure $data
  223. *
  224. * @return Fluent
  225. */
  226. public function data()
  227. {
  228. if (! $this->data) {
  229. $this->fill([]);
  230. }
  231. return $this->data;
  232. }
  233. /**
  234. * @param array|Arrayable|Closure $data
  235. *
  236. * @return $this
  237. */
  238. public function fill($data)
  239. {
  240. $this->data = new Fluent(Helper::array($data));
  241. return $this;
  242. }
  243. /**
  244. * @return Fluent
  245. */
  246. public function model()
  247. {
  248. return $this->data();
  249. }
  250. /**
  251. * Add a fieldset to form.
  252. *
  253. * @param string $title
  254. * @param Closure $setCallback
  255. *
  256. * @return Field\Fieldset
  257. */
  258. public function fieldset(string $title, Closure $setCallback)
  259. {
  260. $fieldset = new Field\Fieldset();
  261. $this->html($fieldset->start($title))->plain();
  262. $setCallback($this);
  263. $this->html($fieldset->end())->plain();
  264. return $fieldset;
  265. }
  266. /**
  267. * Get specify field.
  268. *
  269. * @param string|Field $name
  270. *
  271. * @return Field|null
  272. */
  273. public function field($name)
  274. {
  275. foreach ($this->fields as $field) {
  276. if ($field === $name || $field->column() === $name) {
  277. return $field;
  278. }
  279. }
  280. }
  281. /**
  282. * @return Field[]|Collection
  283. */
  284. public function fields()
  285. {
  286. return $this->fields;
  287. }
  288. /**
  289. * @param int|float $width
  290. * @param Closure $callback
  291. *
  292. * @return $this
  293. */
  294. public function column($width, \Closure $callback)
  295. {
  296. $this->layout()->onlyColumn($width, function () use ($callback) {
  297. $callback($this);
  298. });
  299. return $this;
  300. }
  301. /**
  302. * @return Layout
  303. */
  304. public function layout()
  305. {
  306. return $this->layout ?: ($this->layout = new Layout($this));
  307. }
  308. /**
  309. * Validate this form fields.
  310. *
  311. * @param Request $request
  312. *
  313. * @return bool|MessageBag
  314. */
  315. public function validate(Request $request)
  316. {
  317. if (method_exists($this, 'form')) {
  318. $this->form();
  319. }
  320. $failedValidators = [];
  321. /** @var \Dcat\Admin\Form\Field $field */
  322. foreach ($this->fields() as $field) {
  323. if (! $validator = $field->getValidator($request->all())) {
  324. continue;
  325. }
  326. if (($validator instanceof Validator) && ! $validator->passes()) {
  327. $failedValidators[] = $validator;
  328. }
  329. }
  330. $message = $this->mergeValidationMessages($failedValidators);
  331. return $message->any() ? $message : false;
  332. }
  333. /**
  334. * Merge validation messages from input validators.
  335. *
  336. * @param \Illuminate\Validation\Validator[] $validators
  337. *
  338. * @return MessageBag
  339. */
  340. protected function mergeValidationMessages($validators)
  341. {
  342. $messageBag = new MessageBag();
  343. foreach ($validators as $validator) {
  344. $messageBag = $messageBag->merge($validator->messages());
  345. }
  346. return $messageBag;
  347. }
  348. /**
  349. * Disable Pjax.
  350. *
  351. * @return $this
  352. */
  353. public function disablePjax()
  354. {
  355. $this->forgetHtmlAttribute('pjax-container');
  356. return $this;
  357. }
  358. /**
  359. * Disable form tag.
  360. *
  361. * @return $this;
  362. */
  363. public function disableFormTag()
  364. {
  365. $this->useFormTag = false;
  366. return $this;
  367. }
  368. /**
  369. * Disable reset button.
  370. *
  371. * @return $this
  372. */
  373. public function disableResetButton()
  374. {
  375. $this->buttons['reset'] = false;
  376. return $this;
  377. }
  378. /**
  379. * Disable submit button.
  380. *
  381. * @return $this
  382. */
  383. public function disableSubmitButton()
  384. {
  385. $this->buttons['submit'] = false;
  386. return $this;
  387. }
  388. /**
  389. * Set field and label width in current form.
  390. *
  391. * @param int $fieldWidth
  392. * @param int $labelWidth
  393. *
  394. * @return $this
  395. */
  396. public function width($fieldWidth = 8, $labelWidth = 2)
  397. {
  398. $this->width = [
  399. 'label' => $labelWidth,
  400. 'field' => $fieldWidth,
  401. ];
  402. $this->fields->each(function ($field) use ($fieldWidth, $labelWidth) {
  403. /* @var Field $field */
  404. $field->width($fieldWidth, $labelWidth);
  405. });
  406. return $this;
  407. }
  408. /**
  409. * Find field class with given name.
  410. *
  411. * @param string $method
  412. *
  413. * @return bool|string
  414. */
  415. public static function findFieldClass($method)
  416. {
  417. $class = Arr::get(\Dcat\Admin\Form::extensions(), $method);
  418. if (class_exists($class)) {
  419. return $class;
  420. }
  421. return false;
  422. }
  423. /**
  424. * Add a form field to form.
  425. *
  426. * @param Field $field
  427. *
  428. * @return $this
  429. */
  430. public function pushField(Field $field)
  431. {
  432. $this->fields->push($field);
  433. if ($this->layout) {
  434. $this->layout->addField($field);
  435. }
  436. $field->setForm($this);
  437. $field->width($this->width['field'], $this->width['label']);
  438. if ($field instanceof Field\File) {
  439. $formData = [static::REQUEST_NAME => get_called_class()];
  440. $field->url(route(admin_api_route('form.upload')));
  441. $field->deleteUrl(route(admin_api_route('form.destroy-file'), $formData));
  442. $field->withFormData($formData);
  443. }
  444. $field::collectAssets();
  445. return $this;
  446. }
  447. /**
  448. * Get variables for render form.
  449. *
  450. * @return array
  451. */
  452. protected function variables()
  453. {
  454. $this->setHtmlAttribute('id', $this->getElementId());
  455. $this->fillFields($this->model()->toArray());
  456. return array_merge([
  457. 'start' => $this->open(),
  458. 'end' => $this->close(),
  459. 'fields' => $this->fields,
  460. 'method' => $this->getHtmlAttribute('method'),
  461. 'buttons' => $this->buttons,
  462. 'rows' => $this->rows(),
  463. 'layout' => $this->layout,
  464. ], $this->variables);
  465. }
  466. public function addVariables(array $variables)
  467. {
  468. $this->variables = array_merge($this->variables, $variables);
  469. return $this;
  470. }
  471. public function fillFields(array $data)
  472. {
  473. foreach ($this->fields as $field) {
  474. $field->fill($data);
  475. }
  476. }
  477. /**
  478. * @return string
  479. */
  480. protected function open()
  481. {
  482. return <<<HTML
  483. <form {$this->formatHtmlAttributes()}>
  484. HTML;
  485. }
  486. /**
  487. * @return string
  488. */
  489. protected function close()
  490. {
  491. return '</form>';
  492. }
  493. /**
  494. * Determine if form fields has files.
  495. *
  496. * @return bool
  497. */
  498. public function hasFile()
  499. {
  500. foreach ($this->fields as $field) {
  501. if ($field instanceof Field\File) {
  502. return true;
  503. }
  504. }
  505. return false;
  506. }
  507. /**
  508. * @param $id
  509. *
  510. * @return $this
  511. */
  512. public function setFormId($id)
  513. {
  514. $this->elementId = $id;
  515. return $this;
  516. }
  517. /**
  518. * @return string
  519. */
  520. public function getElementId()
  521. {
  522. return $this->elementId ?: ($this->elementId = 'form-'.Str::random(8));
  523. }
  524. /**
  525. * Generate a Field object and add to form builder if Field exists.
  526. *
  527. * @param string $method
  528. * @param array $arguments
  529. *
  530. * @return Field|null
  531. */
  532. public function __call($method, $arguments)
  533. {
  534. if ($className = static::findFieldClass($method)) {
  535. $name = Arr::get($arguments, 0, '');
  536. $element = new $className($name, array_slice($arguments, 1));
  537. $this->pushField($element);
  538. return $element;
  539. }
  540. if (static::hasMacro($method)) {
  541. return $this->macroCall($method, $arguments);
  542. }
  543. throw new \BadMethodCallException("Field [{$method}] does not exist.");
  544. }
  545. /**
  546. * Disable submit with ajax.
  547. *
  548. * @param bool $disable
  549. *
  550. * @return $this
  551. */
  552. public function disableAjaxSubmit(bool $disable = true)
  553. {
  554. $this->useAjaxSubmit = ! $disable;
  555. return $this;
  556. }
  557. /**
  558. * @return bool
  559. */
  560. public function allowAjaxSubmit()
  561. {
  562. return $this->useAjaxSubmit === true;
  563. }
  564. /**
  565. * @return void
  566. */
  567. protected function setUpSubmitScript()
  568. {
  569. Admin::script(
  570. <<<JS
  571. $('#{$this->getElementId()}').form({
  572. validate: true,
  573. success: function (data) {
  574. {$this->buildSuccessScript()}
  575. {$this->addSavedScript()}
  576. },
  577. error: function (response) {
  578. {$this->buildErrorScript()}
  579. {$this->addErrorScript()}
  580. }
  581. });
  582. JS
  583. );
  584. }
  585. /**
  586. * @return string|void
  587. *
  588. * @deprecated 将在2.0版本中废弃,请用 addSavedScript 代替
  589. */
  590. protected function buildSuccessScript()
  591. {
  592. }
  593. /**
  594. * @return string|void
  595. */
  596. protected function addSavedScript()
  597. {
  598. }
  599. /**
  600. * @return string|void
  601. *
  602. * @deprecated 将在2.0版本中废弃,请用 addErrorScript 代替
  603. */
  604. protected function buildErrorScript()
  605. {
  606. }
  607. /**
  608. * @return string|void
  609. */
  610. protected function addErrorScript()
  611. {
  612. }
  613. /**
  614. * @param array $input
  615. *
  616. * @return array
  617. */
  618. public function sanitize(array $input)
  619. {
  620. Arr::forget($input, [static::REQUEST_NAME, '_token', '_current_']);
  621. return $input;
  622. }
  623. protected function prepareForm()
  624. {
  625. if (method_exists($this, 'form')) {
  626. $this->form();
  627. }
  628. if (! $this->data && method_exists($this, 'default')) {
  629. $data = $this->default();
  630. if (is_array($data)) {
  631. $this->fill($data);
  632. }
  633. }
  634. }
  635. protected function prepareHandler()
  636. {
  637. if (method_exists($this, 'handle')) {
  638. $this->method('POST');
  639. $this->action(route(admin_api_route('form')));
  640. $this->hidden(static::REQUEST_NAME)->default(get_called_class());
  641. $this->hidden('_current_')->default($this->getCurrentUrl());
  642. }
  643. }
  644. /**
  645. * Render the form.
  646. *
  647. * @return string
  648. */
  649. public function render()
  650. {
  651. $this->prepareForm();
  652. $this->prepareHandler();
  653. if ($this->allowAjaxSubmit()) {
  654. $this->setUpSubmitScript();
  655. }
  656. $tabObj = $this->getTab();
  657. if (! $tabObj->isEmpty()) {
  658. $tabObj->addScript();
  659. }
  660. $this->addVariables(['tabObj' => $tabObj]);
  661. return view($this->view, $this->variables())->render();
  662. }
  663. /**
  664. * Output as string.
  665. *
  666. * @return string
  667. */
  668. public function __toString()
  669. {
  670. return $this->render();
  671. }
  672. /**
  673. * @param mixed ...$params
  674. *
  675. * @return $this
  676. */
  677. public static function make(...$params)
  678. {
  679. return new static(...$params);
  680. }
  681. }