|
| 1 | +--- |
| 2 | +title: Stop the cascading EditContext for a specific field in the Form |
| 3 | +description: Stop the cascading EditContext for a specific field in the Form |
| 4 | +type: how-to |
| 5 | +page_title: Stop the cascading EditContext for a specific field in the Form |
| 6 | +slug: form-stop-cascading-editcontext |
| 7 | +position: |
| 8 | +tags: |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | +<table> |
| 14 | + <tbody> |
| 15 | + <tr> |
| 16 | + <td>Product</td> |
| 17 | + <td>Form for Blazor</td> |
| 18 | + </tr> |
| 19 | + </tbody> |
| 20 | +</table> |
| 21 | + |
| 22 | +## Description |
| 23 | + |
| 24 | +I would like to stop the cascading `EditContext` for a specific editor in my Form. The Editor should still be in the Form, but should not be validated. This would also allow me to not provide a `ValueExpression`. |
| 25 | + |
| 26 | +## Solution |
| 27 | + |
| 28 | +To stop the cascading `EditContext` for a specific editor in the TelerikForm you should use the [Template]({%slug form-formitems-template%}) and wrap the custom editor in a `<CascadingValue>` tag. In the `Value` attribute of the `CascadingValue` you should reset the `EditContext`. |
| 29 | + |
| 30 | +````CSHTML |
| 31 | +@* Stop the cascading EditContext for the Edit the DoB checkbox. *@ |
| 32 | +
|
| 33 | +@using System.ComponentModel.DataAnnotations |
| 34 | +
|
| 35 | +<TelerikForm Model="@person"> |
| 36 | + <FormValidation> |
| 37 | + <DataAnnotationsValidator></DataAnnotationsValidator> |
| 38 | + </FormValidation> |
| 39 | + <FormItems> |
| 40 | + <FormItem> |
| 41 | + <Template> |
| 42 | + <CascadingValue Value="@((EditContext?)null)"> |
| 43 | + <label for="enable-datepicker">Edit the DoB</label> |
| 44 | + <TelerikCheckBox Value="@Enabled" OnChange="@(v => OnChange((bool)v))" Id="enable-datepicker"></TelerikCheckBox> |
| 45 | + </CascadingValue> |
| 46 | + </Template> |
| 47 | + </FormItem> |
| 48 | + <FormItem Field="@nameof(Person.Id)" Enabled="false" LabelText="Id"></FormItem> |
| 49 | + <FormItem Field="@nameof(Person.FirstName)" LabelText="First name" Hint="Enter your first name"></FormItem> |
| 50 | + <FormItem Field="@nameof(Person.LastName)" LabelText="Last name" Hint="Enter your last name" ColSpan="2"></FormItem> |
| 51 | + <FormItem Field="@nameof(Person.DOB)" LabelText="Date of Birth" Hint="Enter your Date of Birth" Enabled="@Enabled"></FormItem> |
| 52 | + </FormItems> |
| 53 | +</TelerikForm> |
| 54 | +
|
| 55 | +@code { |
| 56 | + public Person person = new Person(); |
| 57 | +
|
| 58 | + private bool Enabled { get; set; } |
| 59 | +
|
| 60 | + private void OnChange(bool value) |
| 61 | + { |
| 62 | + Enabled = value; |
| 63 | + } |
| 64 | +
|
| 65 | + public class Person |
| 66 | + { |
| 67 | + [Editable(false)] |
| 68 | + public int Id { get; set; } = 10; |
| 69 | + [Required] |
| 70 | + public string FirstName { get; set; } = "John"; |
| 71 | + [Required] |
| 72 | + public string LastName { get; set; } = "Doe"; |
| 73 | + public DateTime DOB { get; set; } = DateTime.Today.AddYears(-20); |
| 74 | + } |
| 75 | +} |
| 76 | +```` |
0 commit comments