简体   繁体   English

在表单第一次验证错误时展开手风琴

[英]Expand accordion upon first validation error of a form

I have an accordion in react-bootstrap and a form with validation in it.我在 react-bootstrap 中有一个手风琴和一个带有验证的表单。 Some form fields are on the first page of the accordion and some are on the second page.一些表单字段在手风琴的第一页上,一些在第二页上。 Upon validation error how can I expand the accordion page that contains the first invalid field?验证错误后,如何展开包含第一个无效字段的手风琴页面?

I managed to get the first invalid form field, but I am unable to determine which accordion page contains this field.我设法获得了第一个无效的表单字段,但我无法确定哪个手风琴页面包含该字段。

If you have the first invalid form field then you can use closest to find it's accordion card via the class 'collapse'.如果您有第一个无效的表单字段,那么您可以使用closest通过 class 'collapse' 找到它的手风琴卡。 Add 'show' to its classes so it will open then you can scroll to the invalid element via scrollIntoView将“显示”添加到它的类中,这样它就会打开,然后您可以通过scrollIntoView滚动到无效元素

 handleSubmit = (event) => { const form = event.currentTarget; if (form.checkValidity() === false) { let firstInvalidFieldInTheForm = form.querySelectorAll(':invalid')[0]; let ancestorAccordion = firstInvalidFieldInTheForm.closest(".collapse"); ancestorAccordion.classList.add("show") firstInvalidFieldInTheForm.scrollIntoView(); event.preventDefault(); event.stopPropagation(); }else{ this.handleSaveButtonClick(); } this.setState({validated: true}); };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM