Form.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805
  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 = clone ($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. ->redirectIf($status, $this->resource(-1))
  517. );
  518. }
  519. /**
  520. * @param \Throwable $e
  521. *
  522. * @return mixed
  523. */
  524. protected function handleException(\Throwable $e)
  525. {
  526. return Admin::handleException($e);
  527. }
  528. /**
  529. * Store a new record.
  530. *
  531. * @param array|null $data
  532. * @param string|string $redirectTo
  533. *
  534. * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse|Response
  535. */
  536. public function store(?array $data = null, $redirectTo = null)
  537. {
  538. try {
  539. if ($data) {
  540. $this->request->replace($data);
  541. }
  542. $data = $data ?: $this->request->all();
  543. if ($response = $this->beforeStore($data)) {
  544. return $this->sendResponse($response);
  545. }
  546. $this->updates = $this->prepareInsert($this->updates);
  547. $id = $this->repository->store($this);
  548. $this->builder->setResourceId($id);
  549. if (($response = $this->callSaved($id))) {
  550. return $this->sendResponse($response);
  551. }
  552. if (! $id) {
  553. return $this->sendResponse(
  554. $this->response()
  555. ->error(trans('admin.save_failed'))
  556. );
  557. }
  558. $url = $this->getRedirectUrl($id, $redirectTo);
  559. return $this->sendResponse(
  560. $this->response()
  561. ->redirectIf($url !== false, $url)
  562. ->success(trans('admin.save_succeeded'))
  563. );
  564. } catch (\Throwable $e) {
  565. $response = $this->handleException($e);
  566. if ($response instanceof Response) {
  567. return $response;
  568. }
  569. return $this->sendResponse(
  570. $this->response()
  571. ->error(trans('admin.save_failed'))
  572. ->withExceptionIf($e->getMessage(), $e)
  573. );
  574. }
  575. }
  576. /**
  577. * Before store.
  578. *
  579. * @param array $data
  580. *
  581. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|Response|void
  582. */
  583. protected function beforeStore(array $data)
  584. {
  585. $this->inputs = $data;
  586. $this->build();
  587. if (($response = $this->callSubmitted())) {
  588. return $response;
  589. }
  590. if ($response = $this->handleUploadFile($this->inputs)) {
  591. return $response;
  592. }
  593. if ($response = $this->deleteFileWhenCreating($this->inputs)) {
  594. return $response;
  595. }
  596. // Handle validation errors.
  597. if ($validationMessages = $this->validationMessages($this->inputs)) {
  598. return $this->validationErrorsResponse($validationMessages);
  599. }
  600. if (($response = $this->prepare($this->inputs))) {
  601. return $response;
  602. }
  603. }
  604. /**
  605. * Prepare input data for insert or update.
  606. *
  607. * @param array $data
  608. *
  609. * @return Response|null
  610. */
  611. protected function prepare($data = [])
  612. {
  613. $this->inputs = $this->removeIgnoredFields($data);
  614. if ($response = $this->callSaving()) {
  615. return $response;
  616. }
  617. $this->updates = $this->inputs;
  618. }
  619. /**
  620. * Remove ignored fields from input.
  621. *
  622. * @param array $input
  623. *
  624. * @return array
  625. */
  626. public function removeIgnoredFields($input)
  627. {
  628. Arr::forget($input, $this->ignored);
  629. return $input;
  630. }
  631. /**
  632. * Get or set data for insert or update.
  633. *
  634. * @param array $updates
  635. *
  636. * @return $this|array
  637. */
  638. public function updates(array $updates = null)
  639. {
  640. if ($updates === null) {
  641. return $this->updates;
  642. }
  643. $this->updates = array_merge($this->updates, $updates);
  644. return $this;
  645. }
  646. /**
  647. * Handle orderable update.
  648. *
  649. * @param int $id
  650. * @param array $input
  651. *
  652. * @return Response
  653. */
  654. protected function handleOrderable(array $input = [])
  655. {
  656. if (array_key_exists('_orderable', $input)) {
  657. $updated = $input['_orderable'] == 1
  658. ? $this->repository->moveOrderUp()
  659. : $this->repository->moveOrderDown();
  660. $message = $updated
  661. ? __('admin.update_succeeded')
  662. : __('admin.nothing_updated');
  663. return $this->sendResponse(
  664. $this->response()
  665. ->status((bool) $updated)
  666. ->message($message)
  667. );
  668. }
  669. }
  670. /**
  671. * Handle update.
  672. *
  673. * @param $id
  674. * @param array|null $data
  675. * @param string|null $redirectTo
  676. *
  677. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse||Response
  678. */
  679. public function update(
  680. $id,
  681. ?array $data = null,
  682. $redirectTo = null
  683. ) {
  684. try {
  685. if ($data) {
  686. $this->request->replace($data);
  687. }
  688. $data = $data ?: $this->request->all();
  689. if ($response = $this->beforeUpdate($id, $data)) {
  690. return $this->sendResponse($response);
  691. }
  692. $this->updates = $this->prepareUpdate($this->updates);
  693. $updated = $this->repository->update($this);
  694. if ($response = $this->callSaved($updated)) {
  695. return $this->sendResponse($response);
  696. }
  697. if (! $updated) {
  698. return $this->sendResponse(
  699. $this->response()
  700. ->error(trans('admin.update_failed'))
  701. );
  702. }
  703. $url = $this->getRedirectUrl($id, $redirectTo);
  704. return $this->sendResponse(
  705. $this->response()
  706. ->success(trans('admin.update_succeeded'))
  707. ->redirectIf($url !== false, $url)
  708. ->refreshIf($url === false)
  709. );
  710. } catch (\Throwable $e) {
  711. $response = $this->handleException($e);
  712. if ($response instanceof Response) {
  713. return $response;
  714. }
  715. return $this->sendResponse(
  716. $this->response()
  717. ->error(trans('admin.update_failed'))
  718. ->withExceptionIf($e->getMessage(), $e)
  719. );
  720. }
  721. }
  722. /**
  723. * Before update.
  724. *
  725. * @param array $data
  726. *
  727. * @return Response|void
  728. */
  729. protected function beforeUpdate($id, array &$data)
  730. {
  731. $this->builder->setResourceId($id);
  732. $this->builder->mode(Builder::MODE_EDIT);
  733. $this->inputs = $data;
  734. $this->model($this->repository->updating($this));
  735. $this->build();
  736. $this->setFieldOriginalValue();
  737. if ($response = $this->callSubmitted()) {
  738. return $response;
  739. }
  740. if ($uploadFileResponse = $this->handleUploadFile($this->inputs)) {
  741. return $uploadFileResponse;
  742. }
  743. $isEditable = $this->isEditable($this->inputs);
  744. $this->inputs = $this->handleEditable($this->inputs);
  745. $this->inputs = $this->handleFileDelete($this->inputs);
  746. $this->inputs = $this->handleHasManyValues($this->inputs);
  747. if ($response = $this->handleOrderable($this->inputs)) {
  748. return $response;
  749. }
  750. // Handle validation errors.
  751. if ($validationMessages = $this->validationMessages($this->inputs)) {
  752. return $this->validationErrorsResponse(
  753. $isEditable ? Arr::dot($validationMessages->toArray()) : $validationMessages
  754. );
  755. }
  756. if ($response = $this->prepare($this->inputs)) {
  757. return $response;
  758. }
  759. }
  760. /**
  761. * @param array $inputs
  762. *
  763. * @return array
  764. */
  765. protected function handleHasManyValues(array $inputs)
  766. {
  767. foreach ($inputs as $column => &$input) {
  768. $field = $this->builder()->field($column);
  769. if (is_array($input) && $field instanceof Field\HasMany) {
  770. $keyName = $field->getKeyName();
  771. foreach ($input as $k => &$v) {
  772. if (! array_key_exists($keyName, $v)) {
  773. $v[$keyName] = $k;
  774. }
  775. if (empty($v[NestedForm::REMOVE_FLAG_NAME])) {
  776. $v[NestedForm::REMOVE_FLAG_NAME] = null;
  777. }
  778. }
  779. }
  780. }
  781. return $inputs;
  782. }
  783. /**
  784. * @param $key
  785. * @param $redirectTo
  786. *
  787. * @return string|null
  788. */
  789. public function getRedirectUrl($key, $redirectTo = null)
  790. {
  791. if ($redirectTo) {
  792. return $redirectTo;
  793. }
  794. $resourcesPath = $this->isCreating() ? $this->resource(0) : $this->resource(-1);
  795. if ($this->request->get('after-save') == 1) {
  796. // continue editing
  797. if ($this->builder->isEditing()) {
  798. return false;
  799. }
  800. return rtrim($resourcesPath, '/')."/{$key}/edit";
  801. }
  802. if ($this->request->get('after-save') == 2) {
  803. // continue creating
  804. return rtrim($resourcesPath, '/').'/create';
  805. }
  806. if ($this->request->get('after-save') == 3) {
  807. // view resource
  808. return rtrim($resourcesPath, '/')."/{$key}";
  809. }
  810. return $this->request->get(Builder::PREVIOUS_URL_KEY) ?: ($this->getCurrentUrl() ?: $resourcesPath);
  811. }
  812. /**
  813. * Check if request is from editable.
  814. *
  815. * @param array $input
  816. *
  817. * @return bool
  818. */
  819. protected function isEditable(array $input = [])
  820. {
  821. return array_key_exists('_editable', $input);
  822. }
  823. /**
  824. * Handle editable update.
  825. *
  826. * @param array $input
  827. *
  828. * @return array
  829. */
  830. protected function handleEditable(array $input = [])
  831. {
  832. if (array_key_exists('_editable', $input)) {
  833. $name = $input['name'];
  834. $value = $input['value'];
  835. Arr::forget($input, ['pk', 'value', 'name']);
  836. Arr::set($input, $name, $value);
  837. }
  838. return $input;
  839. }
  840. /**
  841. * Prepare input data for update.
  842. *
  843. * @param array $updates
  844. *
  845. * @return array
  846. */
  847. public function prepareUpdate(array $updates)
  848. {
  849. $prepared = [];
  850. /** @var Field $field */
  851. foreach ($this->builder->fields() as $field) {
  852. $columns = $field->column();
  853. // If column not in input array data, then continue.
  854. if (! Arr::has($updates, $columns)) {
  855. continue;
  856. }
  857. $value = $this->getDataByColumn($updates, $columns);
  858. $value = $field->prepare($value);
  859. if (is_array($columns)) {
  860. foreach ($columns as $name => $column) {
  861. Arr::set($prepared, $column, $value[$name]);
  862. }
  863. } elseif (is_string($columns)) {
  864. Arr::set($prepared, $columns, $value);
  865. }
  866. }
  867. return $prepared;
  868. }
  869. /**
  870. * Prepare input data for insert.
  871. *
  872. * @param $inserts
  873. *
  874. * @return array
  875. */
  876. public function prepareInsert($inserts)
  877. {
  878. Helper::prepareHasOneRelation($this->builder->fields(), $inserts);
  879. foreach ($inserts as $column => $value) {
  880. if (is_null($field = $this->field($column))) {
  881. unset($inserts[$column]);
  882. continue;
  883. }
  884. $inserts[$column] = $field->prepare($value);
  885. }
  886. $prepared = [];
  887. foreach ($inserts as $key => $value) {
  888. Arr::set($prepared, $key, $value);
  889. }
  890. return $prepared;
  891. }
  892. /**
  893. * Ignore fields to save.
  894. *
  895. * @param string|array $fields
  896. *
  897. * @return $this
  898. */
  899. public function ignore($fields)
  900. {
  901. $this->ignored = Arr::flatten(
  902. array_merge($this->ignored, (array) $fields)
  903. );
  904. return $this;
  905. }
  906. /**
  907. * @param $keys
  908. *
  909. * @return $this
  910. */
  911. public function forgetIgnored($keys)
  912. {
  913. Arr::forget($this->ignored, $keys);
  914. return $this;
  915. }
  916. /**
  917. * Get primary key name of model.
  918. *
  919. * @return string
  920. */
  921. public function keyName()
  922. {
  923. if (! $this->repository) {
  924. return 'id';
  925. }
  926. return $this->repository->getKeyName();
  927. }
  928. /**
  929. * @return string|void
  930. */
  931. public function createdAtColumn()
  932. {
  933. if (! $this->repository) {
  934. return;
  935. }
  936. return $this->repository->getCreatedAtColumn();
  937. }
  938. /**
  939. * @return string|void
  940. */
  941. public function updatedAtColumn()
  942. {
  943. if (! $this->repository) {
  944. return;
  945. }
  946. return $this->repository->getUpdatedAtColumn();
  947. }
  948. /**
  949. * @param array $data
  950. * @param string|array $columns
  951. *
  952. * @return array|mixed
  953. */
  954. protected function getDataByColumn($data, $columns)
  955. {
  956. if (is_string($columns)) {
  957. return Arr::get($data, $columns);
  958. }
  959. if (is_array($columns)) {
  960. $value = [];
  961. foreach ($columns as $name => $column) {
  962. if (! Arr::has($data, $column)) {
  963. continue;
  964. }
  965. $value[$name] = Arr::get($data, $column);
  966. }
  967. return $value;
  968. }
  969. }
  970. /**
  971. * Set original data for each field.
  972. *
  973. * @return void
  974. */
  975. protected function setFieldOriginalValue()
  976. {
  977. $data = $this->model()->toArray();
  978. $this->builder->fields()->each(function (Field $field) use ($data) {
  979. $field->setOriginal($data);
  980. });
  981. }
  982. /**
  983. * @example
  984. * $form->if(true)->then(function (Form $form) {
  985. * $form->text('name');
  986. * });
  987. *
  988. * $form->if(function (Form $form) {
  989. * return $form->model()->id > 5;
  990. * })->then(function (Form $form) {
  991. * $form->text('name');
  992. * });
  993. *
  994. * $form->if(true)->now(function (Form $form) {
  995. * $form->text('name');
  996. * });
  997. *
  998. * $form->if(true)->creating(function (Form $form) {});
  999. *
  1000. * $form->if(true)->removeField('name');
  1001. *
  1002. * @param bool|\Closure $condition
  1003. *
  1004. * @return Condition
  1005. */
  1006. public function if($condition)
  1007. {
  1008. return $this->conditions[] = new Condition($condition, $this);
  1009. }
  1010. /**
  1011. * @return void
  1012. */
  1013. protected function rendering()
  1014. {
  1015. $this->build();
  1016. if ($this->isCreating()) {
  1017. $this->callCreating();
  1018. return;
  1019. }
  1020. $this->fillFields($this->model()->toArray());
  1021. $this->callEditing();
  1022. }
  1023. /**
  1024. * @param array $data
  1025. *
  1026. * @return void
  1027. */
  1028. public function fillFields(array $data)
  1029. {
  1030. $this->builder->fields()->each(function (Field $field) use ($data) {
  1031. if (! in_array($field->column(), $this->ignored, true)) {
  1032. $field->fill($data);
  1033. }
  1034. });
  1035. }
  1036. /**
  1037. * @return void
  1038. */
  1039. protected function build()
  1040. {
  1041. if ($callback = $this->callback) {
  1042. $callback($this);
  1043. }
  1044. foreach ($this->conditions as $condition) {
  1045. $condition->process();
  1046. }
  1047. }
  1048. /**
  1049. * Get validation messages.
  1050. *
  1051. * @param array $input
  1052. *
  1053. * @return MessageBag|bool
  1054. */
  1055. public function validationMessages($input)
  1056. {
  1057. $failedValidators = [];
  1058. /** @var Field $field */
  1059. foreach ($this->builder->fields() as $field) {
  1060. if (! $validator = $field->getValidator($input)) {
  1061. continue;
  1062. }
  1063. if (($validator instanceof Validator) && ! $validator->passes()) {
  1064. $failedValidators[] = [$field, $validator];
  1065. }
  1066. }
  1067. $message = $this->mergeValidationMessages($failedValidators);
  1068. if ($message->any() && $this->builder->isCreating()) {
  1069. $this->deleteFiles($input, true);
  1070. }
  1071. return $message->any() ? $message : false;
  1072. }
  1073. /**
  1074. * @param string|array|MessageProvider $column
  1075. * @param string|array $messages
  1076. *
  1077. * @return $this
  1078. */
  1079. public function responseValidationMessages($column, $messages = null)
  1080. {
  1081. if ($column instanceof MessageProvider) {
  1082. return $this->responseValidationMessages($column->getMessageBag()->getMessages());
  1083. }
  1084. if (! $this->validationMessages) {
  1085. $this->validationMessages = new MessageBag();
  1086. }
  1087. if (! $column) {
  1088. return $this;
  1089. }
  1090. if (is_array($column)) {
  1091. foreach ($column as $k => &$v) {
  1092. $v = (array) $v;
  1093. }
  1094. $this->validationMessages->merge($column);
  1095. } elseif ($messages) {
  1096. $this->validationMessages->merge([$column => (array) $messages]);
  1097. }
  1098. return $this;
  1099. }
  1100. /**
  1101. * Merge validation messages from input validators.
  1102. *
  1103. * @param array $validators
  1104. *
  1105. * @return MessageBag
  1106. */
  1107. protected function mergeValidationMessages($validators)
  1108. {
  1109. $messageBag = new MessageBag();
  1110. foreach ($validators as $value) {
  1111. [$field, $validator] = $value;
  1112. $messageBag = $messageBag->merge($field->formatValidatorMessages($validator->messages()));
  1113. }
  1114. if ($this->validationMessages) {
  1115. return $messageBag->merge($this->validationMessages);
  1116. }
  1117. return $messageBag;
  1118. }
  1119. /**
  1120. * Get or set action for form.
  1121. *
  1122. * @param string|null $action
  1123. *
  1124. * @return $this|string
  1125. */
  1126. public function action($action = null)
  1127. {
  1128. $value = $this->builder->action($action);
  1129. if ($action === null) {
  1130. return $value;
  1131. }
  1132. return $this;
  1133. }
  1134. /**
  1135. * Set field and label width in current form.
  1136. *
  1137. * @param int $fieldWidth
  1138. * @param int $labelWidth
  1139. *
  1140. * @return $this
  1141. */
  1142. public function width($fieldWidth = 8, $labelWidth = 2)
  1143. {
  1144. $this->builder->fields()->each(function ($field) use ($fieldWidth, $labelWidth) {
  1145. /* @var Field $field */
  1146. $field->width($fieldWidth, $labelWidth);
  1147. });
  1148. $this->builder->width($fieldWidth, $labelWidth);
  1149. return $this;
  1150. }
  1151. /**
  1152. * Set view for form.
  1153. *
  1154. * @param string $view
  1155. *
  1156. * @return $this
  1157. */
  1158. public function view($view)
  1159. {
  1160. $this->builder->view($view);
  1161. return $this;
  1162. }
  1163. /**
  1164. * Get or set title for form.
  1165. *
  1166. * @param string $title
  1167. *
  1168. * @return $this
  1169. */
  1170. public function title($title = null)
  1171. {
  1172. $this->builder->title($title);
  1173. return $this;
  1174. }
  1175. /**
  1176. * Tools setting for form.
  1177. *
  1178. * @param Closure|string|AbstractTool|Renderable|Action|array $callback
  1179. *
  1180. * @return $this;
  1181. */
  1182. public function tools($callback)
  1183. {
  1184. if ($callback instanceof Closure) {
  1185. $callback->call($this, $this->builder->tools());
  1186. return $this;
  1187. }
  1188. if (! is_array($callback)) {
  1189. $callback = [$callback];
  1190. }
  1191. foreach ($callback as $tool) {
  1192. $this->builder->tools()->append($tool);
  1193. }
  1194. return $this;
  1195. }
  1196. /**
  1197. * @param bool $disable
  1198. *
  1199. * @return $this
  1200. */
  1201. public function disableHeader(bool $disable = true)
  1202. {
  1203. $this->builder->disableHeader($disable);
  1204. return $this;
  1205. }
  1206. /**
  1207. * @param bool $disable
  1208. *
  1209. * @return $this
  1210. */
  1211. public function disableFooter(bool $disable = true)
  1212. {
  1213. $this->builder->disableFooter($disable);
  1214. return $this;
  1215. }
  1216. /**
  1217. * Disable form submit.
  1218. *
  1219. * @return $this
  1220. */
  1221. public function disableSubmitButton(bool $disable = true)
  1222. {
  1223. $this->builder->footer()->disableSubmit($disable);
  1224. return $this;
  1225. }
  1226. /**
  1227. * Disable form reset.
  1228. *
  1229. * @return $this
  1230. */
  1231. public function disableResetButton(bool $disable = true)
  1232. {
  1233. $this->builder->footer()->disableReset($disable);
  1234. return $this;
  1235. }
  1236. /**
  1237. * Disable View Checkbox on footer.
  1238. *
  1239. * @return $this
  1240. */
  1241. public function disableViewCheck(bool $disable = true)
  1242. {
  1243. $this->builder->footer()->disableViewCheck($disable);
  1244. return $this;
  1245. }
  1246. /**
  1247. * Disable Editing Checkbox on footer.
  1248. *
  1249. * @return $this
  1250. */
  1251. public function disableEditingCheck(bool $disable = true)
  1252. {
  1253. $this->builder->footer()->disableEditingCheck($disable);
  1254. return $this;
  1255. }
  1256. /**
  1257. * Disable Creating Checkbox on footer.
  1258. *
  1259. * @return $this
  1260. */
  1261. public function disableCreatingCheck(bool $disable = true)
  1262. {
  1263. $this->builder->footer()->disableCreatingCheck($disable);
  1264. return $this;
  1265. }
  1266. /**
  1267. * Disable `view` tool.
  1268. *
  1269. * @return $this
  1270. */
  1271. public function disableViewButton(bool $disable = true)
  1272. {
  1273. $this->builder->tools()->disableView($disable);
  1274. return $this;
  1275. }
  1276. /**
  1277. * Disable `list` tool.
  1278. *
  1279. * @return $this
  1280. */
  1281. public function disableListButton(bool $disable = true)
  1282. {
  1283. $this->builder->tools()->disableList($disable);
  1284. return $this;
  1285. }
  1286. /**
  1287. * Disable `delete` tool.
  1288. *
  1289. * @return $this
  1290. */
  1291. public function disableDeleteButton(bool $disable = true)
  1292. {
  1293. $this->builder->tools()->disableDelete($disable);
  1294. return $this;
  1295. }
  1296. /**
  1297. * Footer setting for form.
  1298. *
  1299. * @param Closure $callback
  1300. *
  1301. * @return $this
  1302. */
  1303. public function footer(Closure $callback)
  1304. {
  1305. call_user_func($callback, $this->builder->footer());
  1306. return $this;
  1307. }
  1308. /**
  1309. * Get current resource route url.
  1310. *
  1311. * @param int $slice
  1312. *
  1313. * @return string
  1314. */
  1315. public function resource($slice = -2)
  1316. {
  1317. $path = $this->resource ?: $this->request->getUri();
  1318. $segments = explode('/', trim($path, '/'));
  1319. if ($slice != 0) {
  1320. $segments = array_slice($segments, 0, $slice);
  1321. }
  1322. return url(implode('/', $segments));
  1323. }
  1324. /**
  1325. * Set resource path.
  1326. *
  1327. * @param string $resource
  1328. *
  1329. * @return $this
  1330. */
  1331. public function setResource(string $resource)
  1332. {
  1333. if ($resource) {
  1334. $this->resource = admin_url($resource);
  1335. }
  1336. return $this;
  1337. }
  1338. /**
  1339. * Render the form contents.
  1340. *
  1341. * @return string
  1342. */
  1343. public function render()
  1344. {
  1345. $this->rendering();
  1346. $this->callComposing();
  1347. return $this->builder->render();
  1348. }
  1349. /**
  1350. * Get or set input data.
  1351. *
  1352. * @param string $key
  1353. * @param null $value
  1354. *
  1355. * @return array|mixed
  1356. */
  1357. public function input($key = null, $value = null)
  1358. {
  1359. if (is_null($key)) {
  1360. return $this->inputs;
  1361. }
  1362. if (is_null($value)) {
  1363. return Arr::get($this->inputs, $key);
  1364. }
  1365. return Arr::set($this->inputs, $key, $value);
  1366. }
  1367. /**
  1368. * @param string|array $keys
  1369. *
  1370. * @return void
  1371. */
  1372. public function deleteInput($keys)
  1373. {
  1374. Arr::forget($this->inputs, $keys);
  1375. }
  1376. /**
  1377. * @param int $width
  1378. * @param Closure $callback
  1379. *
  1380. * @return $this
  1381. */
  1382. public function block(int $width, \Closure $callback)
  1383. {
  1384. $this
  1385. ->builder
  1386. ->layout()
  1387. ->block($width, $callback);
  1388. return $this;
  1389. }
  1390. /**
  1391. * @param int|float $width
  1392. * @param Closure $callback
  1393. *
  1394. * @return $this
  1395. */
  1396. public function column($width, \Closure $callback)
  1397. {
  1398. $this->builder->layout()->onlyColumn($width, function () use ($callback) {
  1399. $callback($this);
  1400. });
  1401. return $this;
  1402. }
  1403. /**
  1404. * @return $this
  1405. */
  1406. protected function prepareDialogForm()
  1407. {
  1408. DialogForm::prepare($this);
  1409. return $this;
  1410. }
  1411. /**
  1412. * @param Closure $callback
  1413. *
  1414. * @return bool|void
  1415. */
  1416. public function inDialog(\Closure $callback = null)
  1417. {
  1418. if (! $callback) {
  1419. return DialogForm::is();
  1420. }
  1421. if (DialogForm::is()) {
  1422. $callback($this);
  1423. }
  1424. }
  1425. /**
  1426. * Create a dialog form.
  1427. *
  1428. * @param string|null $title
  1429. *
  1430. * @return DialogForm
  1431. */
  1432. public static function dialog(?string $title = null)
  1433. {
  1434. return new DialogForm($title);
  1435. }
  1436. /**
  1437. * Register custom field.
  1438. *
  1439. * @param string $abstract
  1440. * @param string $class
  1441. *
  1442. * @return void
  1443. */
  1444. public static function extend($abstract, $class)
  1445. {
  1446. static::$availableFields[$abstract] = $class;
  1447. }
  1448. /**
  1449. * @return array
  1450. */
  1451. public static function extensions()
  1452. {
  1453. return static::$availableFields;
  1454. }
  1455. /**
  1456. * Set form field alias.
  1457. *
  1458. * @param string $field
  1459. * @param string $alias
  1460. *
  1461. * @return void
  1462. */
  1463. public static function alias($field, $alias)
  1464. {
  1465. static::$fieldAlias[$alias] = $field;
  1466. }
  1467. /**
  1468. * Find field class.
  1469. *
  1470. * @param string $method
  1471. *
  1472. * @return bool|mixed
  1473. */
  1474. public static function findFieldClass($method)
  1475. {
  1476. // If alias exists.
  1477. if (isset(static::$fieldAlias[$method])) {
  1478. $method = static::$fieldAlias[$method];
  1479. }
  1480. $class = Arr::get(static::$availableFields, $method);
  1481. if (class_exists($class)) {
  1482. return $class;
  1483. }
  1484. return false;
  1485. }
  1486. /**
  1487. * Getter.
  1488. *
  1489. * @param string $name
  1490. *
  1491. * @return array|mixed
  1492. */
  1493. public function __get($name)
  1494. {
  1495. return $this->input($name);
  1496. }
  1497. /**
  1498. * Setter.
  1499. *
  1500. * @param string $name
  1501. * @param mixed $value
  1502. */
  1503. public function __set($name, $value)
  1504. {
  1505. return Arr::set($this->inputs, $name, $value);
  1506. }
  1507. /**
  1508. * Generate a Field object and add to form builder if Field exists.
  1509. *
  1510. * @param string $method
  1511. * @param array $arguments
  1512. *
  1513. * @return Field
  1514. */
  1515. public function __call($method, $arguments)
  1516. {
  1517. if (static::hasMacro($method)) {
  1518. return $this->macroCall($method, $arguments);
  1519. }
  1520. if ($className = static::findFieldClass($method)) {
  1521. $column = Arr::get($arguments, 0, '');
  1522. $element = new $className($column, array_slice($arguments, 1));
  1523. $this->pushField($element);
  1524. return $element;
  1525. }
  1526. admin_error('Error', "Field type [$method] does not exist.");
  1527. return new Field\Nullable();
  1528. }
  1529. }