getFields(); // 获取表单字段填充的值 if (method_exists($this, 'getFieldsValue')) { $this->fieldsValue = $this->getFieldsValue(); $fields = $this->setFieldsValue($fields); } return $this->rule($fields); } /** * 设置 fields values * * @time 2021年03月12日 * @param $fields * @return mixed */ protected function setFieldsValue($fields) { foreach ($fields as $field) { $name = $field->getName(); if (isset($this->fieldsValue[$name])) { $field->value($this->fieldsValue[$name]); } } return $fields; } /** * 获取 form fields * * @time 2021年03月12日 * @return array */ protected function getFields(): array { if ($this->primaryKeyField) { return array_merge($this->fields(), [ self::hidden($this->primaryKeyField, $this->primaryKeyValue) ]); } return $this->fields(); } /** * form rule * * @time 2021年03月06日 * @param array $rules * @return array */ public function rule(array $rules): array { try{ return Elm::createForm('', $rules)->formRule(); } catch (FormBuilderException $e) { throw new FailedException('Form Created Failed: ' .$e->getMessage()); } } /** * 上传图片地址 * * @time 2021年03月03日 * @return string */ public static function uploadImageUrl(): string { return env('app.domain') . '/upload/image'; } /** * 上传文件地址 * * @time 2021年03月10日 * @return string */ public static function uploadFileUrl(): string { return env('app.domain') . '/upload/file'; } /** * auth token * * @time 2021年04月11日 * @return string[] */ public static function authorization(): array { return [ 'authorization' => 'Bearer ' . request()->user()->remember_token, ]; } /** * 上传图片 * * @time 2021年03月03日 * @param $title * @param string $value * @return mixed */ public static function image(string $title, string $field = 'image', string $value = ''): Upload { return self::uploadImage($field, $title, self::uploadImageUrl(), $value) ->uploadName($field) ->data(['none' => '']) ->headers(self::authorization()); } /** * 多图 * * @time 2021年03月03日 * @param $title * @param array $value * @return mixed */ public static function images(string $title, $field = 'images', array $value = []): Upload { return self::uploadImages($field, $title, self::uploadImageUrl(), $value) ->uploadName($field) ->data(['none' => '']) ->headers(self::authorization()); } /** * 上传文件 * * @time 2021年03月10日 * @param string $title * @param string $value * @return Upload */ public static function file(string $title, $field = 'file', string $value = ''): Upload { return self::uploadFile($field, $title, self::uploadFileUrl(), $value) ->uploadName($field) ->data(['none' => '']) ->headers(self::authorization()); } /** * 上传多文件 * * @time 2021年03月10日 * @param string $title * @param array $value * @return Upload */ public static function files(string $title, $field = 'files',array $value = []): Upload { return self::uploadFiles($field, $title, self::uploadFileUrl(), $value) ->uploadName($field) ->data(['none' => '']) ->headers(self::authorization()); } /** * options * * @time 2021年03月24日 * @return FormOptions */ public static function options(): FormOptions { return new FormOptions(); } /** * props * * @time 2021年03月30日 * @param $label * @param string $value * @param array $extra * @param array $data * @return array */ public static function props($label, $value = 'id', array $extra = [], array $data = []): array { $props = [ 'props' => array_merge([ 'label' => $label, 'value' => $value, ], $extra) ]; if (count($data)) { $props['data'] = $data; } return $props; } /** * 不需要 props * * @time 2021年03月30日 * @param $label * @param string $value * @param array $extra * @return array */ public static function treeProps($label, $value = 'id', array $extra = []): array { return array_merge([ 'label' => $label, 'value' => $value, ], $extra); } /** * col * * @time 2021年03月30日 * @param int $col * @return array */ public static function col(int $col): array { return ['span' => $col]; } /** * 编辑器 * * @time 2021年04月08日 * @param $field * @param $title * @param string $value * @return Editor */ public static function editor(string $field, string $title, string $value = ''): Editor { return new Editor($field, $title, $value); } }