Copied to clipboard

Flag this post as spam?

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


  • Marcio Goularte 374 posts 1346 karma points
    Jun 26, 2017 @ 12:56
    Marcio Goularte
    0

    Custom validation UI-O-Matic V2

    ------Umbraco version 7.6.3 assembly: 1.0.6361.21154---------

    Hello guys. I'm using A UI-O-Matic V2 with a Repository implementation. I am developing a registration system and one of the rules of business is to check if there is already a registration for a student and course selected. Since V2 is by data annotations, I tried to use Remote Validation, but it does not work. For now I am generating an Exception, but it is not friendly, since it opens the dialog with Exception and stacktrace. Is there any way to do this type of validation? Some Code:

            public override uEnrollment Create(uEnrollment model)
        {
            if (model.ItemId > 0 && model.MemberId > 0)
            {
                if (_enrollmentservice.ExistsEnrollmentToMemberByItem(model.ItemId, model.MemberId))
                {
                    throw new Exception("It is not possible to enroll. This student is already enrolled in this course");
    
                }
                    var entity = _enrollmentservice.EnrollmentManual(model.MemberId, model.ItemId);
                model = Mapper.Map<uEnrollment>(entity);
            }
            return model;            
        }
    

    //Property

     [Remote("ExistsEnrollmentToMemberByItem", "CursoSurface",
    AdditionalFields = "MemberId,ItemId",
    ErrorMessage = "It is not possible to enroll. This student is already enrolled in this course")]
        public bool Exists { get; set; }
    

    //Surface Controller Action

    [HttpPost]
            public JsonResult ExistsEnrollmentToMemberByItem(int ItemId, int MemberId)
            {
    
                if (!_enrollmentservice.ExistsEnrollmentToMemberByItem(ItemId, MemberId))
                {
                    return Json("It is not possible to enroll. This student is already enrolled in this course", JsonRequestBehavior.AllowGet);
                }
    
                return Json(true, JsonRequestBehavior.AllowGet);
            }
    

    I'm displaying the error like this: (Not friendly) enter image description here

  • Comment author was deleted

    Jun 26, 2017 @ 13:43

    Hmm I'll check why remote val isn't working, must say I haven't tried it...

    You could also rework your code into an actual validation attribute by creating a custom attribute... Does that make sense?

  • Marcio Goularte 374 posts 1346 karma points
    Jun 26, 2017 @ 14:01
    Marcio Goularte
    0

    Makes sense. I've even started to develop the attribute. I had thought about it, but I did not because I do not know how the validation is made. If Unobtrusive client side is used. I know it's got a call to an API: enter image description here

      So I've already been on the Remote path.

      I will report the result of the attribute development here.

    UI-O-Matic is great! First time I'm using!

  • Marcio Goularte 374 posts 1346 karma points
    Jun 26, 2017 @ 17:34
    Marcio Goularte
    0

    Reporting. The custom attribute worked perfectly. Displays the message as desired.

    public class ExistsEnrollmentAttribute : ValidationAttribute, IClientValidatable
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
    
           //to do:I need to inject this.            
            var _enrollmentservice = new EnrollmentService();
    
            var model = (uEnrollment)validationContext.ObjectInstance;
            if (model.ItemId == 0)
            {
                return new ValidationResult("required");
            }
            if (model.MemberId == 0)
            {
                return new ValidationResult("required");
            }            
    
            if (_enrollmentservice.ExistsEnrollmentToMemberByItem(model.ItemId, model.MemberId))
            {
                return new ValidationResult("It is not possible to enroll. This student is already enrolled in this course");
            }
    
            return ValidationResult.Success;
    
        }
    
        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(
           ModelMetadata metadata,
           ControllerContext context)
        {
            var modelClientValidationRule = new ModelClientValidationRule
            {
                ValidationType = "existsEnrollment",
                ErrorMessage = this.FormatErrorMessage(metadata.DisplayName)
            };
    
            return new List<ModelClientValidationRule> { modelClientValidationRule };
        }
    }
    
        //model    
        [ExistsEnrollment]
        public bool Exists { get; set; }
    

    enter image description here

    Thanks a lot Tim.

    I believe this path is better than using remote data annotations

Please Sign in or register to post replies

Write your reply to:

Draft