inline edition unique validation for two columns

inline edition unique validation for two columns

itramitram Posts: 43Questions: 15Answers: 0

I need to validate that the combination of two columns in the row is unique.

I found in the forum this custom validation:
https://datatables.net/forums/discussion/57729/unique-validation-for-multiple-columns

$count = $editor->db()->query('select')->get('*')->table('teachers_subjects')
->where('teachers_subjects.teacher', $values['teachers_subjects']['teacher'])
->where('teachers_subjects.subject', $values['teachers_subjects']['subject'])
->exec()
->count();

I did it and it works fine in the editor pop-up. The validator works fine when editing and when creating a new row.

But it doesn't work for inline editing. It drops a SQL error is the combination of the two columns is not unique.

How to make this validation work for inline edition?

This question has accepted answers - jump to:

Answers

  • rf1234rf1234 Posts: 2,950Questions: 87Answers: 416

    I am using unique indexes and the resulting SQL error if violated to generate client side error messages. That should be working here as well:

    editor
        .on( 'postSubmit', function ( e, json, data, action ) {
            if (json.error) {
                if ( json.error.indexOf('1062 Duplicate entry') >= 0 ) {
                   json.error = lang === 'de' ? 
                      "Tut uns Leid, dieser Kurs existiert schon!" : 
                      "Sorry, this rate already exists!";
                }
            }
        });
    
  • itramitram Posts: 43Questions: 15Answers: 0

    Thanks
    It is not ideal, but it works.
    So... better done than perfect.
    I will use your method.

  • rf1234rf1234 Posts: 2,950Questions: 87Answers: 416
    edited September 2022 Answer ✓

    It is not ideal, but it works.

    Well you could also fix your inline editing differently. You are probably not submitting all values to the server when you do inline editing because not submitting everything is the default. And that will be the cause of your problem. I found that too difficult ... but is is up to you. If you are willing to deeply analyze this ... your decision.

    When I do inline editing I usually use this to avoid the problem right away:

            formOptions: {
                inline: {
                    submit: 'allIfChanged',
                    onBlur: 'submit'
                }
            },
    

    Check this:
    https://editor.datatables.net/reference/option/formOptions.main
    and this please
    https://editor.datatables.net/reference/option/formOptions.inline

    Using the form has: submit: 'all'
    while inline editing has: submit: 'changed'
    as defaults
    I found that 'allIfChanged' works fine for inline editing because whenever anything gets changed ALL values are submitted which should make your validator work which is based on two field values.

  • itramitram Posts: 43Questions: 15Answers: 0

    Sorry if my answer seemed rude. Not my intention.
    I really appreciate your first answer which solves the issue in a fast and easy way.
    And the second answer is very helpful and will keep in mind for future cases.
    Again, thank you very much.

  • itramitram Posts: 43Questions: 15Answers: 0

    Hei rf1234,
    Your answer was useful, so accepted, but I don't know how to accept it. Sorry. Apologize.

  • allanallan Posts: 63,236Questions: 1Answers: 10,418 Site admin
    Answer ✓

    To be able to mark a reply as accepted, the thread needs to have been started with the New Question button rather than New Discussion. I've moved this thread over.

    Allan

Sign In or Register to comment.