question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Required Vaildation And Other Like Radzen

See original GitHub issue

Required Field For Vaildation

Cause I Want Field To Be Required When COnditional Be Like Auto Create Somthing CheckBox When True Disible Requrired But Else The Somthing Be Required

<MCheckBox @bind-Value="Model.AutoCreate" Label.../>
<MTextField @bind-Value="Model.Somthing" Disabled="Model.AutoCreate" Required="@(!Model.AutoCreate)".../>

There’s No Required=“@(!Model.AutoCreate)”

Or You Can Make It Like This

<MCheckBox @bind-Value="Model.AutoCreate" Label.../>
<MTextField @bind-Value="Model.Somthing" Disabled="Model.AutoCreate">
    <Vaildations>
        @if (!Model.AutoCreate)
        {
              <MRequiredField/>
        }
    </Vaildations>
</MTextField>

Issue Analytics

  • State:closed
  • Created 2 months ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
capdiemcommented, Jul 17, 2023

@wisamidris7 You can use Rules implement it.

<MForm @ref="_form">
    <MCheckbox @bind-Value="_autoCreate"></MCheckbox>
    <MTextField @bind-Value="_value" Rules="Rules"></MTextField>
    <MButton OnClick="OnSubmit">Sumbit</MButton>
</MForm>

@code {

    private static Dictionary<string, Func<string, StringBoolean>> s_rules => new()
    {
        { "required", (s) => string.IsNullOrWhiteSpace(s) ? "Required" : true }
    };

    private bool _autoCreate;
    private string? _value;
    private MForm? _form;

    private IEnumerable<Func<string, StringBoolean>> Rules
    {
        get
        {
            var rules = new List<Func<string, StringBoolean>>();

            if (!_autoCreate)
            {
                rules.Add(s_rules["required"]);
            }

            return rules;
        }
    }

    private void OnSubmit()
    {
        // _form!.ResetValidation();
        _form!.Validate();
    }

}
0reactions
capdiemcommented, Jul 24, 2023

@wisamidris7

<MTextField @bind-Value="_value" Rules="@(new List<Func<string, StringBoolean>>() { e => (_autoCreate || !string.IsNullOrWhiteSpace(e)) ? true : "Required" })"></MTextField>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Blazor required validator component
RequiredValidator. Demonstration and configuration of the Radzen Blazor RequiredValidator component. Example; Edit Source. Display validators as popup.
Read more >
Blazor Validation Made Easy with Radzen
Radzen validation is a powerful feature that allows developers to add validation rules to form inputs in a Blazor application. This feature ...
Read more >
Radzen Validation in a ForEach
I have a Foreach which contains a RadzenNumeric and Validation for it. The issue I am having is that the Name and Component...
Read more >
Blazor form validation
Validator is added like any other Blazor component. Page above uses DataAnnotationValidator but you can create custom one if you like.
Read more >
How do you manually trigger the form validation in Blazor?
You have to define and bind the EditContext with EditForm and then call the method editContext.Validate() on button click to manually trigger the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found