0

I created one custom validation attribute and work fine:

 public sealed class DuplicateUrlCheckerAttribute : ValidationAttribute
    {
        public string GetErrorMessage() => $"آدرس Url پیش تر توسط شما یا دیگری ثبت شده است";

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {

            var service = (IOrderValidationService)validationContext.GetService(typeof(IOrderValidationService));

            if (service.DuplicateUrl(value.ToString()))
            {
                return new ValidationResult(GetErrorMessage());
            }

            return ValidationResult.Success;
        }

    }

and I used in a razor page as remote validation (page remote):

 public class IndexModel : PageModel
    {
        readonly IViewerOrderService _viewerOrderService;

        public IndexModel(IViewerOrderService viewerOrderService)
        {
            _viewerOrderService = viewerOrderService;
        }

        [PageRemote(
         AdditionalFields = "__RequestVerificationToken",
         HttpMethod = "post",
         PageHandler = "CheckValidations"
        )]
        [BindProperty]
        public Order Order { get; set; }

        [Required]
        [Url]
        [RegularExpression(@"^(https?\:\/\/)?(www\.youtube\.com|youtu\.?be)\/.+$", ErrorMessage = "آدرس معتبر از سایت یوتیوب وارد کنید.")]
        [DuplicateUrlChecker]
        [PageRemote(
         AdditionalFields = "__RequestVerificationToken",
         HttpMethod = "post",
         PageHandler = "CheckValidations"
        )]
        [BindProperty]
        public string DuplicateUrl { get; set; }

        public void OnGet()
        {

        }
        public async Task<IActionResult> OnPostAsync(CancellationToken cancelationToken)
        {
            if (!ModelState.IsValid)
                return Page();



            Order.LinkUrl = DuplicateUrl;

            if (await _viewerOrderService.AddOrder(Order, cancelationToken))
            {
                TempData.Set("newOrder", Order);
                return RedirectToPage("ThankYou");
            }


            return Page();


        }


        public JsonResult OnPostCheckValidations()
        {
            if (!ModelState.IsValid)
                return new JsonResult(false);


            return new JsonResult(true);
        }
    }

still, work fine but the problem is that, don't show correctly ErrorMessage. in the view instead show this error

آدرس Url پیش تر توسط شما یا دیگری ثبت شده است"

, showing

'DuplicateUrl' is invalid.

even I used [DuplicateUrlChecker(ErrorMessage = "آدرس Url پیش تر توسط شما یا دیگری ثبت شده است") but still show

DuplicateUrl' is invalid.

And View is:

@page "{handler?}"
@model MakeMoney.Web.Pages.IndexModel
@using MakeMoney.Domain.Models.LinkOrder;
@{
  ViewData["Title"] = "دانلود ویدیو ";
}

<form id="wrapped" asp-page="/Index" method="POST">
  <input id="website" name="website" type="text" value="">
  <!-- Leave input above for security protection, read docs for details -->
  <div id="middle-wizard">
    <!-- First branch What Type of Project ============================== -->

...

  <!-- Last step ============================== -->
      <div class="submit step" id="end">
        <div class="question_title">
          <h3>فرم مشخصات</h3>
          <p>لطفا فیلد ها را با دقت پر کنید</p>
        </div>
        <div class="row justify-content-center">
          <div class="col-lg-5">
            <div class="box_general">
              <div class="form-group">
                <input type="email" asp-for="Order.Email" class="required form-control" title="لطفا ایمیل خود را وارد کنید" placeholder="ایمیل">
                <span class="text-danger" asp-validation-for="Order.Email"></span>
              </div>
              <div class="checkbox_questions">
                <input asp-for="Order.Inform" type="checkbox" class="icheck">
                <label>هر زمان روی کانال قرار گرفت مرا باخبرساز</label>
                <span class="text-danger" asp-validation-for="Order.Inform"></span>

              </div>
              <div class="form-group">
                <input type="text" asp-for="DuplicateUrl" class="required form-control" pattern="^(http\:\/\/)?(www\.youtube\.com|youtu\.?be)\/.+$" title="آدرس Url را وارد کنید" placeholder="لینک موردنظر" required>
                <span class="text-danger" asp-validation-for="DuplicateUrl"></span>

              </div>
              <div class="form-group add_bottom_30">
                <label>زیرنویس:</label>
                <div class="styled-select">
                  <select asp-for="Order.SubtitleType" class="required" asp-items="@Html.GetEnumSelectList<SubtitleType>()">
                  </select>
                  <span class="text-danger" asp-validation-for="Order.SubtitleType"></span>

                </div>
              </div>
              <div class="checkbox_questions">
                <input name="terms" type="checkbox" class="icheck required" value="yes" title="اجباری">
                <label> قبول <a href="#" data-toggle="modal" data-target="#terms-txt">شرایط و ضوابط</a></label>
              </div>
            </div>
            <!-- /box_general -->
          </div>
        </div>
        <!-- /row -->
      </div>
      <!-- /Last step ============================== -->
    </div>
  </div>
  <!-- /middle-wizard -->
  <div id="bottom-wizard">
    <button type="button" name="forward" class="forward">بعدی</button>
    <button type="submit" name="process" class="submit">ارسال</button>
    <button type="button" name="backward" class="backward">قبلی</button>
  </div>
  <!-- /bottom-wizard -->
</form>



@section Scripts {
  <partial name="_ValidationScriptsPartial" />
}

I don't know why this is happening. Can anyone help me?

5
  • @ShaharShokrani I edit the post and add view code, Nothing seems to be wrong. Commented Dec 2, 2019 at 15:58
  • @ShaharShokrani in remote validation and <span class="text-danger" asp-validation-for="DuplicateUrl"></span>Place of printing , but I don't know where this message comes from. but in server side validation is work fine ! Commented Dec 2, 2019 at 19:52
  • You can see these images remote validation: remoteValidationImage and server validation serverSideValidation Commented Dec 2, 2019 at 19:59
  • @ShaharShokrani I tried but it didn't make a difference Commented Dec 3, 2019 at 9:26
  • @ShaharShokrani yes, even I remove class attribute Commented Dec 3, 2019 at 9:39

1 Answer 1

1

Finally, I found the answer Just add ErrorMessage to PageRemote.!

     [PageRemote(
            ErrorMessage = "آدرس Url پیش تر توسط شما یا دیگری ثبت شده است",
         AdditionalFields = "__RequestVerificationToken",
         HttpMethod = "post",
         PageHandler = "CheckValidations"
        )]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.