Scheduler - making Title a required field

1 Answer 293 Views
Scheduler
Jordan
Top achievements
Rank 1
Iron
Jordan asked on 08 Aug 2022, 12:48 AM

I cannot figure out how to make Title a required field using HtmlHelper.

There does not seem to be an easy way to make Title required.   I want the error message to show up in the UI, I don't want the error message to be a JavaScript alert.

m.Field(f => f.Title);

In the JSON configuration has a validation parameter which makes this very simple.  But I am not using the JSON config.


@(Html.Kendo().Scheduler<SchedulePrototype.ViewModels.CalendarViewModel>()
    .Name("scheduler")
    .Date(startDate)
    .StartTime(new DateTime(startDate.Year, startDate.Month, startDate.Day, 8, 0, 0))
   // .Timezone("Etc/UTC")
    .Selectable(true)
    .Editable(e => e.Template("customEditorTemplate"))
    .Events(e =>
    {
        e.DataBinding("scheduler_dataBinding");
        e.DataBound("scheduler_dataBound");
      //  e.change("scheduler_change");
        e.Save("scheduler_save");
       // e.remove("scheduler_remove");
       // e.cancel("scheduler_cancel");
       e.Edit("scheduler_edit");
      //  e.add("scheduler_add");
       // e.movestart("scheduler_movestart");
        e.Move("scheduler_move");
    @*        e.moveend("scheduler_moveend");
        e.resizestart("scheduler_resizestart");
        e.resize("scheduler_resize");
        e.resizeend("scheduler_resizeend");*@
        e.Navigate("scheduler_navigate");
    })
    .Timezone("Etc/UTC")
     .Resources(resource =>
    {
         resource.Add(m => m.CalendarEventTypeId)
            .Title("Type")
            .Multiple(false)
            .DataTextField("Name")
            .DataValueField("CalendarEventTypeId")
            .DataSource(ds => ds.Read(read => read.Action("GetCalendarEventTypes", "Calendar")).ServerFiltering(false));
        resource.Add(m => m.DealList)
            .Multiple(true)
            .DataTextField("Title")
            .DataValueField("DealId")
            .DataSource(ds => ds.Read(read => read.Action("GetDealsToScheduleForDropDown", "Deal")).ServerFiltering(false));
    })
    .Views(views =>
    {
        views.DayView(dayView => dayView.Selected(true));
        views.WeekView();
        views.MonthView();
        views.AgendaView();
        views.TimelineView();
    })
    .DataSource(d => d
        .Model(m =>
        {
            m.Id(f => f.Id);
            m.Field(f => f.Title);
            m.RecurrenceId(f => f.RecurrenceID);
        })
        .Events(events => events.Error("error_handler"))
        .Read("Events_Read", "Calendar")
        .Create("Events_Create", "Calendar")
        .Destroy("Events_Destroy", "Calendar")
        .Update("Events_Update", "Calendar")
    )
)

1 Answer, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 10 Aug 2022, 04:22 PM

Hi Jordan,

Thank you for the provided information and code, it is greatly appreciated.

By design, the Telerik UI for ASP.NET Core Scheduler will initialize a Kendo Validator for the Popup's form. Having this in mind by design, the validation should be triggered if the declaration of the field in the model is non-nullable, as long as the integrated editors are bound successfully to the desired field. For example:

Custom Editor:

<div>Custom editor</div>

<div class="k-edit-label">
    @(Html.LabelFor(m => m.Title))
</div>
<div class="k-edit-field">
    @(Html.Kendo().TextBoxFor(m => m.Title))
</div>
 Would produce the following result:

 

Alternatively, you may also extend the built-in validator for the desired field that will be edited and provide a custom rule which will manually trigger the required validation by subscribing to the Save event. For example:

<script type="text/javascript">
        function onSave(e){
              var validator = e.container.find('#title').kendoValidator({
              rules:{
                customRule:function(input){
                  if(input.is("#title") & input.val() === ""){
                    e.preventDefault();
                    return false;
                  }
                  return true;
                }
              },
              messages:{
                customRule:"Make a selection!"
              }
            }).data('kendoValidator');
            validator.validate();
        }
</script>

Here is a Telerik REPL example that tackles this approach:

Telerik REPL for ASP.NET Core Example

More information on custom rules can be found here:

Custom Rules

That being said, I am also attaching a runnable sample for you to review that demonstrates an Editor Template that fires default validation for the specified fields. Would it be possible for you to reproduce the behavior but with a similar editor template configuration as yours and send it back for further observations? This will help me troubleshoot the observed locally and based on the obtained evidence, provide further guidance.

Looking forward to hearing back from you.

Kind Regards,
Alexander
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Jordan
Top achievements
Rank 1
Iron
commented on 12 Aug 2022, 07:41 PM

Thanks!
Tags
Scheduler
Asked by
Jordan
Top achievements
Rank 1
Iron
Answers by
Alexander
Telerik team
Share this question
or