Validator.Required does not appear to work correctly in .Net code

Validator.Required does not appear to work correctly in .Net code

jmo21jmo21 Posts: 13Questions: 3Answers: 0

In Example source code, change StaffController.cs\ Staff() such that "first_name" is validated as follows:

.Field(new Field("first_name").Validator(Validation.Required(new ValidationOpts() { Message = "Name is required" })))

Now run the sample, and go to "Web/examples/inline-editing/simple.html"

Now edit "Last Name" column, add 1 single character, hit return. Last Name stays in edit mode.

This is because the Required validator for "first_name" has fired. As we are not currently editing that field, the error is not displayed anywhere.

Narrowed down to Editor.cs, public bool Validate(DtResponse response, DtRequest request)

In the for loop
foreach (var field in _field)

it always calls:
var validation = field.Validate(values, this, pair.Key.Replace(_idPrefix, ""));

I have wrapped those lines with a check on the "values" object for the key of the current field in the loop as follows:

if (values.Any(a => a.Key == field.Name()))

theory being, if I am not passing in a value for that column (first_name) we shouldn't be firing the validator for that column

Not sure if this the right place for this fix or something that should be fixed elsewhere.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Hi,

    There are a couple of things going on here, but the long and short if it is, don't use Required() - use NotEmpty().

    Required() needs the field to be submitted, which is will not be by default with inline editing (only the field that has been edited is submitted). NotEmpty() allows for fields not to be submitted, but if it is, it should be not empty. See also docs.

    The other half of this is the lack of showing that error message on the client-side. Since inline editing only allows one field to be edited at a time, it assumes that the other fields are already valid (since they are already in the table they must be - unless the validation has changed). This is something that needs to be improved it Editor - even if its an alert() but I think it can be sidestepped entirely by using NotEmpty().

    Regards,
    Allan

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I've added a note to the documentation about using NotEmpty() over Required() and will push the change out to the site shortly.

    Allan

  • jmo21jmo21 Posts: 13Questions: 3Answers: 0

    Gotcha, thanks Allan!

This discussion has been closed.