Prevent the selection of some values in lookup fields by returning an error message

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
fgazza
Veteran Member
Posts: 70
Joined: 2019-04-30 17:37

Prevent the selection of some values in lookup fields by returning an error message

Post by fgazza » 2019-07-21 20:13

Hi everyone.

I have a "partners" table with a "school" lookup field that points to a "schools" table.
Even if in the "school" field of the partner table all the schools registered in the "schools" table appear, I would like that if the user chooses some schools (ex: school1, school3 and school9) an error message appears: you cannot select this school because it is not part of the project partners ". The user therefore will not be able to choose school1, school3 and school9 because in addition to the error message a special function will prevent the completion of the school field with one of the aforementioned schools.

I hope you give me valuable suggestions.

Hello and thanks!

Fabiano

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 219
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Prevent the selection of some values in lookup fields by returning an error message

Post by jsetzer » 2019-07-22 07:02

What about changing the sql query for that lookup field using "advanced" button? If schools table has a field, for example "is_selectable" (integer, checkbox), add " WHERE schools.is_selectable" to your query.

fgazza
Veteran Member
Posts: 70
Joined: 2019-04-30 17:37

Re: Prevent the selection of some values in lookup fields by returning an error message

Post by fgazza » 2019-07-22 11:33

Ok. I think it's a good idea but I'm afraid that by applying your suggestion the result will be that the user will not see the "school1, school3 and school9" entries in the drop-down list, while I would like these entries to be visible and the error message appeared after the user selected one of the prohibited entries. This would be useful for me to be able to point out to the user that his choice is not possible so that he understands the reason, through the message "you cannot select this school because it is not part of the project".
If I understand correctly by doing as you say the user would not see the school in the list but I would like him to see it without being able to select it.
Thank you.
Fabiano

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 219
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Prevent the selection of some values in lookup fields by returning an error message

Post by jsetzer » 2019-07-22 13:32

fgazza wrote:
2019-07-22 11:33
If I understand correctly by doing as you say the user would not see the school in the list but I would like him to see it without being able to select it.
Yes, you are right. Doing it this way, those items will not be available for selection (because they are not available for selection if I got it right).

If you need to show them in the dropdown anyway, forget about my first idea. But then it may become more difficult:
  • Alternative 1: JQuery / AJAX
    Use TABLENAME_dv-function or TABLENAME-dv.js to add an invisible alert. On client-side, after selecting a dropdown item, you can use JQuery to read the id of the selected item, use AJAX to call a server-side script, pass selected school-id to the server, then on server-side check if the selected school is available and return TRUE or FALSE as json, for example. Then, back on client-side, after you got the server-response you can hide() or show() the alert created before.
  • Alternative 2: PHP
    Check availability in your PHP-hook-file TABLENAME.php before_insert() and before_update() and return FALSE if the selected school is not available. Then you need a way to show a warning.
Alternative 1 is harder to implement and there are a couple of tasks to be solved like listening to the dropdown-select event, getting the shool-id and doing some AJAX calls.

Alternative 2 is easier to implement but maybe not interactive enough. Perhaps to help users understand you can additionally change the SQL query and add a hint to the school's name like "School-1 (not available)". Showing it this way they will understand if insert/update fails.

Right now I don't see another way using hooks but maybe someone else has a good idea!

Regards,
Jan

Post Reply