Question

I have a variable that has all the fields that had an error during validation, ie:

hasError = {
  firstName: 'First name is required',
  lastName: 'Last name is required'
}

I then simply add the class if they exist in the error like so:

{?hasError.firstName}error-class{/hasError.firstName}

This is working great. Now, I also have some inputs that are arrays, like so:

<input type="text" name="friends[0][name]" value="">
<input type="text" name="friends[0][age]" value="">

And when they error out, they look like this in my hasError variable:

hasError = {
  friends-name-0: 'Friend name is required',
  friends-age-0: 'Friend age is required'
}

I thought I would be able to use $idx while looping through the friends array to check for it in hasError, but it doesn't seem to work.

How can this be done so that it actually works?

{#friends}
  {?hasError.friends-name-{$idx}}error-class{/hasError.friends-name-{$idx}}
{/friends}

I can change how the hasError key looks if need be (friends-name-0).

Was it helpful?

Solution

It seems I found a solution to this. I had to modify my data being sent to the template to look like this:

hasError: {
  friends: {
    name: {
      0: 'Friend name is required',
      1: 'Friend name is required'
    }
  }
}

And then change my template to the following:

{?hasError.friends.name[$idx]}error-class{/hasError.friends.name[$idx]}

There is so little out there for complex dust.js but I managed to stumble accoss an article that had what I was looking for. Here is the link in case it could help someone else:

https://www.paypal-engineering.com/2013/12/05/dust-is-eloquent-round-and-round-we-go-2/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top