Skip to content

Commit 9fb0846

Browse files
authored
Merge pull request #64 from workfloworchestrator/quick-bugfix
Quick bugfix
2 parents 14c50c8 + abdd8fb commit 9fb0846

File tree

5 files changed

+91
-42
lines changed

5 files changed

+91
-42
lines changed

backend/main.py

Lines changed: 79 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
Hidden,
2929
Choice,
3030
choice_list,
31-
unique_conlist
31+
unique_conlist,
3232
)
3333

3434
# Choice,
@@ -79,9 +79,14 @@ def example_backend_validation(val: int) -> bool:
7979

8080

8181
NumberExample = Annotated[
82-
int, Ge(1), Le(10), MultipleOf(multiple_of=3), Predicate(example_backend_validation)
82+
int,
83+
Ge(18),
84+
Le(99),
85+
MultipleOf(multiple_of=3),
86+
Predicate(example_backend_validation),
8387
]
8488

89+
8590
class DropdownChoices(Choice):
8691
_1 = ("1", "Option 1")
8792
_2 = ("2", "Option 2")
@@ -112,83 +117,118 @@ class ListChoices(Choice):
112117
_6 = ("6", "Option 6")
113118

114119

115-
class Education(BaseModel):
116-
degree: str | None
117-
year: int | None
120+
TestString = Annotated[str, Field(min_length=2, max_length=10)]
118121

119122

120-
class Person(BaseModel):
121-
name: str
122-
age: Annotated[int, Ge(18), Le(99)]
123-
education: Education
123+
class Education(BaseModel):
124+
degree: str
125+
years: int | None
126+
124127

125128
def example_list_validation(val: int) -> bool:
126129
return True
127130

128-
TestList = Annotated[
129-
unique_conlist(str, min_items=2, max_items=5), Predicate(example_backend_validation)
130-
]
131131

132132
TestExampleNumberList = Annotated[
133-
unique_conlist(NumberExample, min_items=2, max_items=5), Predicate(example_list_validation)
133+
unique_conlist(NumberExample, min_items=2, max_items=5),
134+
Predicate(example_list_validation),
134135
]
135136

137+
138+
class Education2(BaseModel):
139+
degree: str
140+
years: int | None
141+
options: ListChoices
142+
languages: TestExampleNumberList
143+
144+
145+
class Person(BaseModel):
146+
name: str
147+
age: Annotated[int, Ge(18), Le(99), MultipleOf(multiple_of=3)]
148+
education: Education
149+
150+
151+
class Person2(BaseModel):
152+
name: str
153+
age: Annotated[int, Ge(18), Le(99), MultipleOf(multiple_of=3)]
154+
education: Education2
155+
156+
136157
TestPersonList = Annotated[
137158
unique_conlist(Person, min_items=2, max_items=5), Predicate(example_list_validation)
138159
]
139160

161+
162+
def is_maurits(val: str) -> bool:
163+
if val != "Maurits":
164+
raise ValueError("Has to be Maurits!")
165+
return True
166+
167+
168+
StringExample = Annotated[str, Predicate(is_maurits)]
169+
170+
140171
@app.post("/form")
141172
async def form(form_data: list[dict] = []):
142173
def form_generator(state: State):
143174
class TestForm0(FormPage):
144-
model_config = ConfigDict(title="Form Title Page 0")
175+
model_config = ConfigDict(title="Form Title Page 1")
145176

146-
numberList: TestExampleNumberList
147-
# personList: TestPersonList = []
177+
number: NumberExample
178+
list: TestExampleNumberList
179+
# list_list: unique_conlist(TestExampleNumberList, min_items=1, max_items=5)
180+
# list_list_list: unique_conlist(
181+
# unique_conlist(Person2, min_items=1, max_items=5),
182+
# min_items=1,
183+
# max_items=2,
184+
# ) = [1, 2]
185+
test: TestString
186+
textList: unique_conlist(TestString, min_items=1, max_items=5)
187+
# numberList: TestExampleNumberList = [1, 2]
188+
person: Person2
189+
personList: unique_conlist(Person2, min_items=2, max_items=5)
148190
# ingleNumber: NumberExample
149-
150191
# number0: Annotated[int, Ge(18), Le(99)] = 17
151192

152193
form_data_0 = yield TestForm0
153194

154195
class TestForm1(FormPage):
155196
model_config = ConfigDict(title="Form Title Page 1")
156197

157-
number1: Annotated[int, Ge(18), Le(99)] = 17
198+
contact_name2: StringExample
199+
options: ListChoices
158200

159201
form_data_1 = yield TestForm1
160202

161203
class TestForm2(FormPage):
162204
model_config = ConfigDict(title="Form Title Page 2")
163205

164-
number: NumberExample = 3
165-
text: Annotated[str, Field(min_length=3, max_length=12)] = "Default text"
166-
textArea: LongText = "Text area default"
167-
divider: Divider
168-
label: Label = "Label"
169-
hidden: Hidden = "Hidden"
170-
# When there are > 3 choices a dropdown will be rendered
171-
dropdown: DropdownChoices = "2"
172-
# When there are <= 3 choices a radio group will be rendered
173-
radio: RadioChoices = "3"
174-
# checkbox: bool = True TODO: Fix validation errors on this
206+
contact_name3: StringExample
207+
age: NumberExample
175208

176-
# When there are <= 5 choices in a list a set of checkboxes are rendered
177-
# multicheckbox: choice_list(MultiCheckBoxChoices, min_items=3) = ["1", "2"]
178-
# list: choice_list(ListChoices) = [0, 1]
209+
form_data_2 = yield TestForm2
179210

180-
person: Person
211+
class TestForm3(FormPage):
212+
model_config = ConfigDict(title="Form Title Page 3")
181213

182-
form_data_2 = yield TestForm2
214+
contact_person: Person
215+
216+
form_data_3 = yield TestForm3
183217

184-
class TestSubmitForm(SubmitFormPage):
185-
model_config = ConfigDict(title="Submit Form")
218+
class TestForm5(FormPage):
219+
model_config = ConfigDict(title="Form Title Page 4")
186220

187-
name_2: str | None = None
221+
contact_person_list: TestPersonList
188222

189-
form_data_submit = yield TestSubmitForm
223+
form_data_5 = yield TestForm5
190224

191-
return form_data_0.model_dump() | form_data_1.model_dump() | form_data_2.model_dump() | form_data_submit.model_dump()
225+
return (
226+
form_data_0.model_dump()
227+
| form_data_1.model_dump()
228+
| form_data_2.model_dump()
229+
| form_data_3.model_dump()
230+
| form_data_5.model_dump()
231+
)
192232

193233
post_form(form_generator, state={}, user_inputs=form_data)
194234
return "OK!"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'pydantic-forms': patch
3+
---
4+
5+
Fixes array field value error

frontend/packages/pydantic-forms/src/components/fields/ArrayField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const ArrayField = ({ pydanticFormField }: PydanticFormElementProps) => {
7676
<div
7777
onClick={() => {
7878
append({
79-
[arrayName]: undefined,
79+
[arrayName]: arrayItem.default ?? undefined,
8080
});
8181
}}
8282
style={{

frontend/packages/pydantic-forms/src/components/fields/IntegerField.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export const IntegerField = ({
1818
onChange(value);
1919
}}
2020
disabled={disabled}
21-
value={!isObject(value) ? value : ''} // Value can be an object when it is created from an ArrayField
21+
// Value will be an object when it is added by an array field. We do this be able to add more than one empty field
22+
value={isObject(value) ? undefined : value}
2223
type="number"
2324
style={{
2425
padding: '8px',

frontend/packages/pydantic-forms/src/components/fields/TextField.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
import React from 'react';
77

8+
import { isObject } from 'lodash';
9+
810
import { PydanticFormControlledElementProps } from '@/types';
911

1012
export const TextField = ({
@@ -19,7 +21,8 @@ export const TextField = ({
1921
onChange(t.currentTarget.value);
2022
}}
2123
disabled={disabled}
22-
value={value}
24+
// Value will be an object when it is added by an array field. We do this be able to add more than one empty field
25+
value={isObject(value) ? '' : value}
2326
type="text"
2427
style={{
2528
padding: '8px',

0 commit comments

Comments
 (0)