Alert

Flash messages.

0 stars out of 5

0 0 0 0 0
  • Author Taufik Nurrohman
  • Maintainer 1
  • Member
  • Version 2.0.2

All methods except get(), let(), and set() can be used. This class uses the __callStatic() method to create dynamic types. Any given method will declare the alert type. The most common and recommended method names are error(), info(), and success(), which can be used to represent three-valued logic (error() as false, info() as null, and success() as true).

Usage

Alert::error('This is an error message.');
Alert::info('This is an info message.');
Alert::success('This is a success message.');

It is possible to add parameters to your message like this. All description data is passed internally to the i() function, making it easy to translate:

Alert::error('Please fill out the %s field!', ['name']); // “Please fill out the name field!”

To display the alert block in your layout, add this line at a specific point in your layout file, for example, just after the form’s opening tag:

<?= self::alert(); ?>

Below is a basic example of how to place the alert block in your HTML form:

<form method="post">
  <?= self::alert(); ?>
  <p>
    <input name="query" type="search">
  </p>
  <p>
    <button type="submit">
      <?= i('Submit'); ?>
    </button>
  </p>
</form>

The built-in alert block doesn’t care about the type. Any generated alert data will be returned as a <p role="alert">…</p> element in the HTML output. To generate a unique alert block for each type, you need to create your own HTML output in a .\lot\y\*\alert.php file:

<?php foreach (Alert::get() as $v): ?>
  <p class="alert alert-<?= $v[2]['type']; ?>" role="alert">
    <?= $v[1]; ?>
  </p>
<?php endforeach; ?>

You can also use a hook to modify the HTML output. However, this hook will have no effect on the alert markup you’ve manually created in the .\lot\y\*\alert.php file (unless you follow how I form the alert markup in the .\x\alert\engine\y\alert.php file):

Hook::set('y.alert', function ($y) {
    $y[0] = 'div';
    foreach ($y[1] as &$v) {
        $v[2]['class'] = 'alert';
    }
    $y[2]['class'] = 'alerts';
    return $y;
});

0 Comments

No comments yet.