edongashi / WpfMaterialForms

Dynamically generated forms and dialogs in WPF
MIT License
51 stars 14 forks source link

Currency binding #16

Open redbaty opened 6 years ago

redbaty commented 6 years ago

Currency binding generates a invalid result.

The problem is located here:

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            try
            {
                deserializer(value as string, overrideCulture ?? cultureInfo);
                return new ValidationResult(true, null);
            }
            catch
            {
                return new ValidationResult(false, errorProvider.GetErrorMessage(value));
            }

on ConversionValidator.cs

The deserializer seems to validate correctly when the user input a value but when validation is called after (like a button) it fails because the string parsed contains special caractes like "R$" for brazillian currency. Example:

User inputs 7,75 and it validates OK and turns it into "R$7,75" then when I press the validation button it will fail saying the string contains invalid characters.

If you don't have time it's ok, I'll take a look at it when my graphics card arrives

redbaty commented 6 years ago

I managed to boot on secure mode, and found the problem! Added to the fork.

edongashi commented 6 years ago

I have an idea, we can include a PreProcess step in conversion

[PreProcess("R\$(\d+)", "$")]
public int Price { get; set; }

from which the replacement will be fed into the converter. I'll think of other solutions and from next week I can become more involved in these things. Looks like you're in a similar situation so that time we can split up every feature and implement them.

redbaty commented 6 years ago

@EdonGashi Yes that would be great beacause it opens up to more things, as for currency this should be fixed by passing NumberStyles to double.parse but this could happen on other types too, I'll check it out when I can.

redbaty commented 6 years ago

@EdonGashi Hey I think you should open a issue to keep track of suggestions, cause I've got loads and when I get some free time I'd be glad to add them!

edongashi commented 6 years ago

Opened #17 & started testing some approaches to field conversion

edongashi commented 6 years ago

Added [Replace("regexpattern", "replacement")] attribute which can be used to modify input string before being parsed. Currently this only works for converted fields. I'm not sure if this should also work for strings, since in them you can just define a custom setter to process the value.

This shouldn't be used for things like currency though, because I'm adding a new attribute [NumberStyle(NumberStyles.Currency)] which can decorate numeric properties. Otherwise hard coding Currency number as fallback does not sound too flexible.

Note on Replace since it accepts dynamic expressions you can do something like

[Replace("{StaticResource CurrencySymbol}(\d+)", "$1")]

redbaty commented 6 years ago

Awesome!

edongashi commented 6 years ago

Reopening so I don't forget about NumberStyles