Copied to clipboard

Flag this post as spam?

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


  • Graham Carr 277 posts 389 karma points
    Nov 19, 2014 @ 16:39
    Graham Carr
    0

    Numerical Range Custom Fieldtype

    Has anyone implemented a custom fieldtype that forces a user to input a value between a specified range (e.g. 10000 - 12000) or is there a way of using an existing Contour field type to accomplish this?

  • Comment author was deleted

    Nov 20, 2014 @ 10:45

    Hi Graham,

    You'll need to create a new one for this, it should be pretty simply, add 2 props to the custom fieldtype  (like rangestart, rangestop) and then override the validate method where you check if the submitted value is between that range

  • Comment author was deleted

    Nov 20, 2014 @ 10:54

    Should be something like 

     [Attributes.Setting("RangeStart", description ="Enter the range start value", control ="Umbraco.Forms.Core.FieldSetting.TextField")]
            public string RangeStart { get; set; }
     
            [Attributes.Setting("RangeStop", description ="Enter the range stop value", control ="Umbraco.Forms.Core.FieldSetting.TextField")]
            public string RangeStop { get; set; }
     
            public override List<Exception> Validate(HttpContextBase httpContext)
            {
                var value = httpContext.Request[this.AssociatedField.Id.ToString()];
                //check range here and return a list of expections
            }

     

  • Comment author was deleted

    Nov 20, 2014 @ 11:02

    Full validate method could look like 

    publicoverrideList<Exception> Validate(HttpContextBase httpContext)
            {
                var value = httpContext.Request[this.AssociatedField.Id.ToString()];
                //check range here and return a list of expections
     
                if (string.IsNullOrEmpty(value))
                    return null;
     
                var val = 0;
                var startRange = 0;
                var stopRange = 0;
     
                if (int.TryParse(value, out val)
                    && int.TryParse(RangeStart, out startRange)
                    && int.TryParse(RangeStop, out stopRange))
                {
                    if (val > stopRange || val < startRange)
                        return  new List<Exception> {new Exception("Not in range")};
                }
     
               return null;
     
            }
  • Graham Carr 277 posts 389 karma points
    Nov 20, 2014 @ 14:26
    Graham Carr
    0

    Thanks for the in-depth information Tim, I shall give it a go :)

  • Comment author was deleted

    Nov 20, 2014 @ 15:11

    Great, let me know if that does the trick :)

  • Graham Carr 277 posts 389 karma points
    Nov 20, 2014 @ 18:22
    Graham Carr
    0

    Hi Tim,

    I have just remembered that I have client side validation enabled for the forms, and the solution obviously only validates server side when the Next button has been clicked on the page. What would be involved to add client side validation to a custom field type, do you have any examples?

    Thanks.

  • Comment author was deleted

    Nov 20, 2014 @ 18:33

    Since we use jquery validate you should be able to just use that, so if jquery validates unobtrisive supports it out of the box you'll just need to add the attributes to your input on your view

    If it doesn't supports it out of the box you can write an extender for jquery validate

  • Comment author was deleted

    Nov 20, 2014 @ 18:34

    Think you'll need to add data-val-range="The field Age must be between 1 and 130."data-val-range-max="130"data-val-range-min="1"

  • Graham Carr 277 posts 389 karma points
    Nov 20, 2014 @ 18:35
    Graham Carr
    0

    Thanks Tim, sounds interesting, I haven't delved into jquery validator before so I will have a read up on it and have a go with what you posted above

  • Comment author was deleted

    Nov 20, 2014 @ 18:36

    You can fetch the additional settings in the fieldtype view with Model.AdditionalSettings["key"]

     

  • Graham Carr 277 posts 389 karma points
    Dec 03, 2014 @ 18:46
    Graham Carr
    0

    Thanks Tim, worked a treat

Please Sign in or register to post replies

Write your reply to:

Draft