nank1ro / flutter-shadcn-ui

shadcn-ui ported in Flutter. Awesome UI components for Flutter, fully customizable.
https://mariuti.com/shadcn-ui
MIT License
847 stars 52 forks source link

How to get ShadInput validation error. #106

Closed yusuphwickama closed 1 month ago

yusuphwickama commented 1 month ago

Hello,

I have this field with a custom validator. How can I access the error message from the validator and display it in my custom Text widget?

ShadInputFormField(
  id: 'account_number',
  enableSuggestions: false,
  error: Padding(
    padding: EdgeInsets.symmetric(horizontal: 6.w),
    child: Text(errorText, style: theme.textTheme.small.copyWith(color: theme.colorScheme.destructive)),
  ),
  label: Padding(
    padding: EdgeInsets.symmetric(horizontal: 6.w),
    child: Text('Account Number', style: theme.textTheme.small.copyWith(fontWeight: FontWeight.bold)),
  ),
  placeholder: Text('$providerType account number'),
  validator: (v) {
    if (v.length < 13) {
      return 'Account must be 13 characters.';
    }
    return null;
  },
),

I want to assign it to errorText

nank1ro commented 1 month ago

Will add it, meanwhile use setState errorText in the validator

nank1ro commented 1 month ago

If you're just changing the text style and padding, consider using errorStyle and errorPadding passing the decoration

yusuphwickama commented 1 month ago

If you're just changing the text style and padding, consider using errorStyle and errorPadding passing the decoration

Thanks, I'm not seeing this parameter. Its available in which version?

yusuphwickama commented 1 month ago

Will add it, meanwhile use setState errorText in the validator

I have multiple fields, this is just one of them so this approach might quickly get complicated.

nank1ro commented 1 month ago

Will add it, meanwhile use setState errorText in the validator

I have multiple fields, this is just one of them so this approach might quickly get complicated.

You can change it from the theme level to all form fields:

ShadThemeData(
    decoration: ShadDecoration(
        labelStyle: TextStyle(color: Colors.blue),
        errorStyle: TextStyle(color: Colors.orange),
        errorPadding: const EdgeInsets.symmetric(horizontal: 6),
        labelPadding: const EdgeInsets.symmetric(horizontal: 6),
  ),
),

or just for the Input fields

ShadThemeData(
  inputTheme: ShadInputTheme(
    decoration: ShadDecoration(
      labelStyle: TextStyle(color: Colors.blue),
      errorStyle: TextStyle(color: Colors.orange),
      errorPadding: const EdgeInsets.symmetric(horizontal: 6),
      labelPadding: const EdgeInsets.symmetric(horizontal: 6),
    ),
  ),
)

or for each single field widget passing the decoration

yusuphwickama commented 1 month ago

Thanks