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
After checking if the posted variable is valid, I add an error to $e:
But If I set(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 myself or is there any helper which I'm just missing?
EDIT:
I've found the helper, but How to use 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:
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);
Here's the question: Do I have to set a bootstrap error message like
<div class="alert alert-warning">...
EDIT:
I've found the helper
Loader::element('system_errors', array('error' => $error));
Ok that is clear. But now i get error. why is that?
Call to a member function add() on a non-object
Can you post the code snippet you are using?
If you are extending the DashboardPageController class and using it should work (not $error or $e, has to be $this->error).
If you are extending the DashboardPageController class and using
$this->error
The controller class:
And inside the method 'set_settings()': Is it because I'm not using it in the view()' method?
class Settings extends DashboardPageController
And inside the method 'set_settings()': Is it because I'm not using it in the view()' method?
That should be working, you haven't defined $error somewhere before that have you?
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?
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?
Do you want to message me your package and I'll debug it from my end?
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 it's still the same... Here's my on_start():
Even after uninstalling/reinstalling the package. Now any ideas?
But if I do a
parent::on_start
public function on_start() { parent::on_start(); $this->requireAsset('css', 'danielgasser', 'css/danielgasser.css'); $this->requireAsset('javascript', 'danielgasser', 'js/danielgasser.js'); }
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:
BTW Because of other operations after the validation, I had to add to exit the method and return to the single page.
Thx to goodnightfirefly for supporting!!
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;
Thx to goodnightfirefly for supporting!!
Excellent :) Glad you got it working!
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:
So since we already have $error available to us, we can just validate things and add to the existing $error object like so:
No need to return the error object or $this->set() as $error is automatically sent to the view as previously mentioned.