Required field

6 posts, 0 answers
  1. Matthew
    Matthew avatar
    6 posts
    Member since:
    Aug 2012

    Posted 24 Jul 2013 Link to this post

    How can I add a required field in edit/create mode?  I see that you can enable validation on the grid in the JS examples but I see no direct examples of doing this in the Complete for MVC examples/demos.  Essentially I have a drop down list for AdmissionMilestoneID and I want to make it required that the user clicks the drop down and picks a value.  I am having issues when setting a default value as it seems to keep triggering the create event after I add the record fully and go to edit a new row.  It tries to recreate the new record even though it was already created.  

    @(Html.Kendo().Grid(Model.federationMilestones) 
    .Name("AdmissionMilestones")   
    .Columns(columns => {       
        columns.ForeignKey(p => p.AdmissionMilestoneID, Model.academicMilestones, "AdmissionMilestoneID", "MilestoneName")
            .Title("Milestone");
        columns.Bound(p => p.IsKeyMilestone);
        columns.Bound(p => p.RecieveReminders);
        columns.Bound(p => p.StudentRelativeDeadline);
    })
    .ToolBar(toolbar =>
    {
        toolbar.Save();
        toolbar.Create();
    })
    .Editable(editable => editable.Mode(GridEditMode.InCell))
    .Sortable()
    .Reorderable(reorder => reorder.Columns(true))
    .Resizable(resize => resize.Columns(true))
    .DataSource(dataSource => dataSource
        .Ajax()
        .Batch(true)
        .ServerOperation(false)
        .Events(events => events.Error("error_handler"))
     
        .Model(model =>
        {
            model.Id(p => p.AdmissionMilestoneID);
            model.Field(p => p.MilestoneName).Editable(false);
            model.Field(p => p.FederationID).DefaultValue(Model.FederationID);
        })
        .Update("Editing_Update_Milestones", "Admin")
        .Create("Editing_Add_Milestones", "Admin")
    )
    )
  2. Matthew
    Matthew avatar
    6 posts
    Member since:
    Aug 2012

    Posted 25 Jul 2013 Link to this post

    So I figured out part of my issues.  It looks like with the MVC wrappers Kendo prefers you to use the [Required] tags which will pop the validation in most circumstances.  I have that working flawlessly with the column "StudentRelativeDeadline".  However, this does not play nice with "AdmissionMilestoneID" which contains the ID behind the drop down list I am trying to validate.  It does validate but instead hits the controller and throws a pop up with the validation message that the field is required instead of the nice message on the grid that does not allow the user to submit the new record.  Any reason why a straight integer input field would work differently than the drop down field?  And any way to get the drop down to work the same?

    public class MilestoneConfigurationItemVM
    {
        [Required]
        public int? AdmissionMilestoneID { get; set; }
        public int? FederationMilestoneID { get; set; }
        public int FederationID { get; set; }
        public string MilestoneName { get; set; }
        public string MilestoneDescription { get; set; }
        public bool IsKeyMilestone { get; set; }
        public bool RecieveReminders { get; set; }
        [UIHint("Integer")]
        [Required]
        public int StudentRelativeDeadline { get; set; }
     
        public MilestoneConfigurationItemVM()
        {
     
        }
     
        public MilestoneConfigurationItemVM(int? admisID, int? fedMileID, int fedID, string name, string description, bool isKey, bool receiveReminders, int relativeDeadline)
        {
            AdmissionMilestoneID = admisID;
            FederationMilestoneID = fedMileID;
            FederationID = fedID;
            MilestoneName = name;
            MilestoneDescription = description;
            IsKeyMilestone = isKey;
            RecieveReminders = receiveReminders;
            StudentRelativeDeadline = relativeDeadline;
        }
    }
  3. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2194 posts

    Posted 26 Jul 2013 Link to this post

    Hi Matthew,

     
    In current scenario when you need the "AdmissionMilestoneID" always populated I would require to change the type to "int" instead of "int?", however if you still need that property nullable you should use the same approach as demonstrated in this CodeLibrary in order to support binding of DropDownList to nullable field. For convenience I created small example based on the provided information and the solution from the CodeLibrary and attached it to the current thread.  

    Kind Regards,
    Vladimir Iliev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  4. Matthew
    Matthew avatar
    6 posts
    Member since:
    Aug 2012

    Posted 26 Jul 2013 Link to this post

    The nullable "AdmissionMilestoneID" was not a requirement and was just something I was testing so sorry for the confusion.  I have correct the model below and used the Required Tag you have in your example project and I still am not getting the required validation to fire and it will allow me to save a new record without something picked in the drop down list.  

    public class MilestoneConfigurationItemVM
    {
        [Required()]
        public int AdmissionMilestoneID { get; set; }
        public int? FederationMilestoneID { get; set; }
        public int FederationID { get; set; }
        public string MilestoneName { get; set; }
        [Required]
        public string FederationMilestoneName { get; set; }
        public bool IsKeyMilestone { get; set; }
        public bool RecieveReminders { get; set; }
        [UIHint("Integer")]
        [Required]
        public int StudentRelativeDeadline { get; set; }
     
        public MilestoneConfigurationItemVM()
        {
     
        }
     
        public MilestoneConfigurationItemVM(int admisID, int? fedMileID, int fedID, string name, string milestoneName, bool isKey, bool receiveReminders, int relativeDeadline)
        {
            AdmissionMilestoneID = admisID;
            FederationMilestoneID = fedMileID;
            FederationID = fedID;
            FederationMilestoneName = name;
            MilestoneName = milestoneName;
            IsKeyMilestone = isKey;
            RecieveReminders = receiveReminders;
            StudentRelativeDeadline = relativeDeadline;
        }
    }
  5. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2194 posts

    Posted 29 Jul 2013 Link to this post

    Hi Matthew,

    In current scenario you should either set the DefaultValue for "AdmissionMilestoneID" or modify the EditorTemplate for this field to add OptionLabel to the DropDownList:

    1. First option - set the DefaultValue:
      .DataSource(dataSource => dataSource       
          .Ajax()     
          .Model(model => {
              model.Id(p => p.OrderID);
              model.Field(p => p.OrderID).Editable(false);
              model.Field(p => p.EmployeeID).DefaultValue(1);         
          })
    2. Second option - modify the GridForeignKey.cshtml EditorTemplate to include OptionLabel:  
      @model object
                  
      @(Html.Kendo().DropDownListFor(m => m)  
              .OptionLabel("-- please select --")
              .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"]))

    Kind Regards,
    Vladimir Iliev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  6. Matthew
    Matthew avatar
    6 posts
    Member since:
    Aug 2012

    Posted 29 Jul 2013 Link to this post

    The default value route does not solve my issue of forcing validation on the drop down like works in your example.  It also causes some weird issues when saving as it does not seem to be taking the saved change.  Your second option of adding the option label to ForeignKey.cshtml at least helps better identify that a choice is wanted so I will definitely do that.  

    To get around this issue I just set a default in the Add method and returned the updated row to the grid.  That is good enough interaction for me.  
Back to Top