Friday, March 10, 2017

Laravel Validating Parent/Child User Input

I'm having a problem validating to prevent a user selecting a parent element, for example:

  • Option 1
  • Option 2
    • Sub Option 1
    • Sub Option 2
    • Sub Option 3
  • Option 3

A user could select a top level option if it has no children, for example they could select option 1 and option 3, but they can't select option as they would need to select one of the sub options.

The database structure is:

id, parent_id, name

If the option has a parent, the parent_id is the id of the parent (if not it is null).

I'm currently validating to check if the option exists in the database, but this obviously doesn't prevent top level options being selected if they have children.

Any suggestions?



via jdawg

Advertisement