为什么setCustomValidity()
在React中无效?我错过了什么吗?使用vanillia HTML和JS工作正常。或者还有其他方法可以轻松地制作与setCustomValidity
类似的内容?
反应代码:
class Form extends React.Component {
formVal(e) {
e.target.setCustomValidity("Test_1");
}
formVal2() {
let inpt = document.getElementById("input");
inpt.target.setCustomValidity("TEST_2");
}
render() {
return (
<div>
<form>
<input
id="input"
type="datetime-local"
onBlur={this.formVal}
/>
</form>
<button onClick={this.formVal2}>Click </button>
</div>
);
}
}
没有React:
<form>
<input type="email" id="mail" name="mail">
<button onclick="test()">Submit</button>
</form>
// JS
var input = document.getElementById("mail");
function test() {
input.setCustomValidity("test")
}
答案 0 :(得分:6)
setCustomValidity
不会立即触发验证,您必须拥有表单提交事件才能触发它或致电checkValidity
。我已经更新了您的CodePen,以展示如何在React中实现这一目标,它也包含在下面。
class Form extends React.Component {
onChange(e) {
let date = new Date(Date.parse(e.target.value));
if (date > new Date()) {
e.target.setCustomValidity("Please select a date in the past.");
} else {
e.target.setCustomValidity("");
}
}
render() {
return (
<div>
<form>
<input
id="input"
type="datetime-local"
onChange={this.onChange}
/>
<button type="submit">Submit</button>
</form>
</div>
);
}
}
ReactDOM.render(<Form />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>
答案 1 :(得分:0)
您好,您需要这样做。
import * as React from "react";
export default function Form () {
const nameInput = React.useRef<HTMLInputElement>(null);
const [nameVal,setNameVal] = React.useState("");
const onSubmitHandler = (e: React.FormEvent<HTMLFormElement>) => {
// handle yow submition
}
const onNameChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (!nameInput.current) return;
setNameVal(e.currentTarget.value);
const {validity} = nameInput.current;
if (nameInput.current.validity.badInput) {
nameInput.current.setCustomValidity("Set the message to show");
}
if (nameInput.current.validity.customError) {
nameInput.current.setCustomValidity("Set the message to show");
}
if (validity.patternMismatch) {
nameInput.current.setCustomValidity("Set the message to show");
}
if (validity.rangeOverflow) {
nameInput.current.setCustomValidity("Set the message to show");
}
if (nameInput.current.validity.rangeUnderflow) {
nameInput.current.setCustomValidity("Set the message to show");
}
if (validity.stepMismatch) {
nameInput.current.setCustomValidity("Set the message to show");
}
if (validity.tooLong) {
nameInput.current.setCustomValidity("Set the message to show");
}
if (validity.tooShort) {
nameInput.current.setCustomValidity("Set the message to show");
}
if (validity.typeMismatch) {
nameInput.current.setCustomValidity("Set the message to show");
}
if (validity.valid) {
nameInput.current.setCustomValidity("Set the message to show");
}
if (validity.valueMissing) {
nameInput.current.setCustomValidity("Set the message to show");
}
}
return (
<form onSubmit={onSubmitHandler}>
<label htmlFor="id-name">Name</label>
<input
ref={nameInput}
type="text"
required
min={5}
max={15}
value={nameVal}
onChange={onNameChange}
/>
<button type="submit">submit</button>
</form>
)
}
我的意思是您不必添加所有这些,只需添加您关心的那些