PHP code example of eftec / messagecontainer

1. Go to this page and download the library: Download eftec/messagecontainer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eftec / messagecontainer example snippets


echo $container['password']['error'];

if (isset($container['password'])) {
    if(isset($container['password']['error'])) {
        echo $container['password']['error'];
    }
}

// We could show the first error (or empty if none):
echo $container->getLocker('password')->firstError(); 

// it shows all errors (or nothing if none):
foreach($container->getLocker('password')->allError() as $msg) {
    echo $msg;
} 

use eftec\MessageContainer;
$container=new MessageContainer(); // we create the full lockers
$container->addItem('locker1','It is a message','warning');  // we store a message inside "id1"
$container->addItem('locker1','It is a message','error');  // we store another message inside "id1"

// And later, you can process it

$lastErrorsOrWarnings=$container->get('locker1')->allErrorOrWarning();
// it will not crash even if the locker2 does not exists.
$lastErrorsOrWarnings2=$container->get('locker2')->allErrorOrWarning();


$container=new MessageContainer();
$container->addItem('id1','some msg 1','error');
$container->addItem('id1','some msg 2','error');
$container->addItem('id1','some msg 1','warning');

$container->addItem('id2','some msg 1','info');
$container->addItem('id2','some msg 1','success');

$container->addItem('id33','some msg 1','error');
$container->addItem('id33','some msg 2','error');
$container->addItem('id33','some msg 1','success');
$container->addItem('id33','some msg 2','success');
$container->addItem('id33','some msg 2','success');

// obtaining information per locker
$msg=$container->getLocker('id1')->firstErrorOrWarning(); // returns if the locker id1 has an error or warning
$msg2=$container->getLocker('id2')->allInfo(); // returns all info store in locker id2 ["some msg1","some msg2"]
$msg3=$container->getLocker('id3')->allInfo(); // (note this locker is not defined so it returns an empty array.
$msg4=$container->getLocker('id33')->hasError(); // returns true if there is an error.
$msg5=$container->getLocker('id33')->countError(); // returns the number of errors (or zero if none).

// obtaining information globally (all lockers)
$msg7=$container->hasError(); // returns true if there is an error in any locker.
$msg8=$container->allErrorArray(true); // returns all errors and warnings presents in any locker.

$container->addItem(<idlocker>,<message>,<level>,<context array>);

// without context:
$container->addItem('locker1','The variable price must be higher than 200','warning');

// with context:
// The variable price must be higher than 200
$container->addItem('locker2'
                    ,'The variable {{var1}} must be higher than {{var2}}'
                    ,'warning'
                    ,['var1'=>'price','var2'=>200]);
// The variable price must be higher than 200 (not 500, the context is not updated this second time)
$container->addItem('locker2'
                    ,'The variable {{var1}} must be higher than {{var2}}'
                    ,'warning'
                    ,['var1'=>'price','var2'=>500]);
// The variable price must be higher than 200 (we use the previous context)
$container->addItem('locker2'
                    ,'The variable {{var1}} must be higher than {{var2}}'
                    ,'warning');


$container->get('idfield'); // container idfield
$container->get('idfield2'); // container idfield2

if($container->hasError()) {
    // Error: we do something here.
    echo "we found ".$container->errorCount()." errors in all lockers";   
}

// using messageList
if($container->hasError()) {
    // Error: we do something here.
    echo "we found ".$container->errorcount." errors in all lockers";
    
}

if ($container->errorcount>0) {
    // some error
}

echo $container->firstErrorText(); // returns first error if any
$array=$container->allError();  // MessageLocker[]
echo $array[0]->firstError(); 
$array=$container->allErrorArray();  // string[]
echo $array[0]; 

$css=$this-messageList->cssClasses('container1');

echo $container->resetAll(); // resets all lockers
$container->addItem('containerid','it is a message','error'); // we add an error in the container with #id containerid
$array=$container->allIds(); // ['containerid']
var_dump($validation->get('containerid'));  // object MessageLocker

$array=$this-messageList->items;
var_dump($this-messageList->items['containerid']); // object MessageLocker

if($container->hasError()) { // $validation->hasError() does the same
    echo "there is an error";
}

$container->get('idfield'); // container idfield 

echo $container->firstErrorText(); // we show the first error (if any) in the container
var_dump($container->allError); // we show the all errors