Form.php 19 KB

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