@@ -36,6 +36,8 @@ class Form extends Model implements \Asseco\CustomFields\App\Contracts\Form
36
36
'definition ' => 'array ' ,
37
37
];
38
38
39
+ private array $ _customFieldNames = [];
40
+
39
41
protected static function newFactory ()
40
42
{
41
43
return FormFactory::new ();
@@ -70,14 +72,16 @@ protected function relateCustomFieldsFromDefinition()
70
72
{
71
73
$ components = Arr::get ($ this ->definition , 'components ' , []);
72
74
75
+ $ this ->_customFieldNames = [];
73
76
$ this ->extractCustomFields ($ components );
77
+ $ this ->relateCustomFields ();
74
78
}
75
79
76
80
protected function extractCustomFields (array $ components ): void
77
81
{
78
82
foreach ($ components as $ componentKey => $ component ) {
79
83
if ($ componentKey === 'key ' ) {
80
- $ this ->relateCustomField ( $ component) ;
84
+ $ this ->_customFieldNames [] = $ component ;
81
85
}
82
86
83
87
if (!is_array ($ component )) {
@@ -103,6 +107,21 @@ protected function relateCustomField(string $key): void
103
107
}
104
108
}
105
109
110
+ protected function relateCustomFields (): void
111
+ {
112
+ if (empty ($ this ->_customFieldNames )) {
113
+ return ;
114
+ }
115
+
116
+ /** @var CustomField $customFieldClass */
117
+ $ customFieldClass = app (CustomField::class);
118
+ $ customFieldIds = $ customFieldClass ::query ()->whereIn ('name ' , array_unique ($ this ->_customFieldNames ))->get ()->pluck ('id ' );
119
+
120
+ if (!empty ($ customFieldIds )) {
121
+ $ this ->customFields ()->attach ($ customFieldIds );
122
+ }
123
+ }
124
+
106
125
public function customFields (): BelongsToMany
107
126
{
108
127
return $ this ->belongsToMany (get_class (app (CustomField::class)))->withTimestamps ();
@@ -112,7 +131,7 @@ public function customFields(): BelongsToMany
112
131
* @param array $formData
113
132
* @return array
114
133
*
115
- * @throws Exception
134
+ * @throws Exception|\Throwable
116
135
*/
117
136
public function validate (array $ formData ): array
118
137
{
0 commit comments