Form.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. */
  73. class Form implements Renderable
  74. {
  75. use HasHtmlAttributes,
  76. HasFormResponse,
  77. HasAuthorization,
  78. Macroable {
  79. __call as macroCall;
  80. }
  81. /**
  82. * @var string
  83. */
  84. protected $view = 'admin::widgets.form';
  85. /**
  86. * @var Field[]|Collection
  87. */
  88. protected $fields;
  89. /**
  90. * @var bool
  91. */
  92. protected $useAjaxSubmit = true;
  93. /**
  94. * @var Fluent
  95. */
  96. protected $data;
  97. /**
  98. * @var mixed
  99. */
  100. protected $primaryKey;
  101. /**
  102. * Available buttons.
  103. *
  104. * @var array
  105. */
  106. protected $buttons = ['reset' => true, 'submit' => true];
  107. /**
  108. * @var bool
  109. */
  110. protected $useFormTag = true;
  111. /**
  112. * @var string
  113. */
  114. protected $elementId;
  115. /**
  116. * @var array
  117. */
  118. protected $width = [
  119. 'label' => 2,
  120. 'field' => 8,
  121. ];
  122. /**
  123. * Form constructor.
  124. *
  125. * @param array $data
  126. * @param mixed $key
  127. */
  128. public function __construct($data = [], $key = null)
  129. {
  130. if ($data) {
  131. $this->fill($data);
  132. }
  133. $this->setKey($key);
  134. $this->initFields();
  135. $this->initFormAttributes();
  136. }
  137. /**
  138. * Initialize the form fields.
  139. */
  140. protected function initFields()
  141. {
  142. $this->fields = new Collection();
  143. }
  144. /**
  145. * Initialize the form attributes.
  146. */
  147. protected function initFormAttributes()
  148. {
  149. $this->setHtmlAttribute([
  150. 'method' => 'POST',
  151. 'action' => '',
  152. 'class' => 'form-horizontal',
  153. 'accept-charset' => 'UTF-8',
  154. 'pjax-container' => true,
  155. ]);
  156. }
  157. /**
  158. * Action uri of the form.
  159. *
  160. * @param string $action
  161. *
  162. * @return $this|string
  163. */
  164. public function action($action = null)
  165. {
  166. if ($action === null) {
  167. return $this->getHtmlAttribute('action');
  168. }
  169. return $this->setHtmlAttribute('action', $action);
  170. }
  171. /**
  172. * Method of the form.
  173. *
  174. * @param string $method
  175. *
  176. * @return $this
  177. */
  178. public function method($method = 'POST')
  179. {
  180. return $this->setHtmlAttribute('method', strtoupper($method));
  181. }
  182. /**
  183. * Set primary key.
  184. *
  185. * @param mixed $value
  186. *
  187. * @return $this
  188. */
  189. public function setKey($value)
  190. {
  191. $this->primaryKey = $value;
  192. return $this;
  193. }
  194. /**
  195. * Get primary key.
  196. *
  197. * @return mixed
  198. */
  199. public function getKey()
  200. {
  201. return $this->primaryKey;
  202. }
  203. /**
  204. * @param array|Arrayable|Closure $data
  205. *
  206. * @return Fluent
  207. */
  208. public function data()
  209. {
  210. if (! $this->data) {
  211. $this->fill([]);
  212. }
  213. return $this->data;
  214. }
  215. /**
  216. * @param array|Arrayable|Closure $data
  217. *
  218. * @return $this
  219. */
  220. public function fill($data)
  221. {
  222. $this->data = new Fluent(Helper::array($data));
  223. return $this;
  224. }
  225. /**
  226. * @return Fluent
  227. */
  228. public function model()
  229. {
  230. return $this->data();
  231. }
  232. /**
  233. * Add a fieldset to form.
  234. *
  235. * @param string $title
  236. * @param Closure $setCallback
  237. *
  238. * @return Field\Fieldset
  239. */
  240. public function fieldset(string $title, Closure $setCallback)
  241. {
  242. $fieldset = new Field\Fieldset();
  243. $this->html($fieldset->start($title))->plain();
  244. $setCallback($this);
  245. $this->html($fieldset->end())->plain();
  246. return $fieldset;
  247. }
  248. /**
  249. * Get specify field.
  250. *
  251. * @param string|Field $name
  252. *
  253. * @return Field|null
  254. */
  255. public function field($name)
  256. {
  257. foreach ($this->fields as $field) {
  258. if ($field === $name || $field->column() === $name) {
  259. return $field;
  260. }
  261. }
  262. }
  263. /**
  264. * @return Field[]|Collection
  265. */
  266. public function fields()
  267. {
  268. return $this->fields;
  269. }
  270. /**
  271. * Validate this form fields.
  272. *
  273. * @param Request $request
  274. *
  275. * @return bool|MessageBag
  276. */
  277. public function validate(Request $request)
  278. {
  279. if (method_exists($this, 'form')) {
  280. $this->form();
  281. }
  282. $failedValidators = [];
  283. /** @var \Dcat\Admin\Form\Field $field */
  284. foreach ($this->fields() as $field) {
  285. if (! $validator = $field->getValidator($request->all())) {
  286. continue;
  287. }
  288. if (($validator instanceof Validator) && ! $validator->passes()) {
  289. $failedValidators[] = $validator;
  290. }
  291. }
  292. $message = $this->mergeValidationMessages($failedValidators);
  293. return $message->any() ? $message : false;
  294. }
  295. /**
  296. * Merge validation messages from input validators.
  297. *
  298. * @param \Illuminate\Validation\Validator[] $validators
  299. *
  300. * @return MessageBag
  301. */
  302. protected function mergeValidationMessages($validators)
  303. {
  304. $messageBag = new MessageBag();
  305. foreach ($validators as $validator) {
  306. $messageBag = $messageBag->merge($validator->messages());
  307. }
  308. return $messageBag;
  309. }
  310. /**
  311. * Disable Pjax.
  312. *
  313. * @return $this
  314. */
  315. public function disablePjax()
  316. {
  317. $this->forgetHtmlAttribute('pjax-container');
  318. return $this;
  319. }
  320. /**
  321. * Disable form tag.
  322. *
  323. * @return $this;
  324. */
  325. public function disableFormTag()
  326. {
  327. $this->useFormTag = false;
  328. return $this;
  329. }
  330. /**
  331. * Disable reset button.
  332. *
  333. * @return $this
  334. */
  335. public function disableResetButton()
  336. {
  337. $this->buttons['reset'] = false;
  338. return $this;
  339. }
  340. /**
  341. * Disable submit button.
  342. *
  343. * @return $this
  344. */
  345. public function disableSubmitButton()
  346. {
  347. $this->buttons['submit'] = false;
  348. return $this;
  349. }
  350. /**
  351. * Set field and label width in current form.
  352. *
  353. * @param int $fieldWidth
  354. * @param int $labelWidth
  355. *
  356. * @return $this
  357. */
  358. public function width($fieldWidth = 8, $labelWidth = 2)
  359. {
  360. $this->width = [
  361. 'label' => $labelWidth,
  362. 'field' => $fieldWidth,
  363. ];
  364. $this->fields->each(function ($field) use ($fieldWidth, $labelWidth) {
  365. /* @var Field $field */
  366. $field->width($fieldWidth, $labelWidth);
  367. });
  368. return $this;
  369. }
  370. /**
  371. * Find field class with given name.
  372. *
  373. * @param string $method
  374. *
  375. * @return bool|string
  376. */
  377. public static function findFieldClass($method)
  378. {
  379. $class = Arr::get(\Dcat\Admin\Form::extensions(), $method);
  380. if (class_exists($class)) {
  381. return $class;
  382. }
  383. return false;
  384. }
  385. /**
  386. * Add a form field to form.
  387. *
  388. * @param Field $field
  389. *
  390. * @return $this
  391. */
  392. public function pushField(Field &$field)
  393. {
  394. $this->fields->push($field);
  395. $field->setForm($this);
  396. $field->width($this->width['field'], $this->width['label']);
  397. $field::collectAssets();
  398. return $this;
  399. }
  400. /**
  401. * Get variables for render form.
  402. *
  403. * @return array
  404. */
  405. protected function variables()
  406. {
  407. $this->setHtmlAttribute('id', $this->getElementId());
  408. foreach ($this->fields as $field) {
  409. $field->fill($this->model()->toArray());
  410. }
  411. return [
  412. 'start' => $this->open(),
  413. 'end' => $this->close(),
  414. 'fields' => $this->fields,
  415. 'method' => $this->getHtmlAttribute('method'),
  416. 'buttons' => $this->buttons,
  417. ];
  418. }
  419. /**
  420. * @return string
  421. */
  422. protected function open()
  423. {
  424. return <<<HTML
  425. <form {$this->formatHtmlAttributes()}>
  426. HTML;
  427. }
  428. /**
  429. * @return string
  430. */
  431. protected function close()
  432. {
  433. return '</form>';
  434. }
  435. /**
  436. * Determine if form fields has files.
  437. *
  438. * @return bool
  439. */
  440. public function hasFile()
  441. {
  442. foreach ($this->fields as $field) {
  443. if ($field instanceof Field\File) {
  444. return true;
  445. }
  446. }
  447. return false;
  448. }
  449. /**
  450. * @param $id
  451. *
  452. * @return $this
  453. */
  454. public function setFormId($id)
  455. {
  456. $this->elementId = $id;
  457. return $this;
  458. }
  459. /**
  460. * @return string
  461. */
  462. public function getElementId()
  463. {
  464. return $this->elementId ?: ($this->elementId = 'form-'.Str::random(8));
  465. }
  466. /**
  467. * Generate a Field object and add to form builder if Field exists.
  468. *
  469. * @param string $method
  470. * @param array $arguments
  471. *
  472. * @return Field|null
  473. */
  474. public function __call($method, $arguments)
  475. {
  476. if ($className = static::findFieldClass($method)) {
  477. $name = Arr::get($arguments, 0, '');
  478. $element = new $className($name, array_slice($arguments, 1));
  479. $this->pushField($element);
  480. return $element;
  481. }
  482. if (static::hasMacro($method)) {
  483. return $this->macroCall($method, $arguments);
  484. }
  485. throw new \BadMethodCallException("Field [{$method}] does not exist.");
  486. }
  487. /**
  488. * Disable submit with ajax.
  489. *
  490. * @param bool $disable
  491. *
  492. * @return $this
  493. */
  494. public function disableAjaxSubmit(bool $disable = true)
  495. {
  496. $this->useAjaxSubmit = ! $disable;
  497. return $this;
  498. }
  499. /**
  500. * @return bool
  501. */
  502. public function allowAjaxSubmit()
  503. {
  504. return $this->useAjaxSubmit === true;
  505. }
  506. /**
  507. * @return void
  508. */
  509. protected function setupSubmitScript()
  510. {
  511. Admin::script(
  512. <<<JS
  513. $('#{$this->getElementId()}').form({
  514. validate: true,
  515. });
  516. JS
  517. );
  518. }
  519. /**
  520. * @param array $input
  521. *
  522. * @return array
  523. */
  524. public function sanitize(array $input)
  525. {
  526. Arr::forget($input, ['_form_', '_token', '_current_']);
  527. return $input;
  528. }
  529. protected function prepareForm()
  530. {
  531. if (method_exists($this, 'form')) {
  532. $this->form();
  533. }
  534. if (! $this->data && method_exists($this, 'default')) {
  535. $data = $this->default();
  536. if (is_array($data)) {
  537. $this->fill($data);
  538. }
  539. }
  540. }
  541. protected function prepareHandler()
  542. {
  543. if (method_exists($this, 'handle')) {
  544. $this->method('POST');
  545. $this->action(route('dcat.api.form'));
  546. $this->hidden('_form_')->default(get_called_class());
  547. $this->hidden('_current_')->default($this->getCurrentUrl());
  548. }
  549. }
  550. /**
  551. * Render the form.
  552. *
  553. * @return string
  554. */
  555. public function render()
  556. {
  557. $this->prepareForm();
  558. $this->prepareHandler();
  559. if ($this->allowAjaxSubmit()) {
  560. $this->setupSubmitScript();
  561. }
  562. return view($this->view, $this->variables())->render();
  563. }
  564. /**
  565. * Output as string.
  566. *
  567. * @return string
  568. */
  569. public function __toString()
  570. {
  571. return $this->render();
  572. }
  573. /**
  574. * @param mixed ...$params
  575. *
  576. * @return $this
  577. */
  578. public static function make(...$params)
  579. {
  580. return new static(...$params);
  581. }
  582. }