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

Replies

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Hi,

    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.

    Allan

  • 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.