React-admin: getting error with nested item - react-admin

When I create an Input like:
The rendering is ok, but when submiting I get the error:
index.js:1446 Warning: Missing translation for key: "Cannot read property 'name' of null"
export const PatientCreate = props => (
<Create {...props}>
<SimpleForm>
<NumberInput source="bsn" />
<TextInput source="patientname.firstname" />
<BooleanInput source="active" />
</SimpleForm>
</Create>
);
When I remove the nested item patientname.firstname, everythings works correctlty..Anyone idea ?
Using Prisma server data, but the error is in Create, before a put is happening, so I think I has something to do with react-admin..
Model is (Prisma MongoDB):
type Patient {
id: ID! #id
bsn: Int #unique
active: Boolean! #default(value: true)
patientname: Patientname
deceased: DateTime
}
type Patientname #embedded {
firstname: String
}
In React-admin documentaion:
https://marmelab.com/admin-on-rest/Inputs.html#textinput
<TextInput source="author.firstName" />
Tnx I anyone can help me ...
Paul

In your i18n translation files need to respect the nested reference.
React admin tries to translate using property `resources.[resourcename].fields.[fieldname] (see docs) if none is present it uses the string on source prop. If you have nested resources and no translation file with nested reference that error will be presented because can't find nested source.
If you create a translation file like this:
const language = {
resources: {
patient: {
patientname: {
firstname: "firstname"
}
}
}
};
Your problem is fixed. Take note that you need to add this translation resource to react-admin, more information can be found here.

Related

Validating a SimpleForm field within React-Admin

I'm using an old version (2.9) of the React-Admin framework and having trouble validating a SimpleForm field.
I realize upgrading to a newer version is the way to go, but that will be a while.
I'm validating a field in a SimpleForm.
My validatefunction (below) returns an integer, 0 or above.
I want the validation to pass when that value is 0, but fail when the value is > 0. Currently, the form vails validation in all cases. Instead, I want it only fail when the return value is > 0.
Is there some way to specify that using some kind of expression on the validate line in the SimpleForm below?
const MyComponent = ({ dispatch, dataProvider, ...props }) => {
return (
<React.Fragment>
<SimpleForm>
...
<ReferenceInput
source="somefield"
validate={[ required(), validatefunction ]}
>
<SelectInput optionText="name" />
</ReferenceInput>
...
</SimpleForm>
</React.Fragment>
)
}
A better approach might be for my validatefunction to return undefined when the validation passes. But for reasons outside the scope here I haven't yet gotten that working.
Your validateFunction should return a string if there is an error.
const validatefunction = (value) => {
const validatorValue = oldValidateFunction(value);
if (validatorValue > 0) {
return 'error this does not validate';
}
}
See https://marmelab.com/react-admin/doc/2.9/CreateEdit.html#per-input-validation-custom-function-validator for the documentation of input validators.

Validate source parameter with typescript in react-admin

I was wondering if it was possible (at the moment) to validate what is transmitted in the source property of inputs/fields.
I generate types (typescript) from my graphql introspection schema and I use it when I retrieve the controller so it would be awesome if typescript could validate that the source really exists in my record.
Example :
type Organisation {
id: number;
name: string;
}
const OrganisationShow = (props: ShowProps) => {
const { record, ...controllerProps } = useShowController<Organisation>(props);
return (
<ShowView
record={record}
{...controllerProps}
>
<TextField source="id" />
<TextField source="name" />
<TextField source="foo" /> // Typescript should deny it
</ShowView>
)
}
Is this something possible or an idea ?
Thanks for your help !
No, this is not possible for now. You would have to pass the type to all fields (<TextField<Organization> source="id" />, so I don't think it's very developer-friendly. And for nested source (e.g. author.name), I don't think it's feasible.

In react-native-google-places-autocomplete , there is a method to set the inputs initial value, but it doesn't trigger the search query

I am trying to pass a saved search term string to prefill into google places autocomplete's input field. The setAddressText method they offer successfully prefills the input, but it does not trigger the search, so no dropdown options open. Only once you type something does the query run, so its obviously listening for an onChangeText event before it runs the query. I therefore don't see the point in the setAddressText method, if you then have to type again anyway. Surely there must be a way to trigger the search, without having to add / takeaway characters manually with the keyboard from the existing search term.
const { googleApiKey } = config
const ref = useRef()
useEffect(() => {
ref.current?.setAddressText(initialValue)
}, [])
return (
<View sx={{ flex: 1 }}>
<GooglePlacesAutocomplete
ref={ref}
placeholder={placeholder}
listViewDisplayed="true"
fetchDetails={true}
textInputProps={{
autoFocus: true,
...sx
}}
onPress={(data, details = null) => {
console.log("data", data, "details", details)
}}
query={{
key: googleApiKey,
language: "en",
components: "country:gb",
types: types ? types : null
}}
/>
</View>
)
}
Any wisdom on this would be greatly appreciated!
Full Disclosure: I maintain this library.
This is not currently supported by the library.
There is a feature request open here.

React Admin - FileInput that uploads to a single flat key instead of an object

In react admin, if you have a FileInput, it requires a child field, and stores the file URL inside the child field. So for example
{
"photo": {
"url": "https://url-to-image.com"
}
}
and the corresponding react admin code is
<FileInput source='photo' label='Photo' placeholder={<p>Drop your image file here</p>}>
<ImageField source='url' title='title' />
</FileInput>
I want to upload to a single key called photoUrl instead of a nested object photo.url, which would look like this:
{
"photoUrl": "https://url-to-image.com"
}
How can I do this? When I omit the ImageField, I get an error that a single child is expected. I tried leaving source="" empty for the imagefield but it still produces an error.
Any ideas?
Try passing parse and format functions to FileInput props
<FileInput
source='photoUrl'
label='Photo'
placeholder={<p>Drop your image file here</p>}
parse={p => p.url}
format={url => ({url})}
>
<ImageField source='url' title='title' />
</FileInput>
https://marmelab.com/react-admin/Inputs.html#transforming-input-value-tofrom-record

why react native can't require image currently?

<Image source={require(rowData.avatar)} />
error : Unknown name module ‘xxxxxx’
Why can print out the path but can't read pictures?
Try
<Image source={(rowData.avatar)} />
Images cannot use dynamically generated sources. Assuming what you're trying to do is to load an image as part of your package, your code must read:
const avatar = require('./path/to/avatar.jpg');
Only then you can use avatar as your source a follows:
rowData = {
avatar: avatar
}
<Image source={rowData.avatar} />
If you know before hands what images are going to be needed in your app, I suggest that you create an asset file in which you add all your hardcoded require, such as:
// assets.js
return {
avatar1: require('./path/to/file.jpg'),
avatar2: require('./path/to/file.jpg'),
avatar3: require('./path/to/file.jpg'),
}
And then you would construct your rowData as follows:
import avatars from './assets';
rowData = {
avatar: avatars['avatar1']
}
Where you would likely replace avatar1 with a variable containing the key pointing to the avatar you're interested in.
Here is an asset file I used for one of my projects.