【发布时间】:2019-09-25 14:28:57
【问题描述】:
只有部分 HTMLElement 支持约束验证 API(例如 HTMLButtonElement)。我想创建一个自定义的 web 组件,它也支持约束验证 api。
下面给出了期望结果的示例:
<form>
<input name="title" required>
<custom-form-component></custom-form-component>
</form>
<script>
const form = document.querySelector('form');
form.checkValidity();
</script>
custom-form-component 可以在自身上调用setCustomValidity(基于相应的用户输入上下文)并验证自己的真假。因此,对form.checkValidity() 的调用应该根据custom-form-component 的结果返回true 或false。
正如我从文档(例如在 MDN 上)中发现的,只有某些元素可以附加影子根。表单元素是不可能的。但是,只有表单元素支持约束验证 api。
具体问题: 如何实现支持约束验证 api 的自定义 Web 组件?
【问题讨论】:
标签: web-component shadow-dom native-web-component constraint-validation