Closed 4mitabh closed 4 years ago
I was thinking, if we can add 4 functions to the expression language:
I will be happy to submit a PR if you are Ok with this.
I am not sure, but there should be expression method for parsing int like Integer.parse or int.parse did you tried?
Integer.parse / int.parse didn't work.
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.
Great!
Thanks, that really helps.
I'll go with a custom component with a numeric property for now.
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.
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.
@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
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.