How to stop workflow action for a certain condition

I have a field called admit which is checkbox and two workflow states called sent for admission and admitted and when admit button is clicked then the transition occurs

so during the workflow transitions from sent for admission to admitted I need to check if the admit checkbox is checked or not, if checked then only the transition should be completed otherwise an error should be thrown and transition should fail stop
here is the code i am working on

error message is being displayed, but the transition is not getting stopped

@Vinay1 in the second table , the admit action click edit then go to condition field, write your condition there . user will not see the button at all if the condition is false.

hi @bahaou,
that would work fine, but I think it would be more good in terms of user experience if we show an error message saying ‘admit should be checked’ because sometimes the user might not check the admit checkbox and wonder why admit button is not getting displayed
so how can i show the error message while transitioning from ‘send for admission’ to ‘admitted’ when admit checkbox is not checked

@Vinay1 well you need to write the code before the workflow action .

i am writting the code, this is what i wrote

the code is working fine, but I am unable to stop the workflow from transitioning
how can i stop the workflow from transitioning?

@Vinay1 your code is after_workflow_action, you cant stop it after it happens now can you ?