Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Observer Typing Model forge
  • I have a problem with time in observer typing.

    When I create a new model or set a time_mysql/time_unix data_type field I get an error, that the typing encode wants a Date object.

    Can't this done be automatically? Creating a date object from input data. If it is numerical, then it is a timestamp, else check for a date_format option in the property definition and fallback to mysql.
  • The observer requires a Date object for date/time fields, that is correct. So if you use these validation rules, you have to make sure the model object contains the correct values.

    There is no automatic way of doing it, dates and times can be input in a million ways and formats, the (form) validation and input processing is very application specific.

    I use a custom observer for that, that validates my application specific input, and converts the field  to and from a MySQL datastamp.
  • I understand. What about doing it in a custom validation rule? Of course, this only works in case I know there is validation, but it probably always is.

    A also tried to override model set method, but I soon realized that it won't work.

    And I also had the idea to create a 'Format' observer, which can do these types of formatting. It would probably be the same as typing apart from that it could handle this types of formatting.
  • There are lots of ways to deal with it, but all are application specific. So it will not be easy (or will be complex) to create an observer generic enough to include.

    We use form (fieldset) validation rules too, for example for date fields, where the user is allowed to enter dates in all sorts of formats.

    Our date form validation rule converts it if it validates to a standard "DD-MM-YYYY", "DD-MM-YYYY HH:MM" or "DD-MM-YYYY HH:MM:SS" format depending on the field, then our date observer will convert this to a valid MySQL DATETIME field "YYYY-MM-DD HH:MM:SS" so it can be stored. When reading back, the observer will convert the DATETIME value to a string, using the Date class.

    The conversion both ways is done by a format specified in the observer properties (so it matches the required field format again), we kept is separate from the column properties definition.

Howdy, Stranger!

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

In this Discussion