teslamotors / informed

A lightweight framework and utility for building powerful forms in React applications
https://teslamotors.github.io/informed
MIT License
949 stars 172 forks source link

FieldExists doesn't exist on the FormApi types #304

Closed marlondc closed 2 years ago

marlondc commented 4 years ago

https://github.com/joepuzzo/informed/pull/44 added FieldExists to FormApi but it's not typed in the index.d.ts

I can make a PR to add FieldExists to the FormApi types if you want.

joepuzzo commented 2 years ago

I removed this in v4. Would need a case to add it back

ppascualv commented 2 years ago

This method was useful for having a error handler component then you can distinguish which errors should be shown at field level or which ones at general level

ppascualv commented 2 years ago

Assuming this error field from an API errors: [{ messages: [string]; path: string }]

<ErrorsWrapper>
      <Alert severity="error">
        <Title>{t("common:errors")}</Title>
        {errors.map(e => {
          if (e.path === "base") {
            return e.messages.map(m => <Error key={m}>{m}</Error>)
          }
          // used to show errors that are produced by hidden fields and cannot be represented on the field that caused the error
          if (!fieldExists(e.path)) {
            return e.messages.map(m => <Error key={m}>{`${t(e.path)} ${m}`}</Error>)
          }
          return null
        })}
      </Alert>
    </ErrorsWrapper>