Skip to content

Instantly share code, notes, and snippets.

@mattcdavis1
Last active September 27, 2017 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattcdavis1/4f1bf7042608bf6c7c81540b988f08e0 to your computer and use it in GitHub Desktop.
Save mattcdavis1/4f1bf7042608bf6c7c81540b988f08e0 to your computer and use it in GitHub Desktop.
Custom Validator
<?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));
}
}
<?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