Validation, Delegate and App lifecycle hooks

I have been working on a typical Aurelia 2 application and have run into a few minor challenges. All have a work-around, but seem different from Aurelia 1.

  1. With Aurelia 1, I would show the errors as a group when the error count was > 1. But now there is a result that contains the value “Valid”, so the count is > 0. I have looked for a property on the ValidationController that tells me everything is valid, but I don’t see one. Is there anything that gives the overall status of validation?

  2. In the past, I used a value converter to change the language of all text items on a page. That was triggered using a change.delegate. Everything I have seen implied to me that that should just work and that there is no need to register the delegate, but I get an error: Error: AUR0009: Attempted to jitRegister something that is not a constructor: ‘au:resource:binding-command:delegate’. Did you forget to register this resource? Using change.trigger works fine. Do I need to do anything special to register this resource?

  3. The main App class doesn’t seem to have the same lifecycle hooks as other components. I was able to work around this, but it took a while to figure out that it doesn’t work like it did with Aurelia 1. Maybe I didn’t create it correctly?

Thank you!

The validation controller has a results property which is an array of ValidationResult.

<ul>
  <li repeat.for="result of validationController.results">
    <template if.bind="!result.valid">${result}</template>
  </li>
</ul>

If one of those result is not valid, then you can say that overall status is not valid.

The Au2 binding language no longer includes delegate. However, if you want to have it, you may use the compat-v1 package.

Consider sharing code example.

Thank you once again for answering my questions, I really appreciate it.

For the errors, I used something like this:

For errors tied to an element I used something like this:

class.bind="validationController.results.some(r => r.object === customer && r.propertyName === 'name' && !r.valid) ? 'form-control is-invalid' : 'form-control'"

and then displayed the errors with something like this:

        <template if.bind="validationController.results && validationController.results.some(r => !r.valid)">
            <div repeat.for="error of validationController.results" class="alert alert-danger" role="alert">
                <strong>${error}</strong>
            </div>
        </template>

I was thinking I missed something that consolidated the error status that could be used in the UI.

I changed delegate to change.trigger, and that worked fine and didn’t realize that delegate didn’t make the cut.

In my app, I was just doing some initialization and with Aurelia 1, I put that in an activate method, which was called. I was able to move everything to the constructor, so there is no problem. It was just code to setup things like cache and locale.

Overall, none of these difficult to overcome, just things that I didn’t expect.

1 Like

Glad that you have worked it out! :partying_face:

Q: why a value convertor and not the default I18n plugin/binding behaviour? You can easily (re)use the corresponding signal.

I think I should take a look that. Just skimming through the first pages of the I18N plugin it appears that it is more configurable than I thought. When I added this, I think the only option was to use a json file. I am currently using a json file to initially load translations, but site-specific overrides are stored in a database. All of this is brought in using a dotnetcore api.