editor presubmit message

editor presubmit message

kringelkringel Posts: 16Questions: 9Answers: 0
edited December 2020 in Free community support

Hello!

I am checking for duplicates in the database and returning an error. It works fine. What I want is to display the user some kind of warning message that this input already exists and still let him create a row. Just a notification about the presence of a duplicate. How should I change the code?
I took this forum topic as a basis https://datatables.net/forums/discussion/12650/check-for-duplicates/p1.

Thanks in advance!

        Field::inst( 'name' )
            ->validator( function($val, $data, $opts) {
                global $db;
                  $link = mysqli_connect("localhost", "my_user", "my_password", "world");
                        $checkdup = $db
                            ->select( 'users', 'id', array(
                                'name' => mysqli_real_escape_string($link, $val)
                            ) )
                            ->count();
                        if($checkdup !== 0){
                            return "Duplicate name.";
                        }
                return true;
            } ),

Answers

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    You would need to have an additional (hidden) field which would tell the validation function to skip or not. e.g. in pseudo code:

    if (skipValidation) {
      return true;
    }
    else {
      do validaton
    }
    

    On the client-side you'd have a postSubmit event that would check for this validation error / warning, and set the hidden field value so the validation would skip on the next submission (e.. set it to true). Remember to set it to false when open happens so it resets for the next insert.

    Regards,
    Allan

This discussion has been closed.