Form.php 43 KB

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