This is a migrated thread and some comments may be shown as answers.

[Solved] Grid not validating all columns in batch edit mode

4 Answers 196 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
IT
Top achievements
Rank 1
IT asked on 15 Aug 2011, 08:14 AM
I have a grid with two string columns that are both "required"...

public class PersonModel
{
    [Display(Name = "First Name")]
    [Required(ErrorMessage = "First Name Required")]
    public string FirstName { get; set; }
 
    [Display(Name = "Last Name")]
    [Required(ErrorMessage = "Last Name Required")]
    public string LastName { get; set; }
}
 
 
 
@(Html.Telerik().Grid<PersonModel>()
    .Name("Grid")
    .ToolBar(commands =>
    {
        commands.Insert();
        commands.SubmitChanges();
    })
    .DataBinding(d => d
        .Ajax()
            .Select("AjaxSelect", "Person")
            .Update("AjaxUpdate", "Person")
    )
    .ClientEvents(e => e
        .OnRowDataBound("GridOnRowDataBound")
        .OnEdit("GridOnEdit")
        .OnSave("GridOnSave")
        .OnError("GridOnError")
    )
    .DataKeys(d => d.Add(o => o.Id))
    .Editable(e => e.Mode(GridEditMode.InCell))
    .Pageable()
    .Sortable()
    .Columns(columns =>
    {
        columns.Bound(o => o.FirstName);
        columns.Bound(o => o.LastName);
    })
)

If I go to add a record, it will let me save changes with one of the columns omitted...

- Click "Add new record"
- Click "Save changes"
- Validation kicks in and tells me I need to enter a First Name... so I type one in, and click "Save changes"
- AJAX kicks in and tries to save the record (which fails, because there is no Last Name)

The validation works on the Last Name field, because if I click in and out without typing something, it will tell me:-

- Click "Add new record"
- Type in First Name
- Click in Last Name column, but click out again without entering anything
- Validation kicks in telling me I have to supply Last Name


A similar issue appears to have been reported here: http://www.telerik.com/community/forums/aspnet-mvc/grid/confused-with-model-validation-on-batch-editing.aspx

Thanks in advance for any help...

4 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 15 Aug 2011, 10:47 AM
Hello Aleks,

 Telerik Grid for ASP.NET MVC relies on the built-in ASP.NET MVC validation. The latter works only if there is a visible input element in the page which is validated and a validation message is displayed. In the case of InCell editing the input filed is visible only when a cell is in edit mode. This means that validation triggers only in that case. 

 Server validation still works though. If you handle the OnError event you would be able to display the error messages as shown in our batch editing example:

function Grid_onError(args) {
            if (args.textStatus == "modelstateerror" && args.modelState) {
                var message = "Errors:\n";
                $.each(args.modelState, function (key, value) {
                    if ('errors' in value) {
                        $.each(value.errors, function() {
                            message += this + "\n";
                        });
                    }
                });
                args.preventDefault();
                alert(message);
            }
        }

That way you would be able to still show (albeit in a different way) that there are validation errors.

The other option is to use a different type of editing mode.

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

0
IT
Top achievements
Rank 1
answered on 15 Aug 2011, 12:17 PM

Thanks for your response.

With the server validation I think I must be missing something... I already have the OnError event wired up (at this moment just a stub with an alert so I can see if it fires)... the clientevent is in the example I gave. The callback is below:

function GridOnError(e) {
    alert("error");
}

However it doesn't seem to fire when validation fails.

I've checked out that example you provided the link for, and I can't see what is in that one that I'm missing.

It's hard to compare to the example... when you insert in the example, the Product name cell is automatically in edit mode (whereas in my grid, the new row is added, but I have to click on the cell to edit... another difference I can't work out between the two) and because the only other "required" fields in the example (the numeric fields) have default values, there is no way to get as far as server validation, because you can't get past the client validation (once in Product name, you have to enter one to get out, and the other two fields already have values you can't remove)...

I think this is what Henry in the post I referenced was getting at... if you change the order of the columns in the example, such that you can get around client validation (the first field is no longer the Product name), then you can bypass client validation (for the reasons you have mentioned) but then server validation doesn't work...

0
Atanas Korchev
Telerik team
answered on 15 Aug 2011, 12:27 PM
Hi Aleks,

There is a known issue with the 2011.2 712 release which is preventing the OnError event from working properly. If you download the latest internal build that event should work.

 Unfortunately there is no immediate workaround apart from using the OnError event or a different editing mode.

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

0
Timir
Top achievements
Rank 1
answered on 30 Aug 2011, 10:09 PM
Hello,

I am using Q2 release and using MVC Grid in Ajaxbiniding InCell Editing - Batch Update mode.

I have a some business rule as example some value in row 2 can not be greater then some value in row 1.

On update I validate it on the server side. Now I want to put the message with row number in the validation summery and with the cell with error.

Any idea how can i do that?

Thanks
Timir
Tags
Grid
Asked by
IT
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
IT
Top achievements
Rank 1
Timir
Top achievements
Rank 1
Share this question
or