In the admin panel created with EasyAdminBundle, the administrator can create a new Booking. I want to add an availability check (via a service) before this new booking instance is persisted into the database. If this check returns false, the admin should be redirected back to the form.
I have extended the EasyCorp\Bundle\EasyAdminBundle\Controller\AdminController
class and overridden the persistEntity()
function:
...
use EasyCorp\Bundle\EasyAdminBundle\Controller\AdminController as BaseAdminController;
class BookingController extends BaseAdminController
{
private $availabilityService;
public function __construct(AvailabilityService $availabilityService)
{
$this->availabilityService = $availabilityService;
}
protected function persistEntity($booking)
{
$checkin = Carbon::instance($booking->getCheckin());
$checkout = Carbon::instance($booking->getCheckout());
if($this->availabilityService->checkAvailability($checkin, $checkout)) {
parent::persistEntity($booking);
} else {
return false; //redirect back to the form
}
}
}