Search code examples
javascriptalpacajs

Fill alpaca properties with a variable


I need to assemble a form dynamically using Alpaca Forms. I have the code below:

$("#form").alpaca({        
    "schema": {            
        "type":"object",
        "properties": {                
            "email": {
                "type":"string",
                "title":"E-mail",
                "required":true
            },
            "mensagem": {
                "type":"string",
                "title":"Mensagem",
                "required":true
            }                                        
        }
    }
});

And it needs to be filled in using variables for it to be generic, because the amount of fields that the form will have will always vary. Ex: sometimes in addition to the "email", "mensagem" properties, I will have the "assunto", "remetente", "destinatario" properties

it would need to look something like this:

$("#form").alpaca({
    "schema" : { schemaVariable }        
});

Solution

  • What you want to achieve is possible, you just need to remove the braces from your variable and use proper JSON object declaration.

    "schema" : schemaVariable
    

    here's a working fiddle for this. Tell me if you need something else.