This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hi, I am working on a backend form plugin. If a user navigates to a non-existent id, how do I redirect to 404? Currently I get an exception.
Form behavior has not been initialized, check that you have called initForm() in your controller.
Type: undefined
Exception: System\Classes\ApplicationException
http://www.example.com/backend/author/plugin/controller/preview/999
(ps: I sure wish this forum had a preview button)
Last updated
I'd question how you end up trying to update a non-existent ID. Your interface shouldn't allow this to happen.
I get that you'd like to show a nicer error, though.
When I try this with RainLab.User
, I get the following message:
Form record with an ID of 123456 could not be found.
This is because the update.htm
correctly intercepts the fatal error. See below.
... Breadcrumbs and stuff ...
<?php if (!$this->fatalError): ?>
... Normal form stuff - render the form, the buttons, sidebar, etc. ...
<?php else: ?>
<div class="padded-container">
<div class="control-breadcrumb">
<?= Block::placeholder('breadcrumb') ?>
</div>
<p class="flash-message static error"><?= e($this->fatalError) ?></p>
<p><a href="<?= Backend::url('rainlab/user/users') ?>" class="btn btn-default">Return to users list</a></p>
</div>
<?php endif ?>
If the fatal error isn't being caught in your plugin, adjust your update.htm
(and the rest of your forms) to include that.
-- Edit: Actually, if I remove this check, I just get a 404.
initForm
is called by the update(..)
action on the base Controller
:
public function update($recordId = null, $context = null)
{
try {
$this->context = strlen($context) ? $context : $this->getConfig('update[context]', 'update');
$this->controller->pageTitle = $this->controller->pageTitle ?: $this->getLang('update[title]', 'backend::lang.form.update_title');
$model = $this->controller->formFindModelObject($recordId);
$this->initForm($model);
$this->controller->vars['formModel'] = $model;
}
catch (Exception $ex) {
$this->controller->handleError($ex);
}
}
Make sure that your controller extends Controller
, and public $implement = [ 'Backend.Behaviors.FormController' ];
If you're still getting the error and you've checked all that, it'd be helpful to see your controller code.
Last updated
1-2 of 2