Get rules from an input #11552
Unanswered
blugnomeXD
asked this question in
Q&A
Replies: 1 comment 3 replies
-
You can provide validation functions or array of functions. The usage is described well in this doc. You can find the live code here. import React from "react";
import Card from "@material-ui/core/Card";
import { SimpleForm, TextInput } from "react-admin";
const required =
(message = "Required") =>
(value) =>
value ? undefined : message;
const minLength =
(min, message = "Too short") =>
(value) =>
value && value.length < min ? message : undefined;
const maxLength =
(max, message = "Too big") =>
(value) =>
value && value.length > max ? message : undefined;
const number =
(message = "Must be a number") =>
(value) =>
value && isNaN(Number(value)) ? message : undefined;
const validateAmount = [required(), number(), minLength(3), maxLength(5)];
export default () => (
<SimpleForm>
<TextInput label="Amount" source="amount" validate={validateAmount} />
</SimpleForm>
); |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Is there a way to obtain the rules validations that an input has? I need to know within the input if I am passing a maxLength validation and the number. But I don't see a way of how to do it. I'll provide a small piece of example code."
If you provide the small piece of example code, I'd be happy to assist further! Im using React-Admin also.
Rendering component...
Input code
Beta Was this translation helpful? Give feedback.
All reactions