Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM Validation and international number formats
  • Hi everyone!

    I try to use ORM for a webpage which deals with houses, apartments, and so on. The users of the webpage are Germans so I want to use German number formats, e.g. "1.234,56" instead of "1,234.56".

    I now try to setup the validation for the models and I don't know how to do it best. Where should I put the transformation of number formats? Should I validate the "German" version or the "English" one? Should I use 2 fields like "price" and "price_de"?

    I think the model should match the database structure which means for me the model should store the English version for the number but in this case my validation makes no sense because the user will never enter the value in this format.

    Do you have any suggestion? Perhaps ORM isn't just the right thing for this case?

    Best regards,
    Christian
  • Hi,
    you can use the "format" method of the "Number" class. It can format the number in every format you want. An example:


    echo Num::format('1234567890', '000.000,0000');
    // 123.456,7890
    If you don't like this approach let me know, I can create a method for you wrapping the number_format function of php.
  • I only store normalized data in the database. PHP internally calculates with decimal point as well, so storing a European format isn't a very smart thing to do.

    I use a generic validation rule to verify decimal numbers. It takes a number as parameter, to indicate the number of decimals that need to be present, and it swaps the decimal comma to point.

    The other way, to display it, you can convert it back to decimal comma if needed.
  • At first thank you for your advices!

    I think I have to clarify my question a little bit. It's not the question on how to convert the numbers but where to do it.

    Let's take an example:

    I have a model called "House" which has the property "price". To make sure the values are properly formed I set up a validator which validates the prices. Because the database expects something like "12345.67" I validate for this format. Now the property can be saved by House->save().

    Now I have a form for the German users. They will enter something like "12345,67". In my Controller I now just want to give this input to my model and use the House->save() method and look for validation exceptions to eventually show the form again.

    This doesn't work. I would need a second property with a different validator and would have to copy and convert the property. Second problem is that ORM tries to safe this second property to the database but this second property does not exist in the table.

    I could save only the German version as VARCHAR but then I would miss much of the SQL features like sorting and calculating.

    I hope you could give me some more advices on how to design this special case!

    Best regards,
    Christian
  • HarroHarro
    Accepted Answer
    As I wrote:

    I convert the user input into a standard PHP number (1.211,25 to 1211.25) in my validation rule.

    I convert it back to the local number format in the view when I need to display it, or in case it's a fieldset form I update the value on the fieldset field before I pass the fieldset to the view.

    This makes sure that internally you always have the correct number representation.
  • Thank you, I will try it that way!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion