Custom validation error on inline edit of different field

Custom validation error on inline edit of different field

phpkidphpkid Posts: 10Questions: 2Answers: 0

Hi,

I'm using Datatables Editor version 1.5.6 and have implemented inline editing.

However the following validation breaks and the 'Journal date cannot be before 1st January 2009' error is returned when a different field is edited inline:

->validator(function($journaldate, $data, $opts) {
    $formattedDate = str_replace('/', '-', $journaldate);
    if ( strtotime($formattedDate) < strtotime('2009-01-01') )
        return 'Journal date cannot be before 1st January 2009';
    else if (strtotime($formattedDate) > strtotime('+12 months'))
        return 'Journal date cannot be more than a year in the future';
    return true;
    } )

This error can be avoided by adding:

submit: 'allIfChanged'

to the inline edit initialisation but out of stubbornness, and curiosity of why it this is occurring, I want to stop validation happening when the related field isn't submitted.

Any help is appreciated.

Thanks,
Alec

Answers

  • phpkidphpkid Posts: 10Questions: 2Answers: 0

    Duplicate post, please ignore/close

  • allanallan Posts: 61,711Questions: 1Answers: 10,103 Site admin

    Add:

    ->validator( 'Validate::notEmpty' )
    

    prior to your own validator. That will accept no input as valid (and then do nothing with it!). The other option is to check for null in your own validator, and if found, accept that as valid (it means that no data was submitted for that field).

    The reason that the validator runs on all fields, is that it allows fields to be required to be submitted (which can be useful on occasion!).

    Regards,
    Allan

  • phpkidphpkid Posts: 10Questions: 2Answers: 0

    Hi Allan,

    Thank you for your reply. Sorry I have been slow to respond, was on holiday.

    Unfortunately 'Validate::notEmpty' doesn't work, as when one validator passes true, the next validator is ran. This means that the "Journal date before ..." error is still displayed.

    Handling null in my custom validator worked, however if you see my question here https://datatables.net/forums/discussion/38437/custom-validation-error-on-inline-edit-of-different-field#latest the minMaxNum validator also throws an error, and I can't think of how to accept null as valid within that validator.

    Again, any help is appreciated.

This discussion has been closed.