0

I need to make alert blocks the children of any button container. In Phalcon framework there's only one instance of flashSession initialization:

<?php $this->flashSession->output(); ?>

This code can be used in specific place of html file. And it's fine if we have only one form with submit button on page - I just put the code inside button block. But If there's more submit buttons, the correct alert placement appears to be impossible. The main question is - is it possible to make flashSession alert to appear only inside block with button which initialized it?

James Fenwick
  • 2,190
  • 1
  • 20
  • 30
netsplatter
  • 559
  • 3
  • 14

2 Answers2

0

Easy solution would be to use form submit value or add a hidden input to the form.

<button type="submit" name="submit" value="form-1">Submit</button>
or
<input type="hidden" name="submit" value="form-1"/>

And above your form where you want to show flash errors do something like:

<?php if ($this->request->has('submit') && $this->request->getPost('submit') == 'form-1'): ?>
<?php $this->flashSession->output(); ?>
<?php endif; ?>
Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32
  • Thank you! I'm not sure about $reqest class, can't create it. Maybe it's the custom framework thing. When I'm trying to make it, the path \Phalcon\Http\Request is incorrect... https://docs.phalconphp.com/en/latest/api/Phalcon_Http_Request.html – netsplatter Mar 18 '16 at 08:56
  • Hey, no need to create it, Request is included by default and you can use it. I updated my example above. See $this->request->has(). This is the plain PHP example without using Volt templates. With volt the syntax is {% if request.has('someVar') %} – Nikolay Mihaylov Mar 18 '16 at 09:46
0

Try using in your view, just the follow: {{ content() }}

Edit

Forget the flashSession in this case. If you can't create an alert above all your forms:

// In controller
if($this->request->getPost('button1')) {// or just _POST
    $this->view->setVar('error1', "You have clicket at button 1");
}

if($this->request->getPost('button2')) {
    $this->view->setVar('error2', "You have clicket at button 2");
}

// In view
<form>
    <button name="button1"><?php $flash->outputMessage('error', $error1); ?></button>
    <button name="button2"><?php $flash->outputMessage('error', $error2); ?></button>
</form>

Here is how like to use. But I don't see any problem to do that in your case:

// In services:
$di->set('flash', function () {
    $flash = new FlashDirect( // wherever your classes are, here using bootstrap
        array(
            'error'   => 'alert alert-danger bold',
            'success' => 'alert alert-success bold',
            'notice'  => 'alert alert-info bold',
            'warning' => 'alert alert-warning bold'
        )
    );

    return $flash;
});

// in controller
$this->flash->error("THere is something wrong");
$this->flash->success("Nice, it's correct!");

// in view
{{ content() }}
<form name="1"></form>
<form name="2"></form>
  • Hi! Thank you for the answer. You mean I should put the code inside global parent container? – netsplatter Mar 18 '16 at 09:01
  • {{ content() }} is the equivalent of flashSession->output() ?> Its just Volt templates syntax. – Nikolay Mihaylov Mar 18 '16 at 09:52
  • You can't use isset() on $this->request->isPost(). You will get Fatal error: "Cannot use isset() on the result of a function call". Phalcon provides request->has() for that, you may want to update your answer ;) – Nikolay Mihaylov Mar 19 '16 at 05:45