Form.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  1. <?php
  2. namespace Dcat\Admin;
  3. use Closure;
  4. use Dcat\Admin\Actions\Action;
  5. use Dcat\Admin\Contracts\Repository;
  6. use Dcat\Admin\Form\AbstractTool;
  7. use Dcat\Admin\Form\Builder;
  8. use Dcat\Admin\Form\Concerns;
  9. use Dcat\Admin\Form\Condition;
  10. use Dcat\Admin\Form\Field;
  11. use Dcat\Admin\Form\NestedForm;
  12. use Dcat\Admin\Support\Helper;
  13. use Dcat\Admin\Traits\HasBuilderEvents;
  14. use Dcat\Admin\Traits\HasFormResponse;
  15. use Dcat\Admin\Widgets\DialogForm;
  16. use Illuminate\Contracts\Support\MessageProvider;
  17. use Illuminate\Contracts\Support\Renderable;
  18. use Illuminate\Database\Eloquent\Model;
  19. use Illuminate\Http\Request;
  20. use Illuminate\Support\Arr;
  21. use Illuminate\Support\Collection;
  22. use Illuminate\Support\Fluent;
  23. use Illuminate\Support\MessageBag;
  24. use Illuminate\Support\Traits\Macroable;
  25. use Illuminate\Validation\Validator;
  26. use Symfony\Component\HttpFoundation\Response;
  27. /**
  28. * Class Form.
  29. *
  30. * @method Field\Text text($column, $label = '')
  31. * @method Field\Checkbox checkbox($column, $label = '')
  32. * @method Field\Radio radio($column, $label = '')
  33. * @method Field\Select select($column, $label = '')
  34. * @method Field\MultipleSelect multipleSelect($column, $label = '')
  35. * @method Field\Textarea textarea($column, $label = '')
  36. * @method Field\Hidden hidden($column, $label = '')
  37. * @method Field\Id id($column, $label = '')
  38. * @method Field\Ip ip($column, $label = '')
  39. * @method Field\Url url($column, $label = '')
  40. * @method Field\Email email($column, $label = '')
  41. * @method Field\Mobile mobile($column, $label = '')
  42. * @method Field\Slider slider($column, $label = '')
  43. * @method Field\Map map($latitude, $longitude, $label = '')
  44. * @method Field\Editor editor($column, $label = '')
  45. * @method Field\Date date($column, $label = '')
  46. * @method Field\Datetime datetime($column, $label = '')
  47. * @method Field\Time time($column, $label = '')
  48. * @method Field\Year year($column, $label = '')
  49. * @method Field\Month month($column, $label = '')
  50. * @method Field\DateRange dateRange($start, $end, $label = '')
  51. * @method Field\DateTimeRange datetimeRange($start, $end, $label = '')
  52. * @method Field\TimeRange timeRange($start, $end, $label = '')
  53. * @method Field\Number number($column, $label = '')
  54. * @method Field\Currency currency($column, $label = '')
  55. * @method Field\SwitchField switch($column, $label = '')
  56. * @method Field\Display display($column, $label = '')
  57. * @method Field\Rate rate($column, $label = '')
  58. * @method Field\Divide divider(string $title = null)
  59. * @method Field\Password password($column, $label = '')
  60. * @method Field\Decimal decimal($column, $label = '')
  61. * @method Field\Html html($html, $label = '')
  62. * @method Field\Tags tags($column, $label = '')
  63. * @method Field\Icon icon($column, $label = '')
  64. * @method Field\Embeds embeds($column, $label = '', Closure $callback = null)
  65. * @method Field\Captcha captcha()
  66. * @method Field\Listbox listbox($column, $label = '')
  67. * @method Field\File file($column, $label = '')
  68. * @method Field\Image image($column, $label = '')
  69. * @method Field\MultipleFile multipleFile($column, $label = '')
  70. * @method Field\MultipleImage multipleImage($column, $label = '')
  71. * @method Field\HasMany hasMany($column, $labelOrCallback, $callback = null)
  72. * @method Field\Tree tree($column, $label = '')
  73. * @method Field\Table table($column, $labelOrCallback, $callback = null)
  74. * @method Field\ListField list($column, $label = '')
  75. * @method Field\Timezone timezone($column, $label = '')
  76. * @method Field\KeyValue keyValue($column, $label = '')
  77. * @method Field\Tel tel($column, $label = '')
  78. * @method Field\Markdown markdown($column, $label = '')
  79. * @method Field\Range range($start, $end, $label = '')
  80. * @method Field\Color color($column, $label = '')
  81. * @method Field\ArrayField array($column, $labelOrCallback, $callback = null)
  82. * @method Field\SelectTable selectTable($column, $label = '')
  83. * @method Field\MultipleSelectTable multipleSelectTable($column, $label = '')
  84. * @method Field\Button button(string $html = null)
  85. */
  86. class Form implements Renderable
  87. {
  88. use HasBuilderEvents;
  89. use HasFormResponse;
  90. use Concerns\HasEvents;
  91. use Concerns\HasFiles;
  92. use Concerns\HandleCascadeFields;
  93. use Concerns\HasRows;
  94. use Concerns\HasTabs;
  95. use Macroable {
  96. __call as macroCall;
  97. }
  98. /**
  99. * Remove flag in `has many` form.
  100. */
  101. const REMOVE_FLAG_NAME = '_remove_';
  102. const CURRENT_URL_NAME = '_current_';
  103. /**
  104. * Available fields.
  105. *
  106. * @var array
  107. */
  108. protected static $availableFields = [
  109. 'button' => Field\Button::class,
  110. 'checkbox' => Field\Checkbox::class,
  111. 'currency' => Field\Currency::class,
  112. 'date' => Field\Date::class,
  113. 'dateRange' => Field\DateRange::class,
  114. 'datetime' => Field\Datetime::class,
  115. 'datetimeRange' => Field\DatetimeRange::class,
  116. 'decimal' => Field\Decimal::class,
  117. 'display' => Field\Display::class,
  118. 'divider' => Field\Divide::class,
  119. 'embeds' => Field\Embeds::class,
  120. 'editor' => Field\Editor::class,
  121. 'email' => Field\Email::class,
  122. 'hidden' => Field\Hidden::class,
  123. 'id' => Field\Id::class,
  124. 'ip' => Field\Ip::class,
  125. 'map' => Field\Map::class,
  126. 'mobile' => Field\Mobile::class,
  127. 'month' => Field\Month::class,
  128. 'multipleSelect' => Field\MultipleSelect::class,
  129. 'number' => Field\Number::class,
  130. 'password' => Field\Password::class,
  131. 'radio' => Field\Radio::class,
  132. 'rate' => Field\Rate::class,
  133. 'select' => Field\Select::class,
  134. 'slider' => Field\Slider::class,
  135. 'switch' => Field\SwitchField::class,
  136. 'text' => Field\Text::class,
  137. 'textarea' => Field\Textarea::class,
  138. 'time' => Field\Time::class,
  139. 'timeRange' => Field\TimeRange::class,
  140. 'url' => Field\Url::class,
  141. 'year' => Field\Year::class,
  142. 'html' => Field\Html::class,
  143. 'tags' => Field\Tags::class,
  144. 'icon' => Field\Icon::class,
  145. 'captcha' => Field\Captcha::class,
  146. 'listbox' => Field\Listbox::class,
  147. 'file' => Field\File::class,
  148. 'image' => Field\Image::class,
  149. 'multipleFile' => Field\MultipleFile::class,
  150. 'multipleImage' => Field\MultipleImage::class,
  151. 'hasMany' => Field\HasMany::class,
  152. 'tree' => Field\Tree::class,
  153. 'table' => Field\Table::class,
  154. 'list' => Field\ListField::class,
  155. 'timezone' => Field\Timezone::class,
  156. 'keyValue' => Field\KeyValue::class,
  157. 'tel' => Field\Tel::class,
  158. 'markdown' => Field\Markdown::class,
  159. 'range' => Field\Range::class,
  160. 'color' => Field\Color::class,
  161. 'array' => Field\ArrayField::class,
  162. 'selectTable' => Field\SelectTable::class,
  163. 'multipleSelectTable' => Field\MultipleSelectTable::class,
  164. ];
  165. /**
  166. * Collected field assets.
  167. *
  168. * @var array
  169. */
  170. protected static $collectedAssets = [];
  171. /**
  172. * Form field alias.
  173. *
  174. * @var array
  175. */
  176. public static $fieldAlias = [];
  177. /**
  178. * @var Repository
  179. */
  180. protected $repository;
  181. /**
  182. * @var Closure
  183. */
  184. protected $callback;
  185. /**
  186. * @var Request
  187. */
  188. protected $request;
  189. /**
  190. * @var bool
  191. */
  192. protected $ajax = true;
  193. /**
  194. * Model of the form.
  195. *
  196. * @var Fluent
  197. */
  198. protected $model;
  199. /**
  200. * @var \Illuminate\Validation\Validator
  201. */
  202. protected $validator;
  203. /**
  204. * @var Builder
  205. */
  206. protected $builder;
  207. /**
  208. * Resource path for this form page.
  209. *
  210. * @var string
  211. */
  212. protected $resource;
  213. /**
  214. * Data for save to current model from input.
  215. *
  216. * @var array
  217. */
  218. protected $updates = [];
  219. /**
  220. * Input data.
  221. *
  222. * @var array
  223. */
  224. protected $inputs = [];
  225. /**
  226. * Ignored saving fields.
  227. *
  228. * @var array
  229. */
  230. protected $ignored = [];
  231. /**
  232. * @var bool
  233. */
  234. protected $isSoftDeletes = false;
  235. /**
  236. * @var MessageBag
  237. */
  238. protected $validationMessages;
  239. /**
  240. * @var Condition[]
  241. */
  242. protected $conditions = [];
  243. /**
  244. * @var array
  245. */
  246. public $context = [];
  247. /**
  248. * @var bool
  249. */
  250. public $validationErrorToastr = true;
  251. /**
  252. * Create a new form instance.
  253. *
  254. * @param Repository|Model|\Illuminate\Database\Eloquent\Builder|string $model
  255. * @param \Closure $callback
  256. * @param Request $request
  257. */
  258. public function __construct($repository = null, ?Closure $callback = null, Request $request = null)
  259. {
  260. $this->repository = $repository ? Admin::repository($repository) : null;
  261. $this->callback = $callback;
  262. $this->request = $request ?: request();
  263. $this->builder = new Builder($this);
  264. $this->isSoftDeletes = $repository ? $this->repository->isSoftDeletes() : false;
  265. $this->model(new Fluent());
  266. $this->prepareDialogForm();
  267. $this->callResolving();
  268. }
  269. /**
  270. * Create a form instance.
  271. *
  272. * @param mixed ...$params
  273. *
  274. * @return $this
  275. */
  276. public static function make(...$params)
  277. {
  278. return new static(...$params);
  279. }
  280. /**
  281. * @param Field $field
  282. *
  283. * @return $this
  284. */
  285. public function pushField(Field $field)
  286. {
  287. $field->setForm($this);
  288. $this->builder->pushField($field);
  289. $this->builder->layout()->addField($field);
  290. $width = $this->builder->getWidth();
  291. $field->width($width['field'], $width['label']);
  292. $field::requireAssets();
  293. return $this;
  294. }
  295. /**
  296. * Get specify field.
  297. *
  298. * @param string|null $name
  299. *
  300. * @return Field|Collection|Field[]|null
  301. */
  302. public function field($name = null)
  303. {
  304. return $this->builder->field($name);
  305. }
  306. /**
  307. * @return Collection|Field[]
  308. */
  309. public function fields()
  310. {
  311. return $this->builder->fields();
  312. }
  313. /**
  314. * @param $column
  315. *
  316. * @return $this
  317. */
  318. public function removeField($column)
  319. {
  320. $this->builder->removeField($column);
  321. return $this;
  322. }
  323. /**
  324. * @param string $title
  325. * @param string $content
  326. *
  327. * @return $this
  328. */
  329. public function confirm(?string $title = null, ?string $content = null)
  330. {
  331. $this->builder->confirm($title, $content);
  332. return $this;
  333. }
  334. /**
  335. * @return bool
  336. */
  337. public function isCreating()
  338. {
  339. return $this->builder->isCreating();
  340. }
  341. /**
  342. * @return bool
  343. */
  344. public function isEditing()
  345. {
  346. return $this->builder->isEditing();
  347. }
  348. /**
  349. * @return bool
  350. */
  351. public function isDeleting()
  352. {
  353. return $this->builder->isDeleting();
  354. }
  355. /**
  356. * @param Fluent|array|\Illuminate\Database\Eloquent\Model $model
  357. *
  358. * @return Fluent|\Illuminate\Database\Eloquent\Model|void
  359. */
  360. public function model($model = null)
  361. {
  362. if ($model === null) {
  363. return $this->model;
  364. }
  365. if (is_array($model)) {
  366. $model = new Fluent($model);
  367. }
  368. $this->model = $model;
  369. }
  370. /**
  371. * Get resource id.
  372. *
  373. * @return mixed
  374. */
  375. public function getKey()
  376. {
  377. return $this->builder()->getResourceId();
  378. }
  379. /**
  380. * 启用或禁用ajax表单提交.
  381. *
  382. * @param bool $value
  383. *
  384. * @return $this
  385. */
  386. public function ajax(bool $value = true)
  387. {
  388. $this->ajax = $value;
  389. return $this;
  390. }
  391. /**
  392. * @return bool
  393. */
  394. public function allowAjaxSubmit()
  395. {
  396. return $this->ajax === true;
  397. }
  398. /**
  399. * 设置使用 Toastr 展示字段验证信息.
  400. *
  401. * @param bool $value
  402. *
  403. * @return $this
  404. */
  405. public function validationErrorToastr(bool $value = true)
  406. {
  407. $this->validationErrorToastr = $value;
  408. return $this;
  409. }
  410. /**
  411. * @param \Closure $closure
  412. *
  413. * @return $this;
  414. */
  415. public function wrap(\Closure $closure)
  416. {
  417. $this->builder->wrap($closure);
  418. return $this;
  419. }
  420. /**
  421. * @return Builder
  422. */
  423. public function builder()
  424. {
  425. return $this->builder;
  426. }
  427. /**
  428. * @return string
  429. */
  430. public function getElementId()
  431. {
  432. return $this->builder->getElementId();
  433. }
  434. /**
  435. * @return \Dcat\Admin\Form\Layout
  436. */
  437. public function layout()
  438. {
  439. return $this->builder->layout();
  440. }
  441. /**
  442. * @return Repository
  443. */
  444. public function repository()
  445. {
  446. return $this->repository;
  447. }
  448. /**
  449. * Generate a edit form.
  450. *
  451. * @param $id
  452. *
  453. * @return $this
  454. */
  455. public function edit($id)
  456. {
  457. $this->builder->mode(Builder::MODE_EDIT);
  458. $this->builder->setResourceId($id);
  459. $this->model($this->repository->edit($this));
  460. return $this;
  461. }
  462. /**
  463. * Add a fieldset to form.
  464. *
  465. * @param string $title
  466. * @param Closure $setCallback
  467. *
  468. * @return Field\Fieldset
  469. */
  470. public function fieldset(string $title, Closure $setCallback)
  471. {
  472. $fieldset = new Field\Fieldset();
  473. $this->html($fieldset->start($title))->plain();
  474. $setCallback($this);
  475. $this->html($fieldset->end())->plain();
  476. return $fieldset;
  477. }
  478. /**
  479. * Destroy data entity and remove files.
  480. *
  481. * @param $id
  482. *
  483. * @return mixed
  484. */
  485. public function destroy($id)
  486. {
  487. try {
  488. $this->builder->setResourceId($id);
  489. $this->builder->mode(Builder::MODE_DELETE);
  490. $data = $this->repository->deleting($this);
  491. $this->model(new Fluent($data));
  492. $this->setFieldOriginalValue();
  493. $this->build();
  494. if ($response = $this->callDeleting()) {
  495. return $this->sendResponse($response);
  496. }
  497. $result = $this->repository->delete($this, $data);
  498. if ($response = $this->callDeleted($result)) {
  499. return $this->sendResponse($response);
  500. }
  501. $status = $result ? true : false;
  502. $message = $result ? trans('admin.delete_succeeded') : trans('admin.delete_failed');
  503. } catch (\Throwable $exception) {
  504. $response = $this->handleException($exception);
  505. if ($response instanceof Response) {
  506. return $response;
  507. }
  508. $status = false;
  509. $message = $exception->getMessage() ?: trans('admin.delete_failed');
  510. }
  511. return $this->sendResponse(
  512. $this->response()
  513. ->alert()
  514. ->status($status)
  515. ->message($message)
  516. );
  517. }
  518. /**
  519. * @param \Throwable $e
  520. *
  521. * @return mixed
  522. */
  523. protected function handleException(\Throwable $e)
  524. {
  525. return Admin::handleException($e);
  526. }
  527. /**
  528. * Store a new record.
  529. *
  530. * @param array|null $data
  531. * @param string|string $redirectTo
  532. *
  533. * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse|Response
  534. */
  535. public function store(?array $data = null, $redirectTo = null)
  536. {
  537. try {
  538. if ($data) {
  539. $this->request->replace($data);
  540. }
  541. $data = $data ?: $this->request->all();
  542. if ($response = $this->beforeStore($data)) {
  543. return $this->sendResponse($response);
  544. }
  545. $this->updates = $this->prepareInsert($this->updates);
  546. $id = $this->repository->store($this);
  547. $this->builder->setResourceId($id);
  548. if (($response = $this->callSaved($id))) {
  549. return $this->sendResponse($response);
  550. }
  551. if (! $id) {
  552. return $this->sendResponse(
  553. $this->response()
  554. ->error(trans('admin.save_failed'))
  555. );
  556. }
  557. $url = $this->getRedirectUrl($id, $redirectTo);
  558. return $this->sendResponse(
  559. $this->response()
  560. ->redirectIf($url !== false, $url)
  561. ->success(trans('admin.save_succeeded'))
  562. );
  563. } catch (\Throwable $e) {
  564. $response = $this->handleException($e);
  565. if ($response instanceof Response) {
  566. return $response;
  567. }
  568. return $this->sendResponse(
  569. $this->response()
  570. ->error(trans('admin.save_failed'))
  571. ->withExceptionIf($e->getMessage(), $e)
  572. );
  573. }
  574. }
  575. /**
  576. * Before store.
  577. *
  578. * @param array $data
  579. *
  580. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|Response|void
  581. */
  582. protected function beforeStore(array $data)
  583. {
  584. $this->inputs = $data;
  585. $this->build();
  586. if (($response = $this->callSubmitted())) {
  587. return $response;
  588. }
  589. if ($response = $this->handleUploadFile($this->inputs)) {
  590. return $response;
  591. }
  592. if ($response = $this->deleteFileWhenCreating($this->inputs)) {
  593. return $response;
  594. }
  595. // Handle validation errors.
  596. if ($validationMessages = $this->validationMessages($this->inputs)) {
  597. return $this->validationErrorsResponse($validationMessages);
  598. }
  599. if (($response = $this->prepare($this->inputs))) {
  600. return $response;
  601. }
  602. }
  603. /**
  604. * Prepare input data for insert or update.
  605. *
  606. * @param array $data
  607. *
  608. * @return Response|null
  609. */
  610. protected function prepare($data = [])
  611. {
  612. $this->inputs = $this->removeIgnoredFields($data);
  613. if ($response = $this->callSaving()) {
  614. return $response;
  615. }
  616. $this->updates = $this->inputs;
  617. }
  618. /**
  619. * Remove ignored fields from input.
  620. *
  621. * @param array $input
  622. *
  623. * @return array
  624. */
  625. public function removeIgnoredFields($input)
  626. {
  627. Arr::forget($input, $this->ignored);
  628. return $input;
  629. }
  630. /**
  631. * Get or set data for insert or update.
  632. *
  633. * @param array $updates
  634. *
  635. * @return $this|array
  636. */
  637. public function updates(array $updates = null)
  638. {
  639. if ($updates === null) {
  640. return $this->updates;
  641. }
  642. $this->updates = array_merge($this->updates, $updates);
  643. return $this;
  644. }
  645. /**
  646. * Handle orderable update.
  647. *
  648. * @param int $id
  649. * @param array $input
  650. *
  651. * @return Response
  652. */
  653. protected function handleOrderable(array $input = [])
  654. {
  655. if (array_key_exists('_orderable', $input)) {
  656. $updated = $input['_orderable'] == 1
  657. ? $this->repository->moveOrderUp()
  658. : $this->repository->moveOrderDown();
  659. $message = $updated
  660. ? __('admin.update_succeeded')
  661. : __('admin.nothing_updated');
  662. return $this->sendResponse(
  663. $this->response()
  664. ->status((bool) $updated)
  665. ->message($message)
  666. );
  667. }
  668. }
  669. /**
  670. * Handle update.
  671. *
  672. * @param $id
  673. * @param array|null $data
  674. * @param string|null $redirectTo
  675. *
  676. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse||Response
  677. */
  678. public function update(
  679. $id,
  680. ?array $data = null,
  681. $redirectTo = null
  682. ) {
  683. try {
  684. if ($data) {
  685. $this->request->replace($data);
  686. }
  687. $data = $data ?: $this->request->all();
  688. if ($response = $this->beforeUpdate($id, $data)) {
  689. return $this->sendResponse($response);
  690. }
  691. $this->updates = $this->prepareUpdate($this->updates);
  692. $updated = $this->repository->update($this);
  693. if ($response = $this->callSaved($updated)) {
  694. return $this->sendResponse($response);
  695. }
  696. if (! $updated) {
  697. return $this->sendResponse(
  698. $this->response()
  699. ->error(trans('admin.update_failed'))
  700. );
  701. }
  702. $url = $this->getRedirectUrl($id, $redirectTo);
  703. return $this->sendResponse(
  704. $this->response()
  705. ->success(trans('admin.update_succeeded'))
  706. ->redirectIf($url !== false, $url)
  707. ->refreshIf($url === false)
  708. );
  709. } catch (\Throwable $e) {
  710. $response = $this->handleException($e);
  711. if ($response instanceof Response) {
  712. return $response;
  713. }
  714. return $this->sendResponse(
  715. $this->response()
  716. ->error(trans('admin.update_failed'))
  717. ->withExceptionIf($e->getMessage(), $e)
  718. );
  719. }
  720. }
  721. /**
  722. * Before update.
  723. *
  724. * @param array $data
  725. *
  726. * @return Response|void
  727. */
  728. protected function beforeUpdate($id, array &$data)
  729. {
  730. $this->builder->setResourceId($id);
  731. $this->builder->mode(Builder::MODE_EDIT);
  732. $this->inputs = $data;
  733. $this->model($this->repository->updating($this));
  734. $this->build();
  735. $this->setFieldOriginalValue();
  736. if ($response = $this->callSubmitted()) {
  737. return $response;
  738. }
  739. if ($uploadFileResponse = $this->handleUploadFile($this->inputs)) {
  740. return $uploadFileResponse;
  741. }
  742. $isEditable = $this->isEditable($this->inputs);
  743. $this->inputs = $this->handleEditable($this->inputs);
  744. $this->inputs = $this->handleFileDelete($this->inputs);
  745. $this->inputs = $this->handleHasManyValues($this->inputs);
  746. if ($response = $this->handleOrderable($this->inputs)) {
  747. return $response;
  748. }
  749. // Handle validation errors.
  750. if ($validationMessages = $this->validationMessages($this->inputs)) {
  751. return $this->validationErrorsResponse(
  752. $isEditable ? Arr::dot($validationMessages->toArray()) : $validationMessages
  753. );
  754. }
  755. if ($response = $this->prepare($this->inputs)) {
  756. return $response;
  757. }
  758. }
  759. /**
  760. * @param array $inputs
  761. *
  762. * @return array
  763. */
  764. protected function handleHasManyValues(array $inputs)
  765. {
  766. foreach ($inputs as $column => &$input) {
  767. $field = $this->builder()->field($column);
  768. if (is_array($input) && $field instanceof Field\HasMany) {
  769. $keyName = $field->getKeyName();
  770. foreach ($input as $k => &$v) {
  771. if (! array_key_exists($keyName, $v)) {
  772. $v[$keyName] = $k;
  773. }
  774. if (empty($v[NestedForm::REMOVE_FLAG_NAME])) {
  775. $v[NestedForm::REMOVE_FLAG_NAME] = null;
  776. }
  777. }
  778. }
  779. }
  780. return $inputs;
  781. }
  782. /**
  783. * @param $key
  784. * @param $redirectTo
  785. *
  786. * @return string|null
  787. */
  788. public function getRedirectUrl($key, $redirectTo = null)
  789. {
  790. if ($redirectTo) {
  791. return $redirectTo;
  792. }
  793. $resourcesPath = $this->isCreating() ? $this->resource(0) : $this->resource(-1);
  794. if ($this->request->get('after-save') == 1) {
  795. // continue editing
  796. if ($this->builder->isEditing()) {
  797. return false;
  798. }
  799. return rtrim($resourcesPath, '/')."/{$key}/edit";
  800. }
  801. if ($this->request->get('after-save') == 2) {
  802. // continue creating
  803. return rtrim($resourcesPath, '/').'/create';
  804. }
  805. if ($this->request->get('after-save') == 3) {
  806. // view resource
  807. return rtrim($resourcesPath, '/')."/{$key}";
  808. }
  809. return $this->request->get(Builder::PREVIOUS_URL_KEY) ?: $this->getCurrentUrl($resourcesPath);
  810. }
  811. /**
  812. * Check if request is from editable.
  813. *
  814. * @param array $input
  815. *
  816. * @return bool
  817. */
  818. protected function isEditable(array $input = [])
  819. {
  820. return array_key_exists('_editable', $input);
  821. }
  822. /**
  823. * Handle editable update.
  824. *
  825. * @param array $input
  826. *
  827. * @return array
  828. */
  829. protected function handleEditable(array $input = [])
  830. {
  831. if (array_key_exists('_editable', $input)) {
  832. $name = $input['name'];
  833. $value = $input['value'];
  834. Arr::forget($input, ['pk', 'value', 'name']);
  835. Arr::set($input, $name, $value);
  836. }
  837. return $input;
  838. }
  839. /**
  840. * Prepare input data for update.
  841. *
  842. * @param array $updates
  843. *
  844. * @return array
  845. */
  846. public function prepareUpdate(array $updates)
  847. {
  848. $prepared = [];
  849. /** @var Field $field */
  850. foreach ($this->builder->fields() as $field) {
  851. $columns = $field->column();
  852. // If column not in input array data, then continue.
  853. if (! Arr::has($updates, $columns)) {
  854. continue;
  855. }
  856. $value = $this->getDataByColumn($updates, $columns);
  857. $value = $field->prepare($value);
  858. if (is_array($columns)) {
  859. foreach ($columns as $name => $column) {
  860. Arr::set($prepared, $column, $value[$name]);
  861. }
  862. } elseif (is_string($columns)) {
  863. Arr::set($prepared, $columns, $value);
  864. }
  865. }
  866. return $prepared;
  867. }
  868. /**
  869. * Prepare input data for insert.
  870. *
  871. * @param $inserts
  872. *
  873. * @return array
  874. */
  875. public function prepareInsert($inserts)
  876. {
  877. Helper::prepareHasOneRelation($this->builder->fields(), $inserts);
  878. foreach ($inserts as $column => $value) {
  879. if (is_null($field = $this->field($column))) {
  880. unset($inserts[$column]);
  881. continue;
  882. }
  883. $inserts[$column] = $field->prepare($value);
  884. }
  885. $prepared = [];
  886. foreach ($inserts as $key => $value) {
  887. Arr::set($prepared, $key, $value);
  888. }
  889. return $prepared;
  890. }
  891. /**
  892. * Ignore fields to save.
  893. *
  894. * @param string|array $fields
  895. *
  896. * @return $this
  897. */
  898. public function ignore($fields)
  899. {
  900. $this->ignored = Arr::flatten(
  901. array_merge($this->ignored, (array) $fields)
  902. );
  903. return $this;
  904. }
  905. /**
  906. * @param $keys
  907. *
  908. * @return $this
  909. */
  910. public function forgetIgnored($keys)
  911. {
  912. Arr::forget($this->ignored, $keys);
  913. return $this;
  914. }
  915. /**
  916. * Get primary key name of model.
  917. *
  918. * @return string
  919. */
  920. public function keyName()
  921. {
  922. if (! $this->repository) {
  923. return 'id';
  924. }
  925. return $this->repository->getKeyName();
  926. }
  927. /**
  928. * @return string|void
  929. */
  930. public function createdAtColumn()
  931. {
  932. if (! $this->repository) {
  933. return;
  934. }
  935. return $this->repository->getCreatedAtColumn();
  936. }
  937. /**
  938. * @return string|void
  939. */
  940. public function updatedAtColumn()
  941. {
  942. if (! $this->repository) {
  943. return;
  944. }
  945. return $this->repository->getUpdatedAtColumn();
  946. }
  947. /**
  948. * @param array $data
  949. * @param string|array $columns
  950. *
  951. * @return array|mixed
  952. */
  953. protected function getDataByColumn($data, $columns)
  954. {
  955. if (is_string($columns)) {
  956. return Arr::get($data, $columns);
  957. }
  958. if (is_array($columns)) {
  959. $value = [];
  960. foreach ($columns as $name => $column) {
  961. if (! Arr::has($data, $column)) {
  962. continue;
  963. }
  964. $value[$name] = Arr::get($data, $column);
  965. }
  966. return $value;
  967. }
  968. }
  969. /**
  970. * Set original data for each field.
  971. *
  972. * @return void
  973. */
  974. protected function setFieldOriginalValue()
  975. {
  976. $data = $this->model()->toArray();
  977. $this->builder->fields()->each(function (Field $field) use ($data) {
  978. $field->setOriginal($data);
  979. });
  980. }
  981. /**
  982. * @example
  983. * $form->if(true)->then(function (Form $form) {
  984. * $form->text('name');
  985. * });
  986. *
  987. * $form->if(function (Form $form) {
  988. * return $form->model()->id > 5;
  989. * })->then(function (Form $form) {
  990. * $form->text('name');
  991. * });
  992. *
  993. * $form->if(true)->now(function (Form $form) {
  994. * $form->text('name');
  995. * });
  996. *
  997. * $form->if(true)->creating(function (Form $form) {});
  998. *
  999. * $form->if(true)->removeField('name');
  1000. *
  1001. * @param bool|\Closure $condition
  1002. *
  1003. * @return Condition
  1004. */
  1005. public function if($condition)
  1006. {
  1007. return $this->conditions[] = new Condition($condition, $this);
  1008. }
  1009. /**
  1010. * @return void
  1011. */
  1012. protected function rendering()
  1013. {
  1014. $this->build();
  1015. if ($this->isCreating()) {
  1016. $this->callCreating();
  1017. return;
  1018. }
  1019. $this->fillFields($this->model()->toArray());
  1020. $this->callEditing();
  1021. }
  1022. /**
  1023. * @param array $data
  1024. *
  1025. * @return void
  1026. */
  1027. public function fillFields(array $data)
  1028. {
  1029. $this->builder->fields()->each(function (Field $field) use ($data) {
  1030. if (! in_array($field->column(), $this->ignored, true)) {
  1031. $field->fill($data);
  1032. }
  1033. });
  1034. }
  1035. /**
  1036. * @return void
  1037. */
  1038. protected function build()
  1039. {
  1040. if ($callback = $this->callback) {
  1041. $callback($this);
  1042. }
  1043. foreach ($this->conditions as $condition) {
  1044. $condition->process();
  1045. }
  1046. }
  1047. /**
  1048. * Get validation messages.
  1049. *
  1050. * @param array $input
  1051. *
  1052. * @return MessageBag|bool
  1053. */
  1054. public function validationMessages($input)
  1055. {
  1056. $failedValidators = [];
  1057. /** @var Field $field */
  1058. foreach ($this->builder->fields() as $field) {
  1059. if (! $validator = $field->getValidator($input)) {
  1060. continue;
  1061. }
  1062. if (($validator instanceof Validator) && ! $validator->passes()) {
  1063. $failedValidators[] = [$field, $validator];
  1064. }
  1065. }
  1066. $message = $this->mergeValidationMessages($failedValidators);
  1067. if ($message->any() && $this->builder->isCreating()) {
  1068. $this->deleteFiles($input, true);
  1069. }
  1070. return $message->any() ? $message : false;
  1071. }
  1072. /**
  1073. * @param string|array|MessageProvider $column
  1074. * @param string|array $messages
  1075. *
  1076. * @return $this
  1077. */
  1078. public function responseValidationMessages($column, $messages = null)
  1079. {
  1080. if ($column instanceof MessageProvider) {
  1081. return $this->responseValidationMessages($column->getMessageBag()->getMessages());
  1082. }
  1083. if (! $this->validationMessages) {
  1084. $this->validationMessages = new MessageBag();
  1085. }
  1086. if (! $column) {
  1087. return $this;
  1088. }
  1089. if (is_array($column)) {
  1090. foreach ($column as $k => &$v) {
  1091. $v = (array) $v;
  1092. }
  1093. $this->validationMessages->merge($column);
  1094. } elseif ($messages) {
  1095. $this->validationMessages->merge([$column => (array) $messages]);
  1096. }
  1097. return $this;
  1098. }
  1099. /**
  1100. * Merge validation messages from input validators.
  1101. *
  1102. * @param array $validators
  1103. *
  1104. * @return MessageBag
  1105. */
  1106. protected function mergeValidationMessages($validators)
  1107. {
  1108. $messageBag = new MessageBag();
  1109. foreach ($validators as $value) {
  1110. [$field, $validator] = $value;
  1111. $messageBag = $messageBag->merge($field->formatValidatorMessages($validator->messages()));
  1112. }
  1113. if ($this->validationMessages) {
  1114. return $messageBag->merge($this->validationMessages);
  1115. }
  1116. return $messageBag;
  1117. }
  1118. /**
  1119. * Get or set action for form.
  1120. *
  1121. * @param string|null $action
  1122. *
  1123. * @return $this|string
  1124. */
  1125. public function action($action = null)
  1126. {
  1127. $value = $this->builder->action($action);
  1128. if ($action === null) {
  1129. return $value;
  1130. }
  1131. return $this;
  1132. }
  1133. /**
  1134. * Set field and label width in current form.
  1135. *
  1136. * @param int $fieldWidth
  1137. * @param int $labelWidth
  1138. *
  1139. * @return $this
  1140. */
  1141. public function width($fieldWidth = 8, $labelWidth = 2)
  1142. {
  1143. $this->builder->fields()->each(function ($field) use ($fieldWidth, $labelWidth) {
  1144. /* @var Field $field */
  1145. $field->width($fieldWidth, $labelWidth);
  1146. });
  1147. $this->builder->width($fieldWidth, $labelWidth);
  1148. return $this;
  1149. }
  1150. /**
  1151. * Set view for form.
  1152. *
  1153. * @param string $view
  1154. *
  1155. * @return $this
  1156. */
  1157. public function view($view)
  1158. {
  1159. $this->builder->view($view);
  1160. return $this;
  1161. }
  1162. /**
  1163. * @param array $vars
  1164. *
  1165. * @return $this
  1166. */
  1167. public function addVariables(array $vars)
  1168. {
  1169. $this->builder->addVariables($vars);
  1170. return $this;
  1171. }
  1172. /**
  1173. * Get or set title for form.
  1174. *
  1175. * @param string $title
  1176. *
  1177. * @return $this
  1178. */
  1179. public function title($title = null)
  1180. {
  1181. $this->builder->title($title);
  1182. return $this;
  1183. }
  1184. /**
  1185. * Tools setting for form.
  1186. *
  1187. * @param Closure|string|AbstractTool|Renderable|Action|array $callback
  1188. *
  1189. * @return $this;
  1190. */
  1191. public function tools($callback)
  1192. {
  1193. if ($callback instanceof Closure) {
  1194. $callback->call($this, $this->builder->tools());
  1195. return $this;
  1196. }
  1197. if (! is_array($callback)) {
  1198. $callback = [$callback];
  1199. }
  1200. foreach ($callback as $tool) {
  1201. $this->builder->tools()->append($tool);
  1202. }
  1203. return $this;
  1204. }
  1205. /**
  1206. * @param bool $disable
  1207. *
  1208. * @return $this
  1209. */
  1210. public function disableHeader(bool $disable = true)
  1211. {
  1212. $this->builder->disableHeader($disable);
  1213. return $this;
  1214. }
  1215. /**
  1216. * @param bool $disable
  1217. *
  1218. * @return $this
  1219. */
  1220. public function disableFooter(bool $disable = true)
  1221. {
  1222. $this->builder->disableFooter($disable);
  1223. return $this;
  1224. }
  1225. /**
  1226. * Disable form submit.
  1227. *
  1228. * @return $this
  1229. */
  1230. public function disableSubmitButton(bool $disable = true)
  1231. {
  1232. $this->builder->footer()->disableSubmit($disable);
  1233. return $this;
  1234. }
  1235. /**
  1236. * Disable form reset.
  1237. *
  1238. * @return $this
  1239. */
  1240. public function disableResetButton(bool $disable = true)
  1241. {
  1242. $this->builder->footer()->disableReset($disable);
  1243. return $this;
  1244. }
  1245. /**
  1246. * Disable View Checkbox on footer.
  1247. *
  1248. * @return $this
  1249. */
  1250. public function disableViewCheck(bool $disable = true)
  1251. {
  1252. $this->builder->footer()->disableViewCheck($disable);
  1253. return $this;
  1254. }
  1255. /**
  1256. * Disable Editing Checkbox on footer.
  1257. *
  1258. * @return $this
  1259. */
  1260. public function disableEditingCheck(bool $disable = true)
  1261. {
  1262. $this->builder->footer()->disableEditingCheck($disable);
  1263. return $this;
  1264. }
  1265. /**
  1266. * Disable Creating Checkbox on footer.
  1267. *
  1268. * @return $this
  1269. */
  1270. public function disableCreatingCheck(bool $disable = true)
  1271. {
  1272. $this->builder->footer()->disableCreatingCheck($disable);
  1273. return $this;
  1274. }
  1275. /**
  1276. * Disable `view` tool.
  1277. *
  1278. * @return $this
  1279. */
  1280. public function disableViewButton(bool $disable = true)
  1281. {
  1282. $this->builder->tools()->disableView($disable);
  1283. return $this;
  1284. }
  1285. /**
  1286. * Disable `list` tool.
  1287. *
  1288. * @return $this
  1289. */
  1290. public function disableListButton(bool $disable = true)
  1291. {
  1292. $this->builder->tools()->disableList($disable);
  1293. return $this;
  1294. }
  1295. /**
  1296. * Disable `delete` tool.
  1297. *
  1298. * @return $this
  1299. */
  1300. public function disableDeleteButton(bool $disable = true)
  1301. {
  1302. $this->builder->tools()->disableDelete($disable);
  1303. return $this;
  1304. }
  1305. /**
  1306. * Footer setting for form.
  1307. *
  1308. * @param Closure $callback
  1309. *
  1310. * @return $this
  1311. */
  1312. public function footer(Closure $callback)
  1313. {
  1314. call_user_func($callback, $this->builder->footer());
  1315. return $this;
  1316. }
  1317. /**
  1318. * Get current resource route url.
  1319. *
  1320. * @param int $slice
  1321. *
  1322. * @return string
  1323. */
  1324. public function resource($slice = -2)
  1325. {
  1326. $path = $this->resource ?: $this->request->getUri();
  1327. $segments = explode('/', trim($path, '/'));
  1328. if ($slice != 0) {
  1329. $segments = array_slice($segments, 0, $slice);
  1330. }
  1331. return url(implode('/', $segments));
  1332. }
  1333. /**
  1334. * Set resource path.
  1335. *
  1336. * @param string $resource
  1337. *
  1338. * @return $this
  1339. */
  1340. public function setResource(string $resource)
  1341. {
  1342. if ($resource) {
  1343. $this->resource = admin_url($resource);
  1344. }
  1345. return $this;
  1346. }
  1347. /**
  1348. * Render the form contents.
  1349. *
  1350. * @return string
  1351. */
  1352. public function render()
  1353. {
  1354. $this->rendering();
  1355. $this->callComposing();
  1356. return $this->builder->render();
  1357. }
  1358. /**
  1359. * Get or set input data.
  1360. *
  1361. * @param string|array $key
  1362. * @param mixed $value
  1363. *
  1364. * @return array|mixed
  1365. */
  1366. public function input($key = null, $value = null)
  1367. {
  1368. if (is_null($key)) {
  1369. return $this->inputs;
  1370. }
  1371. if (is_null($value)) {
  1372. return Arr::get($this->inputs, $key);
  1373. }
  1374. if (is_array($key)) {
  1375. $this->inputs = array_merge($this->inputs, $key);
  1376. return;
  1377. }
  1378. Arr::set($this->inputs, $key, $value);
  1379. }
  1380. /**
  1381. * @param string|array $keys
  1382. *
  1383. * @return void
  1384. */
  1385. public function deleteInput($keys)
  1386. {
  1387. Arr::forget($this->inputs, $keys);
  1388. }
  1389. /**
  1390. * @param int $width
  1391. * @param Closure $callback
  1392. *
  1393. * @return $this
  1394. */
  1395. public function block(int $width, \Closure $callback)
  1396. {
  1397. $this
  1398. ->builder
  1399. ->layout()
  1400. ->block($width, $callback);
  1401. return $this;
  1402. }
  1403. /**
  1404. * @param int|float $width
  1405. * @param Closure $callback
  1406. *
  1407. * @return $this
  1408. */
  1409. public function column($width, \Closure $callback)
  1410. {
  1411. $this->builder->layout()->onlyColumn($width, function () use ($callback) {
  1412. $callback($this);
  1413. });
  1414. return $this;
  1415. }
  1416. /**
  1417. * @return $this
  1418. */
  1419. protected function prepareDialogForm()
  1420. {
  1421. DialogForm::prepare($this);
  1422. return $this;
  1423. }
  1424. /**
  1425. * @param Closure $callback
  1426. *
  1427. * @return bool|void
  1428. */
  1429. public function inDialog(\Closure $callback = null)
  1430. {
  1431. if (! $callback) {
  1432. return DialogForm::is();
  1433. }
  1434. if (DialogForm::is()) {
  1435. $callback($this);
  1436. }
  1437. }
  1438. /**
  1439. * Create a dialog form.
  1440. *
  1441. * @param string|null $title
  1442. *
  1443. * @return DialogForm
  1444. */
  1445. public static function dialog(?string $title = null)
  1446. {
  1447. return new DialogForm($title);
  1448. }
  1449. /**
  1450. * Register custom field.
  1451. *
  1452. * @param string $abstract
  1453. * @param string $class
  1454. *
  1455. * @return void
  1456. */
  1457. public static function extend($abstract, $class)
  1458. {
  1459. static::$availableFields[$abstract] = $class;
  1460. }
  1461. /**
  1462. * @return array
  1463. */
  1464. public static function extensions()
  1465. {
  1466. return static::$availableFields;
  1467. }
  1468. /**
  1469. * Set form field alias.
  1470. *
  1471. * @param string $field
  1472. * @param string $alias
  1473. *
  1474. * @return void
  1475. */
  1476. public static function alias($field, $alias)
  1477. {
  1478. static::$fieldAlias[$alias] = $field;
  1479. }
  1480. /**
  1481. * Find field class.
  1482. *
  1483. * @param string $method
  1484. *
  1485. * @return bool|mixed
  1486. */
  1487. public static function findFieldClass($method)
  1488. {
  1489. // If alias exists.
  1490. if (isset(static::$fieldAlias[$method])) {
  1491. $method = static::$fieldAlias[$method];
  1492. }
  1493. $class = Arr::get(static::$availableFields, $method);
  1494. if (class_exists($class)) {
  1495. return $class;
  1496. }
  1497. return false;
  1498. }
  1499. /**
  1500. * Getter.
  1501. *
  1502. * @param string $name
  1503. *
  1504. * @return array|mixed
  1505. */
  1506. public function __get($name)
  1507. {
  1508. return $this->input($name);
  1509. }
  1510. /**
  1511. * Setter.
  1512. *
  1513. * @param string $name
  1514. * @param mixed $value
  1515. */
  1516. public function __set($name, $value)
  1517. {
  1518. return Arr::set($this->inputs, $name, $value);
  1519. }
  1520. /**
  1521. * Generate a Field object and add to form builder if Field exists.
  1522. *
  1523. * @param string $method
  1524. * @param array $arguments
  1525. *
  1526. * @return Field
  1527. */
  1528. public function __call($method, $arguments)
  1529. {
  1530. if (static::hasMacro($method)) {
  1531. return $this->macroCall($method, $arguments);
  1532. }
  1533. if ($className = static::findFieldClass($method)) {
  1534. $column = Arr::get($arguments, 0, '');
  1535. $element = new $className($column, array_slice($arguments, 1));
  1536. $this->pushField($element);
  1537. return $element;
  1538. }
  1539. admin_error('Error', "Field type [$method] does not exist.");
  1540. return new Field\Nullable();
  1541. }
  1542. }