XF 2.1 Is it possible to access form input values in Entity's _preSave() function?

asprin

Active member
The use case is such that I need to modify the incoming form input value before storing it in the column. For example:

HTML:
<xf:dateinput name="start_date" value="{{ date($xf.time, 'Y-m-d') }}" />

PHP:
function _preSave()
{
    if($this->update())
    {
        $this->starts_in = <grab the start_date value here and convert to UTC>
    }
}
 

Kirby

Well-known member
$this->app()->request()->filter('start_date', 'str');
While this is surely possible, it IMHO should be avoided as that creates a tight coupling between the entity and user input.

Let's assume you've got such code within the entitiy and it is somehow being triggered when the entity save process is called within an automated (cron) job - there would be no user input at all.

Preparing user input is a job for controllers/services, not entities.
 
Last edited:

asprin

Active member
Let's assume you've got such code within the entitiy and it is somehow being triggered when the entity save process is called within an automated (cron) job - there would be no user input at all.
Totally valid point. But in my particular use case, from a controller POV (which accepts incoming form input) how would I ensure that the "calculated" value is stored in a column upon update?

My save function looks something like this:
PHP:
protected function fooSaveProcess(\Asprin\FB\Entity\Foo $foo)
{
    $data = array();
    $data['title'] = 'str';
    $data['mode'] = 'str';
    //$data['starts_in'] = <how to do the calculation here as start_date is present on the form but not as a column on the entity>
    // title, mode, starting are valid columns on the entity
    // start_date is a form input field on the UI upon which processing has to be done before saving
   
    $input = $this->filter($data);
    $form = $this->formAction();
    $form->basicEntitySave($foo, $input);

    return $form;
}

In other words, would it be possible to access POST's start_date in this function?
 
Top