Form.php 19 KB

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