Display Validation Errors

Permalink 1 user found helpful
Maybe I wasn't reading the docs properly, but I couldn't find anything. It seems to be a minor problem, but I just don't get it:

I'd like to display validation errors on a single page (the proper way ;-):
So I submit the form to the controller and there I load the
$e = Loader::helper('validation/error');

After checking if the posted variable is valid, I add an error to $e:
$mail_logo = $this->post('mail_logo');
if (!isset($mail_logo) || empty($mail_logo) || strlen($mail_logo) == 0) {
    $e->add(t('The field "%s" is required', 'mail_logo'));
    return $this->set('error_message', $e);
}
In the single page, if I just print the error with 'var_dump($error_message)', it is filled.
But If I set
return $this->set('error', $e);
(instead of 'error_message') in the controller, the variable $error in the single page is NULL.

Here's the question: Do I have to set a bootstrap error message like
<div class="alert alert-warning">...
myself or is there any helper which I'm just missing?

EDIT:
I've found the helper
Loader::element('system_errors', array('error' => $error));
, but How to use it?

daenu
 
goodnightfirefly replied on at Permalink Reply
goodnightfirefly
The DashboardPageController already has the validation/error helper loaded for us, so no need to have that bit (I'm assuming this is a dashboard page and you are extending that class).

Here is a snippet from concrete/src/Page/Controller/DashboardPageController.php which shows our helper already being created and sent to the view for us:

<?php
namespace Concrete\Core\Page\Controller;
class DashboardPageController extends PageController
{
    /** @var Error */
    protected $error;
    ...
    public function on_start()
    {
        $this->error = Loader::helper('validation/error');
    ....
    public function on_before_render()
    {
        $this->set('error', $this->error);
    ....


So since we already have $error available to us, we can just validate things and add to the existing $error object like so:

if (!isset($mail_logo) || empty($mail_logo) || strlen($mail_logo) == 0) {
    $this->error->add(t('The field "%s" is required', 'mail_logo'));
}


No need to return the error object or $this->set() as $error is automatically sent to the view as previously mentioned.
daenu replied on at Permalink Reply
daenu
Ok that is clear. But now i get
Call to a member function add() on a non-object
error. why is that?
goodnightfirefly replied on at Permalink Reply
goodnightfirefly
Can you post the code snippet you are using?

If you are extending the DashboardPageController class and using
$this->error
it should work (not $error or $e, has to be $this->error).
daenu replied on at Permalink Reply
daenu
The controller class:
class Settings extends DashboardPageController


And inside the method 'set_settings()':
if (!isset($mail_logo) || strlen($mail_logo) == 0 || $mail_logo == '0') {
    $this->error->add(t('The field "%s" is required', 'Logo fürs E-Mail'));
}
Is it because I'm not using it in the view()' method?
goodnightfirefly replied on at Permalink Reply
goodnightfirefly
That should be working, you haven't defined $error somewhere before that have you?
daenu replied on at Permalink Reply
daenu
That is why I tried another solution (hack). Because I had this strange error before. Nope I'm not using $this->error or $error before or for other things.... Could it be, because it's in a package?
goodnightfirefly replied on at Permalink Reply
goodnightfirefly
It should be fine in a package, my code is packaged.

Do you want to message me your package and I'll debug it from my end?
daenu replied on at Permalink Reply
daenu
Mhhm, Thank you but you can't be bothered, really... the code is still full of var_dumps and stuff, but maybe an issue could be, that I'm overriding the 'on_start()' method?
But if I do a
parent::on_start
it's still the same... Here's my on_start():
public function on_start()
    {
        parent::on_start();
        $this->requireAsset('css', 'danielgasser', 'css/danielgasser.css');
        $this->requireAsset('javascript', 'danielgasser', 'js/danielgasser.js');
    }
Even after uninstalling/reinstalling the package. Now any ideas?
daenu replied on at Permalink Best Answer Reply
daenu
So YESS, that was it. By overriding the on_start() method, I had to add the parents method too in order to get the predefined error stuff:
public function on_start()
    {
        $this->requireAsset('css', 'danielgasser', 'css/danielgasser.css');
        $this->requireAsset('javascript', 'danielgasser', 'js/danielgasser.js');
        parent::on_start();
    }

BTW Because of other operations after the validation, I had to add
if($this->error->has()) return;
to exit the method and return to the single page.
Thx to goodnightfirefly for supporting!!
goodnightfirefly replied on at Permalink Reply
goodnightfirefly
Excellent :) Glad you got it working!