How can I change a value for a cell upon validation in a RadGrid using batch editing?

1 Answer 64 Views
Grid Input
Michael
Top achievements
Rank 1
Michael asked on 13 Nov 2023, 07:15 PM | edited on 13 Nov 2023, 08:46 PM

I have a Weight column in my RadGrid, which I'm using a CustomValidationFunction, which is working fine. It disallows invalid values.

But I want blank, 0 (or below) to be set automatically to the minimum value allowed, rather than making the user put it in manually. How can I accomplish this?

 

Here is the column I'm validating:

                                <telerik:GridTemplateColumn UniqueName="Weight" HeaderText="Weight" DataField="Weight" AllowFiltering="false">
                                    <ItemTemplate><%# Eval("Weight") %></ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="WeightAmt" runat="server" />
                                        <asp:CustomValidator runat="server" ID="WeightValidator" EnableClientScript="true" ControlToValidate="WeightAmt"
                                            ClientValidationFunction="WeightValidator" />
                                    </EditItemTemplate>
                                </telerik:GridTemplateColumn>
And here is the validation function as it currently stands:

        function WeightValidator(sender, args) {
            var thisWeight = parseInt(args.Value);
            if (thisWeight > maxWeight) {
                sender.textContent = "Weight may not be larger than " + maxWeight;
                args.IsValid = false;
                return;
            }
            if (thisWeight < 1) {
                thisWeight = minWeight;
                arguments[0].parentElement.parentElement.getElementsByTagName("input")[0].textContent = minWeight.toString();
            }
            if (thisWeight < minWeight) {
                sender.textContent = "Weight may not be smaller than " + minWeight;
                args.IsValid = false;
                return;
            }
            args.IsValid = true;
        }
Debug indicates the `arguments` statement sets the value, but this isn't actually tied to anything on the page, so it gets lost. The only place I see the cell value is in a hidden `div` (`style="display: none")` and I don't see any way of changing that.

In searching for a solution, I see it is recommended to edit the `innerHTML`, but that doesn't even contain the value I'm wanting to edit.

Is there a good way for validation to correct data errors that it can instead of just calling it invalid?

Vasko
Telerik team
commented on 16 Nov 2023, 03:49 PM

Hi Michael,

The code that you provided deals with user input validation. If you want values to be modified automatically, this is not relevant. I  have some questions regarding the automation:

  • How did you imagine changing blank or 0 values to 30 if you do not expect the user to do it?
  • Why implement an additional automated solution within the Grid, If you could perform a single, one-line SQL query to update the values for every record at once.
    For example, a SQL query similar to the following:  
    "UPDATE [TableName] SET [Weight] = 30 WHERE [Weight] < 1 OR Weight = ''"

If you could provide further details about the requirements, we can better counsel you.

Kind regards,
Vasko
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources
Michael
Top achievements
Rank 1
commented on 16 Nov 2023, 04:03 PM

Thank you for your response. The urgency behind it is greatly diminished, as I have found additional issues incompatible with batch editing entirely.

But to answer your questions:

  • I thought it was clear from the code, which you seem to see from your second point. If the user enters nothing or a negative number for the weight, then it automatically substitutes the minimum value, much like autocorrect on your phone for a "mistyped" word.
  • It seems to me a cumbersome process to update all database records when it is only one record being changed. Furthermore, data already exists in the database, and I don't know if a null value is allowed for any such entries, but certainly my new UI will not allow future ones.

1 Answer, 1 is accepted

Sort by
0
Vasko
Telerik team
answered on 16 Nov 2023, 04:44 PM

Hello Micahel,

Thank you for the additional information.

Based on it, I managed to adjust the shared code snippet so that it achieves the desired behavior:  

<script>
    var minWeight = 30
    var maxWeight = 200 // Added for testing purposes

    function WeightValidator(sender, args) {
        var thisWeight = parseInt(args.Value);
        var inputElement = document.getElementById(sender.controltovalidate)  // This will be the ID of the currently clicked input eleemnt

        if (thisWeight > maxWeight) {
            sender.textContent = "Weight may not be larger than " + maxWeight;
            args.IsValid = false;
            return;
        }
        if (thisWeight < 1) {
            thisWeight = minWeight;

            inputElement.value = thisWeight  //Set the value to thisWeight (in my case 30)
        }
        if (thisWeight < minWeight) {
            sender.textContent = "Weight may not be smaller than " + minWeight;
            args.IsValid = false;
            return;
        }
        args.IsValid = true;
    }
</script>

Give the aforementioned method a try and see if it is of use to you.

Kind regards,
Vasko
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources
Tags
Grid Input
Asked by
Michael
Top achievements
Rank 1
Answers by
Vasko
Telerik team
Share this question
or