How to validate blan value based on conditions

I have an 10 field in model class and I have set required field ( mandatory  field ) for all validation.

I have used syncfusion control to bind this model.

The validation is working fine as per our requirement using Data Annotation 

I would like to know below clarification for validation 


I have 10 fields in model class,and I have stored this field in separate table with three login field

For example 

Login1

Login2

Login3


Login1:-

Login1 should validate field1 to field4 

Login2:-

Login2 should validate field3 to field6

Login3:-

Login3 should validate field7 to field10






15 Replies

MK Muthukumar Kannan Syncfusion Team July 21, 2021 03:42 PM UTC

Hi KINS, 

Thanks for contacting Syncfusion support.  

We have validated your query for “validate single model properties in different pages” at our end. According to that, we can able to achieve your requirement with using Syncfusion components. However, we have added the sample for that and attached below. 


Could you please check whether your requirement is addressed or not with the above sample? 

Please let us know if we have misunderstood your query. 

Regards, 
Muthukumar K 



KI KINS July 21, 2021 04:22 PM UTC

Thanks for effort..

The above code is not related to our requirement. 

Please see my actual requirement with example.


In above example code , you have created three razor file (login1,login2,login3) and single model class.


But my requirement is single razor file (customer.razor) and single model class with login wise validation 


For example :-

In customer.razor file

For login1,email ID is required but phone no is not required 

For login2,email ID is not required but phone no is required 

For login3, both are required

For login4,both are not required 


Note:-

I have maintained (required or not required ) for each field in separate table.


I would like to validate required field based on this condition 


Hope it's clear...



JL Joshna Lingala Uthama Reddy Lingala Syncfusion Team July 22, 2021 03:06 PM UTC

Hi KINS, 

Thanks for your shared information. 

We have validated your reported query “validate single model properties in different pages” with your shared information at our end. We would like to let you know that the requirement can be achieved by overriding IsValid method for custom attribute. We have prepared a sample for your reference in which we have validated the Field1 to success only when the value is User1. Similarly, you can also override IsValid based on you condition to validate only if specific user is logged in. 
 

    public class LoginAttribute : ValidationAttribute 
    { 
        public string ValidValue { get; set; } 
        protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
        { 
            var lowerValue = value?.ToString().ToLower(); 
            if (!string.IsNullOrEmpty(lowerValue) && lowerValue.Equals(ValidValue.ToLower()))  
                return null; 
            return new ValidationResult(ErrorMessage, new[] { validationContext.MemberName }); 
        } 
    } 
    public class FormData 
    { 
        [LoginAttribute(ErrorMessage ="Enter valid message", ValidValue = "User1")] 
        public string Field1 { get; set; } 


Please try the above sample and get back to us if you need further assistance. 

Regards, 
Joshna L 



KI KINS July 26, 2021 05:30 PM UTC

Thanks for reply 


Is it possible to visible synfussion control true or false based on above scenario 



JL Joshna Lingala Uthama Reddy Lingala Syncfusion Team July 27, 2021 09:42 AM UTC

Hi KINS, 

Thanks for your update 

We have validated your query for “ possibility of syncfusion controls visibility” at our end. We want to let you know that the requirement can be achieved by adding if condition for the required controls. We have prepared a sample for your reference in which we have added condition for Field2 using value of Field1 i.e., if Field1 value is proper then Field2 is visible in UI. Similarly, you can add you own conditioning for your required fields. 

 
            <div> 
                <label for="Name" style="padding: 20px 0 5px 0">Field1</label> 
                <SfTextBox id='textbox' @bind-Value="@model.Field1" Placeholder="Enter a Field1"></SfTextBox> 
                <ValidationMessage For="@(() => model.Field1)" /> 
            </div> 
            @if(model.Field1 != null && model.Field1 == "User1") 
             { 
                <div> 
                    <label for="Name" style="padding: 20px 0 5px 0">Field2</label> 
                    <SfTextBox id='textbox' @bind-Value="@model.Field2" Placeholder="Enter a Field2"></SfTextBox> 
                    <ValidationMessage For="@(() => model.Field2)" /> 
                </div> 
             } 
 

Also, you can disable the textbox control using Enabled property, please refer to the following UG. 


Please try the above sample and get back to us if you need further assistance. 

Regards, 
Joshna L 



KI KINS July 31, 2021 03:34 PM UTC

I have one more clarification 

How can  I add css class for required field (* which should be in red color) as per above code.



JL Joshna Lingala Uthama Reddy Lingala Syncfusion Team August 2, 2021 07:57 AM UTC

Hi KINS, 

Thanks for your update. 

We have validated your query “adding Css to add * for required fields” at our end. We let you know that your requirement can be achieved by adding a class in label tag and simple css to that class. We have prepared a sample for your reference in which we have added Css to display * in red color for required fields, which can be downloaded from the below link. 


<div> 
    <label for="Name" style="padding: 20px 0 5px 0" class="required">Field3</label> 
    <SfTextBox id='textbox' @bind-Value="@model.Field3" Placeholder="Enter a Field3"></SfTextBox> 
    <ValidationMessage For="@(() => model.Field3)" /> 
</div> 
 
<div> 
    <label for="Name" style="padding: 20px 0 5px 0" class="required">Field4</label> 
    <SfTextBox id='textbox' @bind-Value="@model.Field4" Placeholder="Enter a Field3"></SfTextBox> 
    <ValidationMessage For="@(() => model.Field4)" /> 
</div> 
 
<style> 
  .required:after { 
    content:"*"; 
    color: red; 
  } 
</style> 

 

Please get back to us if you need further assistance. 

Regards, 
Joshna L 



KI KINS August 30, 2021 06:43 AM UTC

sorry for late reply...


I need to display above css in placeholder


Attachment: Untitled_e6b3fddd.zip


KI KINS August 31, 2021 10:12 AM UTC

pls help



NR Nevitha Ravi Syncfusion Team August 31, 2021 11:50 AM UTC

Hi Kins, 
 
Thanks for your update. 
 
It is not feasible to show EditForm validation error details as placeholder or in the placeholder space of Textbox component. By default in our TextBox, success and error class is added on validation. 
 
 
Red border in Field3 denotes validation failure and green border in Field4 denotes validation success. 
 
Regards, 
Nevitha 



KI KINS September 1, 2021 03:04 AM UTC

Thanks for reply...

Is it possible to change color of placeholder if it is mandatory instead of *.??



KI KINS September 2, 2021 04:51 PM UTC

Pls help



JL Joshna Lingala Uthama Reddy Lingala Syncfusion Team September 3, 2021 04:07 PM UTC

Hi KINS, 
  
Sorry for the delay. 
  
We have validated your query at our end. We let you know that we can modify the placeholder color if the field is required by adding the below CSS style in the application.  
  
<EditForm Model="@model" OnValidSubmit="@OnValidSubmit">  
            <DataAnnotationsValidator />  
            <div>  
                <label for="Name" style="padding: 20px 0 5px 0">Field1</label>  
                <SfTextBox id='textbox' @bind-Value="@model.Field1" Placeholder="Enter a Field1"></SfTextBox>  
                <ValidationMessage For="@(() => model.Field1)" />  
            </div>  
            @if(model.Field1 != null && model.Field1 == "User1")  
             {  
                <div>  
                    <label for="Name" style="padding: 20px 0 5px 0" class="required">Field2</label>  
                    <SfTextBox id='textbox' @bind-Value="@model.Field2" Placeholder="Enter a Field2"></SfTextBox>  
                    <ValidationMessage For="@(() => model.Field2)" />  
                </div>  
             }  
              
             <div>  
                <label for="Name" style="padding: 20px 0 5px 0" class="required">Field3</label>  
                        <SfTextBox id='textbox' @bind-Value="@model.Field3" CssClass="requiredClass" Placeholder="Enter a Field3"></SfTextBox>  
                        <ValidationMessage For="@(() => model.Field3)" />  
            </div>  
            <div>  
                <label for="Name" style="padding: 20px 0 5px 0" class="required">Field4</label>  
                        <SfTextBox id='textbox' @bind-Value="@model.Field4" CssClass="requiredClass" Placeholder="Enter a Field4"></SfTextBox>  
                        <ValidationMessage For="@(() => model.Field4)" />  
            </div>  
            <div class="sfButton">  
                <SfButton type="submit" IsPrimary="true">Submit</SfButton>  
            </div>  
  </EditForm>  
<style>  
    .sfButton {  
        padding-top: 10px;  
        display: flex;  
        justify-content: center;  
    }  
    .required:after {  
    content:"*";  
    color: red;  
  }  
    .requiredClass.e-input-group input.e-input::-webkit-input-placeholder {  
        color: red;  
    }  
</style>  
  
  
  
Kindly try with the above information and get back to us if you have any queries 
  
Regards, 
Joshna L 



KI KINS October 18, 2021 07:38 AM UTC

I have another clarification.


Can I add "*" required class in "Place Holder" instead of using Label

 <label for="Name" style="padding: 20px 0 5px 0" class="required">Field4</label>  



JL Joshna Lingala Uthama Reddy Lingala Syncfusion Team October 20, 2021 08:08 AM UTC

Hi KINS, 
 
Thanks for the update. 
 
We have validated the reported query for “ adding of “*” in the placeholder” at our end. We let you know that you can add “*” (Required) in the placeholder using the following CSS styles 
 
 
@using Syncfusion.Blazor.Inputs 
 
<SfTextBox FloatLabelType="FloatLabelType.Auto" Placeholder="Enter a Field" ></SfTextBox> 
 
<style> 
.e-input-group.e-control-wrapper.e-float-input .e-float-text::after { 
content: "*"; 
color: red; 
} 
</style> 
 
 
Output: 
 
 
Kindly try with the above information and get back to us if you have any queries. 
 
Regards, 
Joshna L 
 


Loader.
Up arrow icon