Skip to content

Commit a4ddf3b

Browse files
authored
Toughening up the input text element type's validation. The old code prevented 0 from being used as a valid number. (baserow#4515)
1 parent f161790 commit a4ddf3b

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

backend/src/baserow/contrib/builder/elements/element_types.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1515,10 +1515,11 @@ def is_valid(
15151515
"""
15161516
:param element: The element we're trying to use form data in.
15171517
:param value: The form data value, which may be invalid.
1518+
:param dispatch_context: The context the element is being used in.
15181519
:return: Whether the value is valid or not for this element.
15191520
"""
15201521

1521-
if not value:
1522+
if value == "":
15221523
if element.required:
15231524
raise ValueError("The value is required")
15241525

@@ -1919,6 +1920,7 @@ def is_valid(
19191920
19201921
:param element: The choice element.
19211922
:param value: The choice value we want to validate.
1923+
:param dispatch_context: The context this element was dispatched with.
19221924
:return: The value if it is valid for this element.
19231925
"""
19241926

backend/tests/baserow/contrib/builder/elements/test_element_types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,12 +515,16 @@ def test_choice_element_import_export_formula(data_fixture):
515515
"required,type,value,result",
516516
[
517517
(True, "integer", "", ValueError),
518+
(True, "integer", 0, 0),
518519
(True, "integer", 42, 42),
520+
(True, "integer", -42, -42),
521+
(True, "integer", 3.14, 3.14),
519522
(True, "integer", "4.2", 4.2),
520523
(True, "integer", "4,2", TypeError),
521524
(True, "integer", "42", 42),
522525
(True, "integer", "horse", TypeError),
523526
(False, "integer", "", ""),
527+
(True, "email", "", ValueError),
524528
(True, "email", "foo@bar.com", "foo@bar.com"),
525529
(True, "email", "foobar.com", ValueError),
526530
(False, "email", "", ""),
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "bug",
3+
"message": "Resolved a validation bug in the data input element which considered '0' an invalid number.",
4+
"issue_origin": "github",
5+
"issue_number": null,
6+
"domain": "builder",
7+
"bullet_points": [],
8+
"created_at": "2025-12-30"
9+
}

web-frontend/modules/builder/elementTypes.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,11 +1397,10 @@ export class InputTextElementType extends FormElementType {
13971397
return 'input_text'
13981398
}
13991399

1400-
isValid(element, value, applicationContext) {
1401-
if (!value) {
1400+
isValid(element, value) {
1401+
if (value == null) {
14021402
return !element.required
14031403
}
1404-
14051404
switch (element.validation_type) {
14061405
case 'integer':
14071406
return isNumeric(value)

web-frontend/test/unit/builder/elementTypes.spec.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,14 +545,26 @@ describe('elementTypes tests', () => {
545545
})
546546
test('InputTextElementType | required | no value.', () => {
547547
const elementType = new InputTextElementType()
548-
expect(elementType.isValid({ required: true }, '')).toBe(false)
548+
expect(elementType.isValid({ required: true }, null)).toBe(false)
549549
})
550-
test('InputTextElementType | required | integer | valid value.', () => {
550+
test('InputTextElementType | required | integer | valid positive value.', () => {
551551
const elementType = new InputTextElementType()
552552
expect(
553553
elementType.isValid({ required: true, validation_type: 'integer' }, 42)
554554
).toBe(true)
555555
})
556+
test('InputTextElementType | required | integer | valid negative value.', () => {
557+
const elementType = new InputTextElementType()
558+
expect(
559+
elementType.isValid({ required: true, validation_type: 'integer' }, -42)
560+
).toBe(true)
561+
})
562+
test('InputTextElementType | required | integer | zero value.', () => {
563+
const elementType = new InputTextElementType()
564+
expect(
565+
elementType.isValid({ required: true, validation_type: 'integer' }, 0)
566+
).toBe(true)
567+
})
556568
test('InputTextElementType | required | integer | invalid value.', () => {
557569
const elementType = new InputTextElementType()
558570
expect(

0 commit comments

Comments
 (0)