XF 2.2 Return messages from preSave, without stopping execution

Dannymh

Active member
Hi Again, annoying old me.

I need to check against a set of conditions in PreSave i.e. is incoming amount mor than existing amount
there will be conditions whereby it will cause an "error" or at least log a message that needs to return to the user.

Generally speaking

  • User inputs amount
  • amount is less than needed in preSave so holds a message "amount to low"
  • Still saves amount and offer to DB
  • Returns message and offers to allow them to up their ammount.

Is that possible or am I going to have to check this postSave and try to target the result I want with SQL
 
It depends on your exact needs I guess, but you wouldn't necessarily need to handle this with entity events at all:

PHP:
// ...
$entity->save();

if ($entity->amount < 500)
{
    return $this->error(\XF::phrase('some_error'));
}

// ...

You could also proceed as normal and do the check and display an error message a template.
 
It depends on your exact needs I guess, but you wouldn't necessarily need to handle this with entity events at all:

PHP:
// ...
$entity->save();

if ($entity->amount < 500)
{
    return $this->error(\XF::phrase('some_error'));
}

// ...

You could also proceed as normal and do the check and display an error message a template.
the template method may also work.

The issue is that if I do this post save it can be difficult to ascertain the correct data from the database for the comparisons because the data is mixed order.

Essentially I just want to say
"Hey your amount is too low" but go ahead and complete the transaction and save the record.
 
The issue is that if I do this post save it can be difficult to ascertain the correct data from the database for the comparisons because the data is mixed order.
I did not understand this. If query via your primary key column, wouldn't it give you the exact amount they entered in the form?
 
I did not understand this. If query via your primary key column, wouldn't it give you the exact amount they entered in the form?
yes but I don't want that one.

So think about it this way
I have not perfectly structured data lets call them bids because thats the closest thing I can think of
Might look like
id, thread, user, amount, insert_time

When someone in a form puts in a bid i need to check it against the data in the database before inserting it, the conditions of the data will be returned based on the highest amount and the earliest insert time.

I want to do this pre-save because in cases where the amounts are equal or higher I have had issues with the last record being pulled through as this record so I end up comparing against myself. I wanted to do all of this pre-save so i dont wind up in that situation, i can run through the scenarios, still save the record but post the messaging I need.

The other part of wanting to do it pre-save is I need to have a context of what the before state was. I can do all that postSave but it gets a little more complex. I think I am going to try first of all to do it that way
 
Last edited:
Top Bottom