Form.php 16 KB

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