# Validation: reduce boilerplate

**URL:** <https://discourse.aurelia.io/t/validation-reduce-boilerplate/521>\
**Category:** Help Requests\
**Created:** [December 12, 2017, 4:09pm UTC](https://discourse.aurelia.io/t/validation-reduce-boilerplate/521 "2017-12-12T16:09:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![migajek](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/migajek/32/379_2.png) [@migajek](https://discourse.aurelia.io/u/migajek)\
**Post date:** [December 12, 2017, 4:09pm UTC](https://discourse.aurelia.io/t/validation-reduce-boilerplate/521/1 "2017-12-12T16:09:44Z")

</div>

Hi,  
Is it possible to avoid polluting every form-view controller with

```
this.controller = ctrlFactory.createForCurrentScope();
this.controller.addRenderer(new BootstrapFormRenderer());        

```

?

My case is an app with significant amount of forms, all of them built with unified styling and structure (Bootstrap)  
While I like the Aurelia Validation in general, I dislike boilerplate code.

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [December 13, 2017, 8:03am UTC](https://discourse.aurelia.io/t/validation-reduce-boilerplate/521/2 "2017-12-13T08:03:05Z")

</div>

From the code of `ValidationControllerFactory`: [https://github.com/aurelia/validation/blob/master/src/validation-controller-factory.ts](https://github.com/aurelia/validation/blob/master/src/validation-controller-factory.ts)

You can do this:

```auto

import { Container } from 'aurelia-framework';
import { ValidationController, Validator, PropertyAccessorParser } from 'aurelia-validation';

/**
 * Creates BootstrapRenderrerValidationControllerFactory instances.
 */
export class BootstrapRenderrerValidationControllerFactory {
  public static get(container: Container) {
    return new BootstrapRenderrerValidationControllerFactory (container);
  }

  constructor(private container: Container) { }

  /**
   * Creates a new controller instance.
   */
  public create(validator?: Validator) {
    if (!validator) {
      validator = this.container.get(Validator) as Validator;
    }
    const propertyParser = this.container.get(PropertyAccessorParser) as PropertyAccessorParser;
    return new ValidationController(validator, propertyParser);
  }

  /**
   * Creates a new controller and registers it in the current element's container so that it's
   * available to the validate binding behavior and renderers.
   */
  public createForCurrentScope(validator?: Validator) {
    const controller = this.create(validator);
    this.container.registerInstance(ValidationController, controller);
    // add here
    controller.addRenderer(new BootstrapFormRenderer());
    return controller;
  }
}

(BootstrapRenderrerValidationControllerFactory as any)['protocol:aurelia:resolver'] = true;

```

Then you can inject and call `createForCurrentScope` only. Or you can go further and only need to inject your own validation controller that extends `ValidationController`, and has `BootstrapRenderer` with it by default:

```auto
export class BootstrapValidationController extends ValidationController {
  constructor(validator) {
    super(validator);
    this.addRenderer(new BootstrapFormRenderer());
  }
}

```

* * *

Edit:

There is another way:

```auto

import {ValidationControllerFactory } from 'aurelia-validation'

ValidationControllerFactory .prototype.createWithRenderer = function(renderer) {
  let controller = this.createForCurrentScope();
  controller.addRenderer(renderer || new BootstrapFormRenderer());
  return controller;
}

```
