0
votes

I have a standard form, generated via cake/bake.

When the form is send, my controller checks a (trivial) condition,

  • on condition A it just saves the data (patchEntity($foo, $this->request->getData())
  • on condition B it should call a JS warning, and then save the data.

My favorite warning would be a Bootstrap4 Modal, but how can I call a modal in a controller? I know this kind of violates the idea of MVC. So, alternative ideas are welcome.

The only solution I see atm is that I redirect to a new action which just opens a modal.

2

2 Answers

1
votes

Given that the condition can only be evaluated on the server side, you could for example issue an AJAX request, either before sending the form, or for sending the form itself, and then respond with corresponding information that you can evaluate in your AJAX response handler to decide whether you need to show a modal.

You should then probably (re)send the form with an additional flag that indicates that the consecutive request stems from the warning dialog, and that you can go on with saving the data.

0
votes

What is the problem in using flash component, like so :-

$this->Flash->error(__('This is the warning.'));

Remember to add $this->loadComponent('Flash'); in the initialize function of your controller, like so:-

class ArticlesController extends AppController{

    public function initialize(){

        parent::initialize();
        $this->loadComponent('Flash'); // Include the FlashComponent
    }

    // Rest of your code here...
}