我尝试实现一个页面,用于使用admin-on-rest创建新用户。我想为Password
和Repeat Password
添加两个字段,您可以指导我吗?我在<TextInput source="password" type="password" validate={ required }/>
字段中使用Password
,但是Repeat Password
呢?
我们如何验证Repeat Password
?
我对如何创建此页面一无所知。
此问题validate confirmation password in redux form的答案是有效的。
我的例子:
import React from 'react';
import { SimpleForm, TextInput, Create } from 'react-admin';
import { required, email } from 'react-admin';
const PasswordValidate = values => {
const errors = {};
if (!values.username) {
errors.username = 'Required';
}
if (!values.password) {
errors.password = 'Required';
}
if (!values.confirmPassword ) {
errors.confirmPassword = 'Required' ;
} else if (values.confirmPassword !== values.password) {
errors.confirmPassword = 'Password mismatched' ;
}
return errors;
};
export const UserCreate = (props) => (
<Create {...props}>
<SimpleForm validate={PasswordValidate}>
<TextInput source="email" type="email" validate={[required(), email()]} />
<TextInput source="username" />
<TextInput label="Password" source="password" validate={required()} />
<TextInput label="Repeat password" source="confirmPassword" type="password" validate={required()} />
</SimpleForm>
</Create>
);