Unique not working after setFormatter

Unique not working after setFormatter

dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

Hi there,
i have the follwing problem:
User accounts must be entered in a table column. These accounts must be in lower case an unique. So i use the follwing syntax:

->setFormatter( function ( $val, $data ) { return trim(strtolower($val)); })
->validator( Validate::unique())

I enter the account "NewAccount" in the editor, it will be written as "newaccount" into the db. Thats correct.
If i enter then the account "newaccount", i get a error message that this account exists. Thats correct.
But if i enter e.g. "NewaCcount" it will also be written as "newaccount" into the db. Thats wrong!

The easiest way would be, to change the column in the db into a unique column. But i do not have the rights to change the table settings.
It seems that the problem is that the reformating to lower happens after the unique check.

I was testing that i do the reformating to lower case in javascript after leaving the input filed of the editor. But i had no success. Has anybody a clue or solution for me?

Thank's in advance


  • allanallan Posts: 57,236Questions: 1Answers: 9,113 Site admin


    You are right - the validator does indeed happen before the set formatter is run. I honestly can't recall my rational for that at the moment, so I've opened a bug to revisit that.

    However, the good news is that there is a simple workaround with an event - e.g.:

    ->on('preCreate', function ($e, &$values) {
      $values['accountName'] = strtolower($values['accountName']);

    And do the same for preEdit (which note has three parameters - an id is added in between those two).

    Note the & in &$values - that tells PHP to pass my reference, overriding its default copy on write behaviour. Without it any changes to $values would be local. This way, it changes the original array.


  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

    Thanks a lot Allan. IT WORKS!
    You saved my day.

    Sorry, for the late response.

Sign In or Register to comment.