(editor) force string to lowercase before writing to database

(editor) force string to lowercase before writing to database

mdesignmdesign Posts: 72Questions: 17Answers: 0

Can anyone help me out ?
How would be the right syntax for formatting an string to lowercase before writing to database

i have this (shortened)

$db->sql('set names utf8');
$editor = Editor::inst(
$db, 'alle_post',
'alle_post.post_idn' /* Primary Key /
)
->fields(
Field::inst('alle_post.webx_idn'),
Field::inst('alle_post.post_nam'), /
this should be lowercase */
);

$editor
->debug(true)
->process($_POST)
->json();

Replies

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Providing a custom formatter would be the logical approach.
    https://editor.datatables.net/manual/php/formatters

  • mdesignmdesign Posts: 72Questions: 17Answers: 0

    thank tangerine, but could you give me an example please. i don't get it.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    edited June 2020

    This example from this thread shows how to do it on the client (this is to upper case, but you'll get the idea).

    This section of the manual shows how to create a formatter that tangerine mentioned on the server,

    Colin

  • mdesignmdesign Posts: 72Questions: 17Answers: 0

    thx colin, following your js bin example

    editor.on( 'preSubmit', function (e, o, action) {
      if(action !== 'remove') {
        post_nam = o.data[Object.keys(o.data)].post_nam;
        o.data[Object.keys(o.data)].post_nam = post_nam.toUpperCase();
      }
    });
    

    but get an error:
    Uncaught TypeError: Cannot read property 'toUpperCase' of undefine

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    That would suggest post_nam isn't part of the data. Can you add console.log(o) at the start of the if clause, please, and report back,

    Colin

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Probably easier to use a PHP for matter here:

    Field::inst( 'status' )
        ->setFormatter( function ( $val, $data ) {
                return strtoupper( $val );
        } )
    

    Allan

  • mdesignmdesign Posts: 72Questions: 17Answers: 0

    great guys! alan's Solution did it within the lightbox-editing. how can i achieve this also within inline-editing ?

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    The server side PHP code is identical for form editing and inline editing.

  • mdesignmdesign Posts: 72Questions: 17Answers: 0

    thx, now it works. this was missing:
    editField: 'alle_post.post_nam',

This discussion has been closed.