Search code examples
javascriptangularjsangular-schema-form

angular-schema-form array length


I have this schema:

    {
     "type": "object",
     "title": "Comment",
     "required": [
     "comments"
      ],
  "properties": {
    "comments": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "title": "Name",
            "type": "string"
          },
          "email": {
            "title": "Email",
            "type": "string",
            "pattern": "^\\S+@\\S+$",
            "description": "Email will be used for evil."
          },
          "spam": {
            "title": "Spam",
            "type": "boolean",
            "default": true
          },
          "comment": {
            "title": "Comment",
            "type": "string",
            "maxLength": 20,
            "validationMessage": "Don't be greedy!"
          }
        },
        "required": [
          "name",
          "comment"
        ]
      }
    }
  }
}

This is an array of comments. I can add and remove comments.

How can I render 2 items of the array always by default ?

I've tried with maxItems and minItems but those parameters don't render items.


Solution

  • There are two ways to do it currently.

    First is to set them in the default model so it looks something like:

    $scope.model = {
        "comments": [{},{}]
    }
    

    The second would be to use onChange on the array:

    "onChange": function(val) { if(val.length < 2) val.push({}); }
    

    The difference between the two being that the onChange will not allow it to drop below the minimum length you set while the first option is just for the initial default.