Reputation: 1199
please help me with ensureForEach
, I cannot find documents of that point. I have a class:
export class EnterReplacements {
replacements: Replacement[] = null;
constructor(
private router: Router,
private eventAggregator: EventAggregator,
private repoCreator: RepoCreator,
protected validation: Validation
) {
this.validation = validation.on(this)
.ensureForEach('replacements')
.ensure('value')
.isNotEmpty()
.etc(); // repeat all the same stuff as in the Item validation
}
}
It's get an error in runtime:
ERROR [app-router] Error: Error instantiating EnterReplacements. Check the inner error for details.
This's my html:
<div class="col-xs-12 col-sm-6 col-lg-4" repeat.for="replacement of replacements">
<input class="string-input" type="text" value.bind="replacement.value" placeholder.bind="replacement.friendlyName" change.delegate="$parent.onChanged()"/>
</div>
I need to validate all value
property of items in replacements
array is not empty. Please help !!!
Upvotes: 3
Views: 2469
Reputation: 13194
As per the latest release of Aurelia-Validation, the Rules
for validating that an array is filled can be done through 2 ways:
The simplest way is with minItems(count)
, for example we would use minItems(1)
to make sure the array is filled with at least 1 item.
The second way which also works (we could use this for very specific validation rules)
satisfies(value => Array.isArray(value) && value.length > 0)
Upvotes: 2
Reputation: 616
You could use a generic function for validation and do it yourself (which would show on the top level as a validation for the array)
this.validation = validation.on(this)
.ensure('replacements')
.isNotEmpty()
.passes(replacementsArray => {
let valid = true;
replacementsArray.forEach(v => {
valid = v != null && valid;
});
return valid;
});
Things to be wary of:
- isNotEmpty()
will run first (regardless of where it appears)
- I believe isNotEmpty is necessary to get .passes()
to run (that's been my experience, though I coudln't see a mention of that in the docs)
- I don't actually know what you want to validate for each value, so I've done a null equivalence check, which I think would satisfy your isNotEmpty
intentions? You will probably want to change that.
Upvotes: 1