示例#1
0
        public static async Task <IFormBuilder <T> > Field <T>(this IFormBuilder <T> builder, string name, Func <Task <string> > prompt, ActiveDelegate <T> active = null, ValidateAsyncDelegate <T> validate = null)
            where T : class
        {
            string prmpt = await prompt();

            return(builder.Field(name, prmpt, active, validate));
        }
示例#2
0
        public IForm <T> Build(Assembly resourceAssembly = null, string resourceName = null)
        {
            if (!_form._steps.Any((step) => step.Type == StepType.Field))
            {
                var paths = new List <string>();
                FormBuilder <T> .FieldPaths(typeof(T), "", paths);

                IFormBuilder <T> builder = this;
                foreach (var path in paths)
                {
                    builder.Field(new FieldReflector <T>(path));
                }
                builder.Confirm(new PromptAttribute(_form.Configuration.Template(TemplateUsage.Confirmation)));
            }
            if (resourceAssembly == null)
            {
                resourceAssembly = typeof(T).Assembly;
            }
            if (resourceName == null)
            {
                resourceName = typeof(T).FullName;
            }
            var lang = resourceAssembly.GetCustomAttribute <NeutralResourcesLanguageAttribute>();

            if (lang != null && !string.IsNullOrWhiteSpace(lang.CultureName))
            {
                try
                {
                    IEnumerable <string> missing, extra;
                    string name = null;
                    foreach (var resource in resourceAssembly.GetManifestResourceNames())
                    {
                        if (resource.Contains(resourceName))
                        {
                            var pieces = resource.Split('.');
                            name = string.Join(".", pieces.Take(pieces.Count() - 1));
                            break;
                        }
                    }
                    if (name != null)
                    {
                        var rm = new ResourceManager(name, resourceAssembly);
                        var rs = rm.GetResourceSet(Thread.CurrentThread.CurrentUICulture, true, true);
                        _form.Localize(rs.GetEnumerator(), out missing, out extra);
                        if (missing.Any())
                        {
                            throw new MissingManifestResourceException($"Missing resources {missing}");
                        }
                    }
                }
                catch (MissingManifestResourceException)
                {
                    // Resource was not localized
                }
            }
            Validate();
            return(this._form);
        }
示例#3
0
        /// <summary>
        /// Define a step for filling in a particular value in the form state.
        /// </summary>
        /// <param name="builder">Form builder.</param>
        /// <param name="name">Path in the form state to the value being filled in.</param>
        /// <param name="active">Delegate to test form state to see if step is active.</param>
        /// <param name="validate">Delegate to validate the field value.</param>
        /// <remarks>
        /// This step will use reflection to construct everything needed for a dialog from a combination
        /// of the <see cref="DescribeAttribute"/>, <see cref="TermsAttribute"/>, <see cref="PromptAttribute"/>, <see cref="OptionalAttribute"/>
        /// <see cref="NumericAttribute"/> and <see cref="TemplateAttribute"/> annotations that are supplied by default or you
        /// override.
        /// </remarks>
        /// <returns>This form.</returns>
        public static IFormBuilder <T> Field <T>(this IFormBuilder <T> builder, string name, ActiveDelegate <T> active = null, ValidateAsyncDelegate <T> validate = null)
            where T : class
        {
            var field = (active == null ? new FieldReflector <T>(name) : new Conditional <T>(name, active));

            if (validate != null)
            {
                field.SetValidate(validate);
            }
            return(builder.Field(field));
        }
    public static IForm <AdaptiveCardsFormFlow> BuildForm()
    {
        IFormBuilder <AdaptiveCardsFormFlow> formBuilder = GetFormbuilder();

        var built = formBuilder
                    .Field(nameof(Name), "What is your name?")
                    .Field(nameof(RequestedDate))
                    .Confirm("Is this information correct? {*}")
                    .Build();

        return(built);
    }
        public static IFormBuilder <T> DateTimeRangeField <T>(this IFormBuilder <T> formBuilder, string name, ActiveDelegate <T> active = null, ValidateAsyncDelegate <T> validate = null)
            where T : class
        {
            var prompt = typeof(T).GetProperty(name).GetCustomAttributes(typeof(PromptAttribute), false).Cast <PromptAttribute>().First();

            return(formBuilder.Field(new FieldReflector <T>(name)
                                     .SetType(typeof(string))
                                     .SetPrompt(prompt)
                                     .SetFieldDescription(name)
                                     .SetActive(active)
                                     .SetValidate(validate)));
        }
示例#6
0
        public static IForm<ScheduleCallbackDialog> BuildForm()
        {
            IFormBuilder<ScheduleCallbackDialog> formBuilder = GetFormbuilder();

            var built = formBuilder
                .Field(nameof(Name), "What is your name?")
                .Field(nameof(PhoneNumber), "Please enter your phone number.")
                .Field(nameof(RequestedDate))
                .Confirm("Is this information correct? {*}")
                .Build();

            return built;
        }
示例#7
0
        public static IFormBuilder <InterviewModel> AddQuestion(this IFormBuilder <InterviewModel> model, string fieldName, string prompt)
        {
            model.Field(new FieldReflector <InterviewModel>(fieldName)
                        .SetDefine(async(state, field) =>
            {
                field.SetPrompt(new PromptAttribute(prompt)
                {
                    ChoiceStyle = ChoiceStyleOptions.AutoText
                });
                return(true);
            }));

            return(model);
        }
示例#8
0
        /// <summary>
        /// Define a step for filling in a particular value in a JObject as defined by a JSON Schema.
        /// </summary>
        /// <param name="builder">Form builder.</param>
        /// <param name="schema">JSON schema defining JObject.</param>
        /// <param name="name">Path in the form state to the value being filled in.</param>
        /// <param name="active">Delegate to test form state to see if step is active.</param>
        /// <param name="validate">Delegate to validate the field value.</param>
        /// <returns>This form.</returns>
        /// <remarks>
        /// See <see cref="FieldJson"/> for a description of JSON Schema extensions
        /// for defining your fields.
        /// </remarks>
        public static IFormBuilder <JObject> Field(this IFormBuilder <JObject> builder, JObject schema, string name, ActiveDelegate <JObject> active = null, ValidateAsyncDelegate <JObject> validate = null)
        {
            var field = new FieldJson(schema, name);

            if (active != null)
            {
                field.SetActive(active);
            }
            if (validate != null)
            {
                field.SetValidate(validate);
            }
            return(builder.Field(field));
        }
示例#9
0
        /// <summary>
        /// Add any remaining fields defined in <paramref name="schema"/>.
        /// </summary>
        /// <param name="builder">Where to add any defined fields.</param>
        /// <param name="schema">JSON Schema that defines fields.</param>
        /// <param name="exclude">Fields not to include.</param>
        /// <returns>Modified <see cref="IFormBuilder{T}"/>.</returns>
        /// <remarks>
        /// See <see cref="FieldJson"/> for a description of JSON Schema extensions
        /// for defining your fields.
        /// </remarks>
        public static IFormBuilder <JObject> AddRemainingFields(this IFormBuilder <JObject> builder, JObject schema, IEnumerable <string> exclude = null)
        {
            var exclusions = (exclude == null ? new string[0] : exclude.ToArray());
            var fields     = new List <string>();

            Fields(schema, null, fields);
            foreach (var field in fields)
            {
                if (!exclusions.Contains(field) && !builder.HasField(field))
                {
                    builder.Field(schema, field);
                }
            }
            return(builder);
        }
示例#10
0
        /// <summary>
        /// Add all fields not already added to the form.
        /// </summary>
        /// <param name="builder">Where to add fields.</param>
        /// <param name="exclude">Fields not to include.</param>
        /// <returns>Modified <see cref="IFormBuilder{T}"/>.</returns>
        /// <remarks>
        /// This will add all fields defined in your form that have not already been
        /// added if the fields are supported.
        /// </remarks>
        public static IFormBuilder <T> AddRemainingFields <T>(this IFormBuilder <T> builder, IEnumerable <string> exclude = null)
            where T : class
        {
            var exclusions = (exclude == null ? new string[0] : exclude.ToArray());
            var paths      = new List <string>();

            FormBuilder <T> .FieldPaths(typeof(T), "", paths);

            foreach (var path in paths)
            {
                if (!exclusions.Contains(path) && !builder.HasField(path))
                {
                    builder.Field(new FieldReflector <T>(path));
                }
            }
            return(builder);
        }
示例#11
0
        public IForm <T> Build()
        {
            if (!_form._steps.Any((step) => step.Type == StepType.Field))
            {
                var paths = new List <string>();
                FormBuilder <T> .FieldPaths(typeof(T), "", paths);

                IFormBuilder <T> builder = this;
                foreach (var path in paths)
                {
                    builder.Field(new FieldReflector <T>(path));
                }
                builder.Confirm("Is this your selection?\n{*}");
            }
            Validate();
            return(this._form);
        }
示例#12
0
 /// <summary>
 /// Define a step for filling in a particular value in a JObject as defined by a JSON Schema.
 /// </summary>
 /// <param name="builder">Form builder.</param>
 /// <param name="schema">JSON schema defining JObject.</param>
 /// <param name="name">Path in the form state to the value being filled in.</param>
 /// <param name="prompt">Simple \ref patterns to describe prompt for field.</param>
 /// <param name="active">Delegate to test form state to see if step is active.n</param>
 /// <param name="validate">Delegate to validate the field value.</param>
 /// <returns>This form.</returns>
 /// <remarks>
 /// See <see cref="FieldJson"/> for a description of JSON Schema extensions
 /// for defining your fields.
 /// </remarks>
 public static IFormBuilder <JObject> Field(this IFormBuilder <JObject> builder, JObject schema, string name, string prompt, ActiveDelegate <JObject> active = null, ValidateAsyncDelegate <JObject> validate = null)
 {
     return(builder.Field(schema, name, new PromptAttribute(prompt), active, validate));
 }
示例#13
0
 /// <summary>
 /// Define a step for filling in a particular value in the form state.
 /// </summary>
 /// <param name="builder">Form builder.</param>
 /// <param name="name">Path in the form state to the value being filled in.</param>
 /// <param name="prompt">Simple \ref patterns to describe prompt for field.</param>
 /// <param name="active">Delegate to test form state to see if step is active.n</param>
 /// <param name="validate">Delegate to validate the field value.</param>
 /// <returns>This form.</returns>
 /// <remarks>
 /// This step will use reflection to construct everything needed for a dialog from a combination
 /// of the <see cref="DescribeAttribute"/>, <see cref="TermsAttribute"/>, <see cref="PromptAttribute"/>, <see cref="OptionalAttribute"/>
 /// <see cref="NumericAttribute"/> and <see cref="TemplateAttribute"/> annotations that are supplied by default or you
 /// override.
 /// </remarks>
 public static IFormBuilder <T> Field <T>(this IFormBuilder <T> builder, string name, string prompt, ActiveDelegate <T> active = null, ValidateAsyncDelegate <T> validate = null)
     where T : class
 {
     return(builder.Field(name, new PromptAttribute(prompt), active, validate));
 }