Search code examples
plonezopeploneformgen

TALES expression to compare numeric input in Plone?


TALES expression is new to me. Can I get some good reference for the same? Actually I wish to define a content rule for numeric input field using ploneformgen. Something like:

python: request.form.get('amt', False) <= 5000     

then apply the rule.

Here 'amt' is a numeric/whole number field on the input form.


Solution

  • For reference, you should look at the official TALES specification, or refer to the TALES section of the Zope Page Templates reference.

    In this case, you are using a plain python expression, and thus the normal rules of python code apply.

    The expression request.form.get('amt', False) would return the request parameter 'amt' from the request, and if that's missing, return the boolean False, which you then compare to an integer value.

    There are 2 things wrong with that expression: first of all you assume that the 'amt' parameter is an integer value. Even a PFG integer field however, is still a string in the request object. As such you'll need to convert in to an integer first before you can compare it.

    Also, you fall back to a boolean, which in integer comparisons will be regarded as the equivalent of 0, better be explicit and use that instead:

    python: int(request.form.get('amt', 0)) <= 5000
    

    Note that for a PFG condition, you can also return a string error message instead of boolean True:

    python: int(request.form.get('amt', 0)) <= 5000 or 'Amount must be not be greater than 5000'