vue-form-generator example

https://github.com/icebob/vue-form-generator

by wenyi

HTML

<script src="https://unpkg.com/[email protected]/dist/vue.min.js"></script>
<script src="https://unpkg.com/[email protected]"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/vfg.css">
<h1 class="text-center">Demo of vue-form-generator</h1>
  <div class="container" id="app">
    <div class="panel panel-default">
      <div class="panel-heading">Vue Form</div>
      <div class="panel-body">
        <vue-form-generator :schema="schema" :model="model" :options="formOptions"></vue-form-generator>
      </div>
    </div>
    <div class="panel panel-default">
      <div class="panel-heading">HTML Form</div>
      <div class="panel-body">
        <div class="vue-form-generator">
          <div class="form-group field-input">
            <label for="html-date">Date</label>
            <div class="field-wrap">
              <div class="wrapper">
                <input id="html-date" type="date" class="form-control" />              
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>


    <div class="panel panel-default">
      <div class="panel-heading">Model</div>
      <div class="panel-body">
        <pre v-if="model" v-html="prettyJSON(model)"></pre>
      </div>
    </div>

    <div class="panel panel-default">
      <div class="panel-heading">Schema</div>
      <div class="panel-body">
        <pre v-if="model" v-html="prettyJSON(schema)"></pre>
      </div>
    </div>
  </div>

CSS

html {
	font-family: Tahoma;
	font-size: 14px;
}

body {
	font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
	font-size: 14px;
	line-height: 1.42857143;
	color: #333;
}

pre {
	overflow: auto;
}
	pre .string { color: #885800; }
	pre .number { color: blue; }
	pre .boolean { color: magenta; }
	pre .null { color: red; }
	pre .key { color: green; }    

h1 {
	text-align: center;
	font-size: 36px;
	margin-top: 20px;
	margin-bottom: 10px;
	font-weight: 500;
}

fieldset {
	border: 0;
}

.panel {
	margin-bottom: 20px;
	background-color: #fff;
	border: 1px solid transparent;
	border-radius: 4px;
	-webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, .05);
	box-shadow: 0 1px 1px rgba(0, 0, 0, .05);
	border-color: #ddd;
}

.panel-heading {
	color: #333;
	background-color: #f5f5f5;
	border-color: #ddd;

	padding: 10px 15px;
	border-bottom: 1px solid transparent;
	border-top-left-radius: 3px;
	border-top-right-radius: 3px;        
}

.panel-body {
	padding: 15px;
}				

.field-checklist .wrapper {
	width: 100%;
}

JavaScript

VueFormGenerator.validators.is_int = function(value, field, model) {
	if(true) {
  	return ["11Is not an intenger"];
  }
  return [];
}

VueFormGenerator.validators.resources.textTooSmall = "Too1 short, should be {0} and is {1}";

var vm = new Vue({
    el: "#app",
	
    components: {
        "vue-form-generator": VueFormGenerator.component
    },

    data() {
			return {
        model: {
					//selected: "option 1"
        },
        schema: {
            fields: [{
            	type: "input",
              inputType: "text",
              label: "Date",
              model: "date_field",
              format: "YYYY-MM-DD",
              min: 2,
              max: 4,
              validator: ["string", "is_int"],
            },
            {
            	type: "submit",
              onSubmit: () => {
              	console.log("submit")
              }
            }]
        },

        formOptions: {
            validateAfterLoad: true,
            validateAfterChanged: true
        }
			};
    },
    
    methods: {
        prettyJSON: function(json) {
            if (json) {
                json = JSON.stringify(json, undefined, 4);
                json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
                return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function(match) {
                    var cls = 'number';
                    if (/^"/.test(match)) {
                        if (/:$/.test(match)) {
                            cls = 'key';
                        } else {
                            cls = 'string';
                        }
                    } else if (/true|false/.test(match)) {
                        cls = 'boolean';
                    } else if (/null/.test(match)) {
                        cls = 'null';
                    }
                    return '<span class="' + cls + '">' + match + '</span>';
                });
        ...