mcintyre321 / FormFactory

MVC5, Core or standalone - Generate rich HTML5 forms from your ViewModels, or build them programatically
http://formfactoryaspmvc.azurewebsites.net/
MIT License
304 stars 102 forks source link

Localitzation and Validation Messages #73

Closed ivansoriabia closed 5 years ago

ivansoriabia commented 5 years ago

Hello Harry!

I'm finishing my project with FormFactory and I'm trying to validate the data from my inputs. In ASP .NET Core you can set it like this:

public class SomeViewModel
{
    [Required(ErrorMessage = "Your elegant error message goes here")]
    public int ProductId { get; set; }
} 

But I haven't found any way to set it when I create my PropertyVms, so there is way to set my custom error message?

Another thing that I would like to change is the language that the NotOptional / Optional Label is shown. I would like to set it in Spanish.

Thank You!

mcintyre321 commented 5 years ago

So you are creating PropertyVms programatically? You can add your own attributes: e.g.

new PropertyVm(typeof(DateTime), "StartDate")
{
    DisplayName = "Start Date",
    Value = startDate
    GetCustomAttributes = () => new object[] {new DisplayFormatAttribute {DataFormatString = "dd/MM/yyyy"}]
},

So you can add a RequiredAttribute into the GetCustomAttributes Func

mcintyre321 commented 5 years ago

I think to fix the optional message you'll have to either

  1. take your own custom version of /Views/Shared/FormFactory/Form.Property.cshtml and modify it to have a hardcoded version

2, or take a fork, and modify mine to work of a resx file, and send me a Pull Request

mcintyre321 commented 5 years ago

This PR already added a resx file

https://github.com/mcintyre321/FormFactory/pull/68/files

ivansoriabia commented 5 years ago

What I need to do to add Spanish translation to FormFactory?

mcintyre321 commented 5 years ago

Being a Brit, I'm terrible at i18n, but have a read of this.

As I recall

  1. add keys to the resx files for the text you want translated see Resources.resx and Resources.es.resx - VS should give you a custom editor for doing this.

  2. use the resource in the code e.g. CompareAttribute.cs

  3. get your app to set the correct CurrentCulture

zHaytam commented 5 years ago

Does creating a new resource file (e.g. Resource.fr.resx) in our projects work? or do we have to clone FormFactory and create the new language resource files in it for it to work?

mcintyre321 commented 5 years ago

I'm not sure, I'm not a big resx user.