Custom Validation
Introduction
Validate your form based on a third party condition, like an external api response.
Extend Plugin
Create your own plugin or use this one as template: https://github.com/skydiver/october-plugin-formsextender
First, create a new partial on your theme.
This file will be used to display your custom error.
Let's call ajax_error.htm
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<strong>Something went wrong!</strong>
</div>
Now we need to add our custom logic to thrown an exception and display our custom error:
Replace your boot method with:
public function boot() {
\Event::listen('martin.forms.beforeSaveRecord', function (&$formdata, $component) {
// replace with your own logic
$somethingWentWrong = true;
if ($somethingWentWrong) {
$controller = \App::make(\Cms\Classes\Controller::class);
throw new \AjaxException([
'#genericForm_forms_flash' => $controller->renderPartial(
'_ajax_error.htm',
['remoteError' => 'something failed]
)
]);
}
});
}
Notes:
genericForm
is your component alias- underscore on
_ajax_error.htm
it's not an error, file name needs to be prefixed using_
- second argument of
renderPartial
it's an array with custom values passed to the partial