(editor) force string to lowercase before writing to database

(editor) force string to lowercase before writing to database

mdesignmdesign Posts: 70Questions: 16Answers: 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,342Questions: 35Answers: 394

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

  • mdesignmdesign Posts: 70Questions: 16Answers: 0

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

  • colincolin Posts: 15,112Questions: 1Answers: 2,583
    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: 70Questions: 16Answers: 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,112Questions: 1Answers: 2,583

    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: 61,438Questions: 1Answers: 10,049 Site admin

    Probably easier to use a PHP for matter here:

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

    Allan

  • mdesignmdesign Posts: 70Questions: 16Answers: 0

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

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

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

  • mdesignmdesign Posts: 70Questions: 16Answers: 0

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

This discussion has been closed.