Skip to content

Instantly share code, notes, and snippets.

@mattcdavis1 mattcdavis1/CustomValidator.php Secret
Last active Sep 27, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.