-
-
Save mattcdavis1/4f1bf7042608bf6c7c81540b988f08e0 to your computer and use it in GitHub Desktop.
Custom Validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Union\AppModule\Forms\Legal6; | |
use Anomaly\Streams\Platform\Ui\Form\FormBuilder; | |
use Anomaly\Streams\Platform\Ui\Form\FormValidator; | |
use Anomaly\Streams\Platform\Ui\Form\Event\FormWasValidated; | |
use Illuminate\Support\MessageBag; | |
class MyCustomValidator extends FormValidator | |
{ | |
public function handle(FormBuilder $builder) | |
{ | |
return parent::handle($builder); | |
} | |
public function validateAll(FormBuilder $builder) | |
{ | |
$factory = app('validator'); | |
$builder->setFormErrors(new MessageBag()); | |
$this->extender->extend($factory, $builder); | |
$input = $builder->getFormEntry()->fresh()->toArray(); | |
$messages = $this->messages->make($builder); | |
$attributes = $this->attributes->make($builder); | |
$rules = $this->rules->compile($builder); | |
/* @var Validator $validator */ | |
$validator = $factory->make($input, $rules); | |
$validator->setCustomMessages($messages); | |
$validator->setAttributeNames($attributes); | |
$this->setResponse($validator, $builder); | |
$this->events->fire(new FormWasValidated($builder)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Union\AppModule\Http\Controller; | |
class MyController | |
{ | |
public function edit($id, $step) | |
{ | |
// stuff | |
if (!$myForm->hasErrors() && $myForm->getResponse()) { | |
if (request('action') == 'save_approval') { | |
$myFormBuilder->setValidator('My\Module\Forms\CustomValidator@validateAll'); | |
$myFormBuilder->validate(); | |
$errorsAll = $myFormBuilder->getFormErrors(); | |
// more stuff | |
} | |
return $this->handleFormResponse($legal6Entry, $step); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment