Adding new fields to the form config panel and making them required. #161
Replies: 1 comment 1 reply
|
Hi Ben, Thanks for bringing this up – here’s a demo: A bit of background on how this currently works: By default, the builder does not validate config fields internally. Validation is expected to happen externally in the This is also why there’s currently no first-class API for adding validation rules to config fields. Internally, config panel element templates intentionally strip out the error rendering parts to reduce component tree size and overhead. That said, there’s been growing demand for this functionality, so it’s definitely something to consider for a future release. For now, it can still be achieved with a small workaround. First, restore the original element layout so validation errors can actually be displayed: import {
defineConfig,
sections,
separators,
BaseFormField,
} from '@vueform/builder';
import config from './vueform.config';
import { markRaw } from 'vue';
const ElementLayout = markRaw(config.theme.templates.ElementLayout);
const FormTemplateTypeIdField = class extends BaseFormField {
name = 'FormTemplateTypeIdField';
get schema() {
return {
form_template_type_id: {
type: 'select',
label: 'Form Type',
columns: { label: 5 },
rules: ['required'],
templates: {
ElementLayout, // <----- HERE
},
// ...
},
};
}
};Then override the const FormTemplateTypeIdField = class extends BaseFormField {
name = 'FormTemplateTypeIdField';
get schema() {
return {
// ...
};
}
// Prevent empty values from replacing
// an already valid value.
save(value, old, key, el$) {
if (!value) return;
this.update({ [key]: value });
}
};Hope this helps for now. Let me know if you run into any issues with the workaround or if something still behaves unexpectedly on your side. |

Hi Ben,
Thanks for bringing this up – here’s a demo:
https://stackblitz.com/edit/github-ys6hcrvh?file=builder.config.js
A bit of background on how this currently works:
By default, the builder does not validate config fields internally. Validation is expected to happen externally in the
@saveevent. For example, imagine the builder loads with a required form-level setting already empty. If the user never interacts with that field, there would otherwise be no indication that it needs to be filled in. Because of this, the intended flow is to validate the received data inside your@savehandler before persisting it.This is also why there’s currently no first-class API for adding validation …