Skip to content

Commit

Permalink
add type validation in number field
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Jul 12, 2024
1 parent 02669c6 commit 39ab774
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/components/fields/NumberField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
:id="'input-' + fieldKey"
class="number-input-field"
:value="value ?? ''"
type="number"
type="text"
:label-outside="true"
:title="field.name"
:placeholder="field.description || t('assistant','Type some number')"
:error="!isValid"
:helper-text="isValid ? '' : t('assistant', 'The current value is not a number')"
@update:value="onUpdateValue" />
</div>
</template>
Expand Down Expand Up @@ -50,6 +52,9 @@ export default {
},
computed: {
isValid() {
return this.value === null || this.value === '' || typeof this.value === 'number'
},
},
watch: {
Expand All @@ -60,11 +65,11 @@ export default {
methods: {
onUpdateValue(value) {
const intValue = parseInt(value)
if (isNaN(intValue)) {
this.$emit('update:value', null)
const numberValue = parseFloat(value)
if (isNaN(numberValue)) {
this.$emit('update:value', value)
} else {
this.$emit('update:value', intValue)
this.$emit('update:value', numberValue)
}
},
},
Expand Down

0 comments on commit 39ab774

Please sign in to comment.