Form.php 20 KB

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