OndrejKunc / flutter_dynamic_forms

A collection of flutter and dart libraries allowing you to consume complex external forms at runtime.
MIT License
203 stars 59 forks source link

Validate form elements with numbers #70

Closed 4mitabh closed 4 years ago

4mitabh commented 4 years ago

Hi, I have a textField for numbers, and need to perform simple validations on it. sort of like, @mytextField.value > 100

The above fails because the value is a string and not a number.

Are there any conversion functions to convert form values from string to int and double ? Or is there a better way to achieve the same validation ?

TIA.

4mitabh commented 4 years ago

I was thinking, if we can add 4 functions to the expression language:

  1. bool isValidInteger(String) - Returns if a string successfully parses into an Int
  2. bool isValidDouble(String) - Returns if a string successfully parses into a Double
  3. int toInteger(String, int) - parses string into a Int, returns the value 2nd parameter if parsing fails.
  4. double toDouble(String, double) - parses string into a double, returns the value 2nd parameter if parsing fails.

I will be happy to submit a PR if you are Ok with this.

kalismeras61 commented 4 years ago

I am not sure, but there should be expression method for parsing int like Integer.parse or int.parse did you tried?

4mitabh commented 4 years ago

Integer.parse / int.parse didn't work.

OndrejKunc commented 4 years ago

Hi,

I looked at the source code and there really is no string to integer conversion at the moment. One workaround right now is to write custom component and define the property of type Property<int> instead of Property<String>. You would then need to make sure only the ints will be assigned to the property (that can be done in the renderer part). However, it is not very ideal, because you may need String values in that property as well - in that case, you would need to have two properties and assign both on the input change.

I think your proposal looks good, those methods will definitely be handy and I will happily accept that PR! There are just 2 obstacles you may hit when you start implementing it. With each new function, you will need to implement a visitor method on CloneExpressionVisitor and TraversalExpressionsVisitor. And the second thing is that internally we use custom number types Integer and Decimal so we can have a common Number ancestor. It gets automatically converted to regular int and double if you use it in the component properties. So it may make more sense instead of isValidDouble to have isValidDecimal and instead of toDouble to have toDecimal. Additionaly you can also have Number toNumber(String, Number) and Number isValidNumber(String) to benefit from the common Number ancestor. There is already a parse method on the Number data type that you can use. See https://github.com/OndrejKunc/flutter_dynamic_forms/blob/2f03512d4aac8952a100570a1299f34775f069c1/packages/expression_language/lib/src/number_type/number.dart#L244

I hope that doesn't scare you from implementing it :). I know the documentation is really needed - at least for all the available functions. In the future, I would like to revisit the expression_language implementation and maybe find a way how to make it more customizable, so you would be able to write custom functions and inject them directly from your project.

4mitabh commented 4 years ago

Great!

Thanks, that really helps.

I'll go with a custom component with a numeric property for now.

sharpsan commented 3 years ago

Are we moving towards support for custom expression_language functions?

I find some of my expression logic gets very redundant and the same expression is used across multiple entries. It would be convenient to be able to use shortcuts like the existing now(), for example. In some cases functionality can't be achieved until custom expressions are supported.

OndrejKunc commented 3 years ago

Hi @sharpsan, it is the next big feature I would like to implement. I didn't have much time lately, but right now it looks like this project will be used in my current workplace, so I will probably have some capacity to do it soon.

OndrejKunc commented 3 years ago

@sharpsan I just released a new version of packages, which contains the possibility to implement custom expressions. See https://github.com/OndrejKunc/flutter_dynamic_forms/tree/master/packages/expression_language#writing-custom-expressions or check the example project https://github.com/OndrejKunc/flutter_dynamic_forms/tree/master/packages/flutter_dynamic_forms_components/example