Conditional remote validation in Razor Pages

Blooming Developer 276 Reputation points
2022-03-15T02:51:22.363+00:00

Hi @Zhi Lv - MSFT ,

I have to validate a field based on another fields input. If Field A is not empty, then Field B should be filled out. Currently this form is inside a tab, so when i do the javascript validation it is navigating to the first tab and also not validating the others fields too. Is there anyway i can use conditional remote validation?

PFA the attached codes.183095-contractor-hrms.txt

Any help would be appreciated.Thanks!
Teena

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,244 questions
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,046 Reputation points Microsoft Vendor
    2022-03-22T09:51:39.557+00:00

    Hi @Blooming Developer ,

    I dont want to reload the page, could you please let me know whether i can use PageRemote for this.?

    The PageRemote attribute uses client side script to validate user input on the server without posting the entire form. Generally, we can use it to check whether the input value is exist or not. Then, base on the request to show the validation message. It doesn't apply to your scenario: the conditional validation, based on one input (property) to validate or not validate another property.

    So to the conditional validation, we have to use the custom validation attributes(such as the RequiredIfAttribute). And you also found that, by using the custom validation attributes, it will validate the data on server side, if you want to do the validation on client side. You have to add the custom client side validation.

    You can refer the following sample code:

    The RequiredIfAttribute.cs:

    185534-image.png

    Add the custom client side validation:

    185430-image.png

    You can view the source code from here: 185568-sourcecode.txt

    The result like this: it will use the custom client side validation.

    185623-1.gif

    More detail information about custom client side valiation, see Custom client-side validation.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion


1 additional answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,046 Reputation points Microsoft Vendor
    2022-03-16T06:59:06.123+00:00

    Hi @Blooming Developer ,

    Yes im using a partial view. And if Workpass type is empty ,i dont want to validate(not hide) Workpassexpiry date,else i want to check workpassexpiry date is empty or not. And my project is using asp.net core 3.1

    In this scenario, there is no need to use remote page validation. You can try to create a custom RequiredIfAttribute attribute, then use it on the WorkpassExpiryDate property. Refer the following steps:

    1. Create a RequiredIfAttribute class: 183583-image.png
    2. Create a BiodataViewModel model contains the relate properties: 183592-image.png
    3. Create a _BioData.cshtml page like this: 183584-image.png
    4. Create a CreateBioData page with page model The CreateBioData.cshtml
      Note: For the client side validation, you can use F12 developer tools to check html elements and find the relates <span> tag to display the error message. And, here I directly use JQuery to validate the enter value, you can also add custom client side validation via jQuery Validation, refer Custom client-side validation 183468-image.png The CreateBioData.cshtml.cs 183585-image.png

    You can view the source code from here: 183586-sourcecode.txt

    The result like this:

    183469-1.gif


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion