This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
I have 3 models, User, Booking and Attendee.
Booking model
public $belongsTo = [
'user' => User::class,
];
public $belongsToMany = [
'attendees' => Attendee::class
];
Attendee model
public $belongsTo = [
'user' => User::class
];
I have a config_relation.yaml defined for the Bookings controller...
attendees:
label: Attendees
view:
list: $/neil/my-plugin/models/attendee/relation_columns.yaml
manage:
form: $/neil/my-plugin/models/attendee/fields.yaml
scope: forUser
When creating and updating a booking, I'd like to check that a User has been selected when the "Add Attendee" button is clicked. If not, I can throw an exception and let the user know they can't add attendees yet.
I know I can hook into the onRelationButtonAdd()
method in my Bookings controller, and that accepts an $id - but I'm not sure how I can check if a User has been selected on the parent model (Booking).
The reason I want to do this is because I've added a scope to the Attendee relation that only brings back Attendees that are attached to the selected User - And that scope needs an User assigned to the booking for it to work.
I would also like it so that if the User is changed on a Booking, the previous Attendees are removed from the Booking.
Is it possible to do this on the Booking create form?
neilcarpenter said:
I have 3 models, User, Booking and Attendee.
Booking model
public $belongsTo = [ 'user' => User::class, ]; public $belongsToMany = [ 'attendees' => Attendee::class ];
Attendee model
public $belongsTo = [ 'user' => User::class ];
I have a config_relation.yaml defined for the Bookings controller...
attendees: label: Attendees view: list: $/neil/my-plugin/models/attendee/relation_columns.yaml manage: form: $/neil/my-plugin/models/attendee/fields.yaml scope: forUser
When creating and updating a booking, I'd like to check that a User has been selected when the "Add Attendee" button is clicked. If not, I can throw an exception and let the user know they can't add attendees yet.
I know I can hook into the
onRelationButtonAdd()
method in my Bookings controller, and that accepts an $id - but I'm not sure how I can check if a User has been selected on the parent model (Booking).The reason I want to do this is because I've added a scope to the Attendee relation that only brings back Attendees that are attached to the selected User - And that scope needs an User assigned to the booking for it to work.
I would also like it so that if the User is changed on a Booking, the previous Attendees are removed from the Booking.
Is it possible to do this on the Booking create form?
Hi neilcarpenter,
If I understand well your problem could be managed by built-in 'trigger' and 'dependencies' functions of October backend forms.
My idea:
Step 1. Hide "Attendees" field by default and change its visibility by a Trigger when user is selected.
See documentation: https://octobercms.com/docs/backend/forms#field-trigger-events
Step 2. Change values of "Attendees" filed according to "User" field by a Dependency.
See documentation: https://octobercms.com/docs/backend/forms#field-dependencies
1-2 of 2