Form.php 42 KB

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