Ejemplo n.º 1
0
        /// <summary>
        /// Processes the specified context.
        /// </summary>
        /// <param name="context">The context to process.</param>
        public override void Process(FormBuilderContext context)
        {
            FormArrayAttribute arrayAttribute = context.Property.GetCustomAttribute <FormArrayAttribute>();

            // Test to the form subobject attribute
            if (arrayAttribute != null)
            {
                if (context.Property.PropertyType.GetInterfaces()
                    .Where(i => i.GetTypeInfo().IsGenericType)
                    .Select(i => i.GetGenericTypeDefinition()).Any(i => i == typeof(IEnumerable <>)))
                {
                    // Get the subtype from a generic type argument
                    Type subType = context.Property.PropertyType.GetGenericArguments()[0];

                    // Create the subform
                    JContainer properties = context.FormBuilder.BuildForm(subType, context.OriginDtoType, context.TargetCulture, context.FullPropertyPath + "[]");

                    // Merge the properties of the sub object into the current context
                    JObject currentFormElement = context.GetOrCreateCurrentFormElement();
                    currentFormElement["key"]   = context.FullPropertyPath;
                    currentFormElement["items"] = properties;

                    if (!string.IsNullOrEmpty(arrayAttribute.AddButtonTitle))
                    {
                        string addText = GetTextForKey(arrayAttribute.AddButtonTitle, context);
                        currentFormElement["add"] = new JValue(addText);
                    }
                }
                else
                {
                    throw new InvalidOperationException("An " + nameof(FormArrayAttribute) + " must always be on a property with a type derived from IEnumerable<>");
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Processes the specified context.
        /// </summary>
        /// <param name="context">The context to process.</param>
        public override void Process(SchemaBuilderContext context)
        {
            FormArrayAttribute arrayAttribute = context.Property.GetCustomAttribute <FormArrayAttribute>();

            if (arrayAttribute != null)
            {
                JObject currentSchemaObject = context.Element.GetOrCreateSchemaObject();

                if (context.Property.PropertyType.GetInterfaces()
                    .Where(i => i.GetTypeInfo().IsGenericType)
                    .Select(i => i.GetGenericTypeDefinition()).Any(i => i == typeof(IEnumerable <>)))
                {
                    // The property is an generic enumerable type
                    Type underlyingType = context.Property.PropertyType.GetGenericArguments()[0];

                    // Build the schema for a complext type
                    JObject complexSchema = context.SchemaBuilder.BuildSchema(underlyingType, context.OriginDtoType, context.TargetCulture);

                    currentSchemaObject["title"] = new JValue(context.Property.Name);
                    currentSchemaObject["type"]  = new JValue("array");
                    currentSchemaObject["items"] = complexSchema;

                    // Add minItems constraint if available
                    if (arrayAttribute.MinItems > 0)
                    {
                        currentSchemaObject["minItems"] = new JValue(arrayAttribute.MinItems);
                    }

                    // Add maxItems constraint if available
                    if (arrayAttribute.MaxItems > 0)
                    {
                        currentSchemaObject["maxItems"] = new JValue(arrayAttribute.MaxItems);
                    }
                }
                else
                {
                    throw new InvalidOperationException("The FormArray attibute may be used only on properties using types implementing IEnumerable<>.");
                }
            }
        }