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?

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Nov 20, 2014 @ 10:45
    Tim Geyssens
    0

    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

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Nov 20, 2014 @ 10:54
    Tim Geyssens
    0

    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
            }

     

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Nov 20, 2014 @ 11:02
    Tim Geyssens
    0

    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 :)

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Nov 20, 2014 @ 15:11
    Tim Geyssens
    0

    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.

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Nov 20, 2014 @ 18:33
    Tim Geyssens
    0

    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

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Nov 20, 2014 @ 18:34
    Tim Geyssens
    0

    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

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Nov 20, 2014 @ 18:36
    Tim Geyssens
    0

    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

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies