Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon Hebert 22 posts 92 karma points
    Sep 02, 2015 @ 20:58
    Simon Hebert
    0

    Add specific class names to elements of a form (Umbraco Forms)

    Hi!

    Our need is to apply CSS classes to input elements generated by Umbraco Forms for execute some predefined animations. I know that there is many unique class names generated on each fields, but we can't use these names and we need to apply specific names provided by JavaScript/CSS frameworks (jQuery, Bootstrap...) like "col-lg-12 buttons animated".

    Is there is a way to do this? I know i can modify the FieldType.xyz HTML templates. But each fields of the same type in the same form can have different class names in our case...

    A good solution would be to have an option in the form field editor of each fields for specify a list of CSS classes. This way, we could customize the styling of the elements of the form.

    Any other way to do this?

    Thank you for your help!

    Simon

  • Mark Bowser 273 posts 860 karma points c-trib
    Sep 02, 2015 @ 21:57
    Mark Bowser
    1

    Not sure if this is the ideal solution, but I'll just throw the option out there anyway. You could build custom field types in place of the defaults provided by Umbraco Forms. You can do stuff like this:

    public sealed class SomeSortOfFieldWithAClass : FieldType
    {
        [global::Umbraco.Forms.Core.Attributes.Setting("CSS Class", control = "Umbraco.Forms.Core.FieldSetting.TextField", description = "CSS class for dropdown")]
        public string CssClass { get; set; }
    
        public SomeSortOfFieldWithAClass ()
        {
            Name = "Some Sort of Field with a Class";
            Id = new Guid("5bb40b77-20e8-423c-ac69-ef25599f4601");
            Description = "You can set the class on me!";
            Icon = "dropdownlist.png";
            DataType = FieldDataType.String;
        }
    
        public override string RenderPreview()
        {
            return "<input type=\"text\"/>";
        }
    
        public override string RenderPreviewWithPrevalues(List<object> prevalues)
        {
            return RenderPreview();
        }
    }
    

    And then, create /umbraco/Plugins/umbracoContour/Views/FieldType.SomeSortOfFieldWithAClass.cshtml. I'd base it off of one of the existing views, but it might look something like this:

    @model Umbraco.Forms.Mvc.Models.FieldViewModel
    
    @{
        var mandatoryAttributes = string.Format("data-val=\"true\" data-val-required=\"{0}\"", Model.RequiredErrorMessage);
        var cssClass = Model.AdditionalSettings.First(s => s.Key == "CssClass").Value;
    }
    
    <!-- Markup here! -->
    

    It definitely feels gross to have to replace each of the contour field types with your own... In my case, I only needed the css class on my custom field type, so it wasn't a big deal. I'll definitely be following this thread to see if any awesome ideas spring up.

  • Maria 34 posts 128 karma points
    Sep 03, 2015 @ 06:40
  • Simon Hebert 22 posts 92 karma points
    Sep 03, 2015 @ 13:17
    Simon Hebert
    0

    @Mark Bowser : Thanks for your detailed example. Maybe this is the best solution to my problem right now. But this way, as you said, I need to create a custom field for each type of fields (dropdown, text, radio, textarea, etc.) used in the form. Is your code actually work with Contour or Forms?

    @Maria : I've already seen these information, but unfortunately, customizing markup for all the form is not a viable solution since I need to define different css class to different elements of the same type in a form.

    @Umbraco Team : Any chance to have a "Field css class" option implemented in the Forms backoffice for each type of fields like the one existing on the form "Styling" section (Form css class)?

  • Mark Bowser 273 posts 860 karma points c-trib
    Sep 03, 2015 @ 17:21
    Mark Bowser
    0

    Simon, the code I posted was for Contour, but I believe the code is very similar. I'll bet intellisense would work out a lot of the kinks for you. Sorry I don't have an Umbraco Forms example on me.

  • Simon Hebert 22 posts 92 karma points
    Sep 03, 2015 @ 20:52
    Simon Hebert
    0

    Mark,

    Yes, it's a good starting point for porting the code to Umbraco Forms!

    The perfect solution would be to have an option in each form field configuration in the back end that let the user declaring customized attributes to the HTML elements generated. Like css class or data-* HTML5 attributes!

    Hope that the Umbraco Team will take the need in consideration.

    Simon

Please Sign in or register to post replies

Write your reply to:

Draft