示例#1
0
        public async Task <IActionResult> PutFormlySchema(int id, FormlySchema formlySchema)
        {
            if (id != formlySchema.Id)
            {
                return(BadRequest());
            }

            _context.Entry(formlySchema).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FormlySchemaExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
示例#2
0
        public void Test()
        {
            // act
            var schema = FormlySchema.FromType <ClassLevelType>();

            // assert
            _testOutputHelper.WriteLine(schema.ToJson());

            schema.Should().BeEquivalentTo(
                new FormlyFieldConfig
            {
                Type       = "tabs",
                FieldGroup = new FormlyFieldConfigCollection
                {
                    new FormlyFieldConfig
                    {
                        Key  = nameof(ClassLevelType.FirstName),
                        Type = "input"
                    },
                    new FormlyFieldConfig
                    {
                        Key        = nameof(ClassLevelType.Complex),
                        FieldGroup = new FormlyFieldConfigCollection
                        {
                            new FormlyFieldConfig
                            {
                                Key  = nameof(ComplexType.Address),
                                Type = "input"
                            }
                        }
                    }
                }
            });
        }
示例#3
0
        public async Task <ActionResult <FormlySchema> > GetFormlySchema(int id)
        {
            FormlySchema formlySchema = await _context.FormlySchema
                                        .Include(j => j.Schema).ThenInclude(s => s.TemplateOptions)
                                        .Include(j => j.Schema).ThenInclude(s => s.TemplateOptions).ThenInclude(g => g.Options)
                                        .Include(j => j.Schema).ThenInclude(s => s.Validation).ThenInclude(w => w.Messages)
                                        .Include(j => j.Schema).ThenInclude(s => s.FieldGroup).ThenInclude(k => k.DefaultValue)
                                        .Include(j => j.Schema).ThenInclude(s => s.FieldGroup).ThenInclude(k => k.TemplateOptions).ThenInclude(z => z.Options)
                                        .Include(j => j.Schema).ThenInclude(s => s.FieldGroup).ThenInclude(k => k.FieldArray).ThenInclude(z => z.FieldGroup).ThenInclude(t => t.TemplateOptions).ThenInclude(g => g.Options)
                                        .Include(j => j.Schema).ThenInclude(s => s.FieldGroup).ThenInclude(k => k.FieldArray).ThenInclude(z => z.FieldGroup).ThenInclude(t => t.Validation).ThenInclude(s => s.Messages)
                                        .SingleAsync(j => j.Id == id);

            if (formlySchema == null)
            {
                return(NotFound());
            }
            foreach (ChildFormlySchema x in formlySchema.Schema)
            {
                if (x.FieldGroup.Count == 0)
                {
                    x.FieldGroup = null;
                }
            }

            return(formlySchema);
        }
示例#4
0
        public void EmptyType()
        {
            // act
            var schema = FormlySchema.FromType <EmptyType>();

            // assert
            schema.Should().BeEmpty();
            _testOutputHelper.WriteLine(schema.ToJson());
        }
示例#5
0
        public async Task <ActionResult <FormlySchema> > PostFormlySchema(ChildFormlySchema[] formlySchemaArray)
        {
            FormlySchema formlySchema           = new FormlySchema();
            Collection <ChildFormlySchema> list = new Collection <ChildFormlySchema>();

            formlySchema.Schema = list;
            foreach (ChildFormlySchema item in formlySchemaArray)
            {
                formlySchema.Schema.Add(item);
            }
            _context.FormlySchema.Add(formlySchema);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetFormlySchema", new { id = formlySchema.Id }, formlySchema));
        }
        public void Arrays()
        {
            // act
            var schema = FormlySchema.FromType <CollectionRoot>();

            // assert
            _testOutputHelper.WriteLine(schema.ToJson());
            schema.Should().BeEquivalentTo(
                new FormlyFieldConfig
            {
                Key        = nameof(CollectionRoot.Names),
                Type       = "repeat",
                FieldArray = new FormlyFieldConfig
                {
                    Type = "input"
                }
            });
        }
示例#7
0
        public void NestedForms()
        {
            // act
            var schema = FormlySchema.FromType <Parent>();

            // assert
            _testOutputHelper.WriteLine(schema.ToJson());
            schema.Should().BeEquivalentTo(
                new FormlyFieldConfig
            {
                Key = nameof(Parent.EmptyChild),
                FieldGroupClassName = "custom-class",
                FieldGroup          = FormlyFieldConfigCollection.Empty
            },
                new FormlyFieldConfig
            {
                Key        = nameof(Parent.ChildWithData),
                FieldGroup = new FormlyFieldConfigCollection
                {
                    new FormlyFieldConfig
                    {
                        Key  = nameof(Parent.ChildWithData.FirstName),
                        Type = "input"
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key  = $"{nameof(Parent.InlineChildWithData)}.{nameof(Parent.InlineChildWithData.ZipCode)}",
                Type = "input"
            },
                new FormlyFieldConfig
            {
                Key  = $"{nameof(InlineChildWithNestedData)}.{nameof(InlineChildWithNestedData.Data)}",
                Type = "input"
            },
                new FormlyFieldConfig
            {
                Key  = $"{nameof(InlineChildWithNestedData)}.{nameof(InlineChildWithNestedData.InlineChildWithData)}.{nameof(InlineChildWithNestedData.InlineChildWithData.ZipCode)}",
                Type = "input"
            });
        }
示例#8
0
        public void DataTypeAttribute()
        {
            // act
            var schema = FormlySchema.FromType <VariousInputTypes>();

            // assert
            schema.Should().BeEquivalentTo(
                new FormlyFieldConfig
            {
                Key             = nameof(VariousInputTypes.DateTime),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Type = "datetime-local"
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(VariousInputTypes.Date),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Type = "date"
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(VariousInputTypes.Time),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Type = "time"
                }
            },
                new FormlyFieldConfig
            {
                Key  = nameof(VariousInputTypes.Duration),
                Type = "input",
            },
                new FormlyFieldConfig
            {
                Key             = nameof(VariousInputTypes.PhoneNumber),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Type = "tel"
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(VariousInputTypes.Currency),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Type = "number"
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(VariousInputTypes.Text),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Type = "text"
                }
            },
                new FormlyFieldConfig
            {
                Key  = nameof(VariousInputTypes.Html),
                Type = "textarea",
            },
                new FormlyFieldConfig
            {
                Key  = nameof(VariousInputTypes.MultilineText),
                Type = "textarea",
            },
                new FormlyFieldConfig
            {
                Key             = nameof(VariousInputTypes.EmailAddress),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Type = "email"
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(VariousInputTypes.Password),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Type = "password"
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(VariousInputTypes.Url),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Type = "url"
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(VariousInputTypes.ImageUrl),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Type = "url"
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(VariousInputTypes.CreditCard),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Type = "tel"
                }
            },
                new FormlyFieldConfig
            {
                Key  = nameof(VariousInputTypes.PostalCode),
                Type = "input",
            },
                new FormlyFieldConfig
            {
                Key             = nameof(VariousInputTypes.Upload),
                Type            = "file",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Type = "file"
                }
            }
                );

            _testOutputHelper.WriteLine(schema.ToJson());
        }
示例#9
0
        public void GodTest()
        {
            // act
            var schema = FormlySchema.FromType <Foo>();

            // assert
            schema.Should().BeEquivalentTo(
                new FormlyFieldConfig
            {
                Key          = nameof(Foo.FirstName),
                Type         = "input",
                DefaultValue = "Dan"
            },
                new FormlyFieldConfig
            {
                Key  = nameof(Foo.IsMarried),
                Type = "checkbox"
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.Dob),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Type = "datetime-local"
                }
            },
                new FormlyFieldConfig
            {
                Key  = nameof(Foo.DataMemberNotSet),
                Type = "input"
            },
                new FormlyFieldConfig
            {
                Key  = "DataMemberProp",
                Type = "input"
            },
                new FormlyFieldConfig
            {
                Key  = nameof(Foo.JsonPropertyNotSet),
                Type = "input"
            },
                new FormlyFieldConfig
            {
                Key  = "JsonPropertyProp",
                Type = "input"
            },
                new FormlyFieldConfig
            {
                Key  = nameof(Foo.DisplayNameNotSet),
                Type = "input"
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.DisplayName),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Label = nameof(Foo.DisplayName) + "Prop"
                }
            },
                new FormlyFieldConfig
            {
                Key  = nameof(Foo.DisplayNotSet),
                Type = "input"
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.Display),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Label = nameof(Foo.Display) + "Prop"
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.Required),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Required = true
                },
                Validation = new Validation
                {
                    Messages = new MessageDictionary {
                        { "required", "Custom" }
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.JsonRequired),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Required = true
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.DataMemberRequired),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Required = true
                }
            },
                new FormlyFieldConfig
            {
                Key            = nameof(Foo.HideExpression),
                Type           = "input",
                HideExpression = "some expression"
            },
                new FormlyFieldConfig
            {
                Key            = nameof(Foo.HideExpressionFunction),
                Type           = "input",
                HideExpression = new JRaw("(model) => !this.model.firstName")
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.Placeholder),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Placeholder = "PlaceholderValue"
                }
            },
                new FormlyFieldConfig
            {
                Key  = nameof(Foo.ExpressionProperty),
                Type = "input",
                ExpressionProperties = new ExpressionPropertyDictionary
                {
                    { "templateOptions.disabled", "!model.text" },
                    { "a", "b" }
                }
            },
                new FormlyFieldConfig
            {
                Key  = nameof(Foo.ExpressionPropertyFunction),
                Type = "input",
                ExpressionProperties = new ExpressionPropertyDictionary
                {
                    { "a", new JRaw("() => b") }
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.RangeInt),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Min  = 5,
                    Max  = 10,
                    Type = "number"
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.RangeDouble),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Min  = 5.5,
                    Max  = 10.5,
                    Type = "number"
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.MaxLength),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    MaxLength = 5,
                },
                Validation = new Validation
                {
                    Messages = new MessageDictionary {
                        { "maxLength", "Custom" }
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.MinLength),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    MinLength = 5
                },
                Validation = new Validation
                {
                    Messages = new MessageDictionary {
                        { "minLength", "Custom" }
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.RegExp),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Pattern = "[a-z]"
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.ReadOnly),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Disabled = true,
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.Description),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Description = "Custom",
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.DisplayDescription),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Description = "Custom",
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.Select),
                Type            = "select",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Options = new OptionCollection
                    {
                        new { label = "Red", value = "Red" },
                        new { label = "Green", value = "Green" },
                        new { label = "Blue", value = "Blue" }
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.SelectWithDisplayNames),
                Type            = "select",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Options = new OptionCollection
                    {
                        new { label = "Midnight Green", value = "GreenColor" },
                        new { label = "Deep Blue", value = "BlueColor" }
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key      = nameof(Foo.CustomWrapper),
                Type     = "input",
                Wrappers = new WrapperCollection {
                    "panel1", "panel2"
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.FlagsEnumMultiselect),
                Type            = "select",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Multiple = true,
                    Options  = new OptionCollection
                    {
                        new { label = "None", value = "None" },
                        new { label = "Create", value = "Create" },
                        new { label = "Read", value = "Read" },
                        new { label = "Update", value = "Update" },
                        new { label = "Delete", value = "Delete" },
                        new { label = "All", value = "All" },
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.FlagsEnumMultiselectOverriden),
                Type            = "select",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Options = new OptionCollection
                    {
                        new { label = "None", value = "None" },
                        new { label = "Create", value = "Create" },
                        new { label = "Read", value = "Read" },
                        new { label = "Update", value = "Update" },
                        new { label = "Delete", value = "Delete" },
                        new { label = "All", value = "All" },
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key  = nameof(Foo.FieldType),
                Type = "custom"
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.InputType),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Type = "custom"
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.TextAreaRows),
                Type            = "textarea",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Rows = 6
                }
            },
                new FormlyFieldConfig
            {
                Key        = nameof(Foo.CustomValidators),
                Type       = "input",
                Validators = new Validators
                {
                    Validation = new ValidatorsValidationCollection
                    {
                        "ip", "ip2", "ip3"
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.CustomAsyncValidators),
                Type            = "input",
                AsyncValidators = new Validators
                {
                    Validation = new ValidatorsValidationCollection
                    {
                        "ipAsync", "ip2Async"
                    }
                },
                Validators = new Validators
                {
                    Validation = new ValidatorsValidationCollection
                    {
                        "ip3"
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.CustomFunctionValidators),
                Type            = "input",
                AsyncValidators = new Validators
                {
                    Validation = new ValidatorsValidationCollection
                    {
                        new JRaw("ipAsync"), new JRaw("ip2Async")
                    }
                },
                Validators = new Validators
                {
                    Validation = new ValidatorsValidationCollection
                    {
                        new JRaw("ip3"),
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.Marvel),
                Type            = "select",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Options = new OptionCollection
                    {
                        new { label = "Iron Man", value = "IronMan", group = "Male" },
                        new { label = "Captain America", value = "CaptainAmerica", group = "Male" },
                        new { label = "Black Widow", value = "BlackWidow", group = "Female" },
                        new { label = "Captain Marvel", value = "CaptainMarvel" },
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.RightAddon),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    AddonRight = new Addon
                    {
                        Class = "right class",
                        Text  = "right text",
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(Foo.LeftAddon),
                Type            = "input",
                TemplateOptions = new FormlyTemplateOptions
                {
                    AddonLeft = new Addon
                    {
                        Class = "left class",
                        Text  = "left text",
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key       = nameof(Foo.WithClassName),
                Type      = "input",
                ClassName = "custom-class",
            });

            LogSchema(schema);
        }
示例#10
0
        public void Test()
        {
            // act
            var schema = FormlySchema.FromType <MemberSelectData>();

            // assert
            schema.Should().BeEquivalentTo(
                new FormlyFieldConfig
            {
                Key             = nameof(MemberSelectData.SameTypeProperty),
                Type            = "select",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Options = new OptionCollection
                    {
                        new { label = "label1", value = "value1", group = "group1" },
                        new { label = "label2", value = "value2" },
                        new { label = "label3", value = "value3", group = "group3" },
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(MemberSelectData.SameTypeMethod),
                Type            = "select",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Options = new OptionCollection
                    {
                        new { label = "label1", value = "value1", group = "group1" },
                        new { label = "label2", value = "value2", group = "group2" },
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(MemberSelectData.OtherTypeProperty),
                Type            = "select",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Options = new OptionCollection
                    {
                        new { label = "label1", value = "value1", group = "group1" },
                        new { label = "label2", value = "value2" },
                        new { label = "label3", value = "value3", group = "group3" },
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(MemberSelectData.OtherTypeMethod),
                Type            = "select",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Options = new OptionCollection
                    {
                        new { label = "label1", value = "value1", group = "group1" },
                        new { label = "label2", value = "value2", group = "group2" },
                    }
                }
            },
                new FormlyFieldConfig
            {
                Key             = nameof(MemberSelectData.CustomProperties),
                Type            = "select",
                TemplateOptions = new FormlyTemplateOptions
                {
                    Options = new OptionCollection
                    {
                        new { label = "label1", value = "value1", group = "group1" },
                        new { label = "label2", value = "value2", group = "group2" },
                    }
                }
            }
                );
        }
        public void TypeWithNested()
        {
            var fieldConfigCollection = FormlySchema.FromType <OrderFooWithNested>();

            fieldConfigCollection.Select(x => x.Key).Should().BeInAscendingOrder();
        }
        public void PlainType()
        {
            var fieldConfigCollection = FormlySchema.FromType <OrderFoo>();

            fieldConfigCollection.Select(x => x.Key).Should().BeInAscendingOrder();
        }
示例#13
0
        public void Test()
        {
            // arrange
            var expectedFormlySchema = @"[
  {
    ""key"": ""Text"",
    ""type"": ""input"",
    ""templateOptions"": {
      ""label"": ""Text"",
      ""placeholder"": ""Formly is terrific!"",
      ""required"": true
    }
  },
  {
    ""key"": ""Nested.Story"",
    ""type"": ""textarea"",
    ""templateOptions"": {
      ""label"": ""Some sweet story"",
      ""placeholder"": ""It allows you to build and maintain your forms with the ease of JavaScript :-)""
    },
    ""expressionProperties"": {
      ""focus"": ""formState.awesomeIsForced"",
      ""templateOptions.description"": (model, formState) =>
        formState.awesomeIsForced ? 'And look! This field magically got focus!' : null
    }
  },
  {
    ""key"": ""Awesome"",
    ""type"": ""checkbox"",
    ""expressionProperties"": {
      ""templateOptions.disabled"": ""formState.awesomeIsForced"",
      ""templateOptions.label"": (model, formState) =>
        formState.awesomeIsForced
          ? ""Too bad, formly is really awesome...""
          : ""Is formly totally awesome? (uncheck this and see what happens)""
    }
  },
  {
    ""key"": ""WhyNot"",
    ""type"": ""textarea"",
    ""hideExpression"": ""model.awesome"",
    ""templateOptions"": {
      ""label"": ""Why Not?"",
      ""placeholder"": ""Type in here... I dare you""
    },
    ""expressionProperties"": {
      ""templateOptions.placeholder"": (model, formState) =>
        formState.awesomeIsForced
          ? ""Too bad... It really is awesome! Wasn't that cool?""
          : ""Type in here... I dare you"",
      ""templateOptions.disabled"": ""formState.awesomeIsForced""
    }
  },
  {
    ""key"": ""Custom"",
    ""type"": ""custom"",
    ""templateOptions"": {
      ""label"": ""Custom inlined""
    }
  }
]";

            // act
            var formlySchema     = FormlySchema.FromType <IntroductionExample>();
            var formlySchemaJson = formlySchema.ToJson();

            // assert
            _testOutputHelper.WriteLine(formlySchemaJson);
            formlySchemaJson.Should().Be(expectedFormlySchema);
        }