On my SF application, i have "regular" form (generated with formType in controller).
Let's say user1 edit the formA and it takes several minutes to fill it. In that period, user2 also edit the formA. You can guess, the last user that hit submit wins, and the other one looses all he filled.
How can i prevent this from happening? This is not the case of a multiple submit (by the same user).
I don't know if it's possible and how to best implement it, but my guess would be to add some "lock" mechanism.
Would it suffice to add a @Version to a field in the entity for my Form and SF takes care of the rest ?
I would like that when user2 tries to open the form, i can send the message : "Sorry, user1 is already completing the form". I don't want that user2 looses all he fills only when he hit submit.
Thank you.
What you are trying to achieve is not provided out of the box by the framework.
What you can do is take advantage of the Lock Component or implement a similar solution by yourself.
// Symfony\Component\Lock\Factory $factory
$lock = $factory->createLock('your-amazing-form-USER-ID');
if (!$lock->acquire()) {
// Sorry, user1 is already completing the form
// throw exception or render a template for example
}
// Do what you need to show the form.
// Form is submitted and valid? Do what you need and release the lock.
if ($form->submited()) {
// Save to database
$lock->release();
}
A good idea is to give a TTL to the lock so that if the user does not submits the form the lock is still released. Since you still want that the user who acquired the lock is able to submit the form, then the resource name should contain for example the user identifier.