if empty => take today's date

if empty => take today's date

mdesignmdesign Posts: 72Questions: 17Answers: 0
edited October 2020 in Editor

i would need an if else issue.

my code works as long the user inputs an date for 'post_dup', otherwise stops with the alert: "An SQL error occurred: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'post_dup' cannot be null"

in this case editor should take today's date. can anyone help me out with the correct syntax within my existing code.
thx a lot

// Vars
$date_sql = 'Y-m-d H:i:s';  // 2019-12-31 23:59:00  siehe php.net/manual/de/function.date
$datedate = 'd/m/Y';        // 31/12/2019           editor field => format:'DD/MM/YYYY'
$datetime = 'd/m/Y - H:i';  // 31/12/2019 - 12:12   editor field => format:'DD/MM/YYYY - HH:mm'

use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;

$db->sql('set names utf8');
$editor = Editor::inst(
  $db, 'alle_post', 
       'alle_post.post_idn' /* Primary Key */
  )
    ->fields(
    Field::inst('alle_post.post_dup')
      ->validator(Validate::dateFormat($datetime))
      ->getFormatter(Format::datetime( $date_sql, $datetime))
      ->setFormatter(Format::datetime( $datetime, $date_sql)),
    );

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

Answers

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

    A set formatter is the way to do this. You can add a condition to check for an empty value and if so return the current date from the function, otherwise return the date submitted.

    Allan

  • mdesignmdesign Posts: 72Questions: 17Answers: 0

    thx allan, i tried your answer but i don't get it. how would be the exact syntax in my case. i'm sorry, but i always struggle with this things.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited December 2020
        ->setFormatter( function ( $val, $data ) {
                    return $val
                        ? $val
                        : date('c');
        } )
    

    Is this to be mixed with the existing set formatter though? If so you'd need to call the existing formatter:

        ->setFormatter( function ( $val, $data ) {
                    return $val
                        ? Format::datetime( $datetime, $date_sql)($val, $data)
                        : date('c');
        } )
    

    I think should do it.

    Allan

  • mdesignmdesign Posts: 72Questions: 17Answers: 0

    Allan your second solution gives me an Parse error: syntax error, unexpected '?'

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

    Delete one of the question marks from Allan's example.

  • mdesignmdesign Posts: 72Questions: 17Answers: 0

    thx a lot tangerine. that helped

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

    Oops - sorry. Edited my post to fix that. Thanks for picking that up @tangerine.

    Allan

  • LapointeLapointe Posts: 430Questions: 81Answers: 4

    Hi @allan

    Nobody's perfect but somebody try to.

    Bob

  • mdesignmdesign Posts: 72Questions: 17Answers: 0

    @tangerine. your solution helped me out with new entry, but now the update doesn't work anymore. can you provide me the right syntax for the combination of both.

    $date_sql = 'Y-m-d H:i:s';  // 2019-12-31 23:59:00 
    $datetime = 'd/m/Y - H:i';  // 31/12/2019 - 12:12
    
    
    Field::inst('alle_zzzz.zzzz_dto')
      ->validator(Validate::dateFormat($datetime))
      ->getFormatter(Format::datetime( $date_sql, $datetime))
      ->setFormatter(Format::datetime( $datetime, $date_sql)), // for UPDATE only
    
      /* (!) works at NEW INSERT, not by UPDATE */
      ->setFormatter( function ($val, $data) {
        return $val ?
          Format::datetime( $datetime, $date_sql)($val, $data)
          : date('c'); // date current
      }),      
         
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    When you do the update, is it not submitting the existing value from the client-side and thus it would be using that value rather than date('c')?

    Worth noting that you can only have one setFormatter on a field, so line 11 will overwrite line 8 in the above code. It won't cause an error, but line 8 is redundant.

    Allan

  • mdesignmdesign Posts: 72Questions: 17Answers: 0
    edited December 2020

    i know that i can only use one setFormatter. what i would need is the right syntax for the combination of both.

    so when there is no date input (new entry) then take today's date otherwise use the transmitted value. in other words i would need for the second setFormatter above the extension for updating data.

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

    I don't quite understand I'm afraid. The code you show above in lines 11 to 15 looks correct to me. When there is a value submitted it will format it into ISO8601. Otherwise if there is no value submitted (insert or edit, it applies to both) then the current date would be used.

    I'm not quite clear if that isn't want you are looking for, or if it is and it isn't working? If it isn't working, what is happening, as the code above looks like it should be doing that.

    Allan

  • mdesignmdesign Posts: 72Questions: 17Answers: 0

    http://live.datatables.net/suquzuri/1/edit

    by update i get an error
    <b>Notice</b>: Undefined variable: datetime in <b>...editor-alle_zzzz-ssp.php</b>
    <b>Notice</b>: Undefined variable: date_sql in <b>...editor-alle_zzzz-ssp.php</b>

  • mdesignmdesign Posts: 72Questions: 17Answers: 0
    /* -------------------------------------------------------------------------- *
       DataTables Editor PHP library
     * -------------------------------------------------------------------------- */
    include_once('-conf-editor.php');
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions;
     
    $db->sql('set names utf8');
    $editor = Editor::inst(
      $db, 'alle_zzzz',
           'alle_zzzz.zzzz_idn' /* Primary Key */
      )
        ->fields(
        Field::inst('alle_zzzz.webx_idn'),
        Field::inst('alle_zzzz.zzzz_idn'),
        Field::inst('alle_zzzz.zzzz_sta'),
        Field::inst('alle_zzzz.zzzz_tit'),
        Field::inst('alle_zzzz.zzzz_txt'),
        Field::inst('alle_zzzz.zzzz_dto')
          ->validator(Validate::dateFormat($datetime))
          ->getFormatter(Format::datetime( $date_sql, $datetime))
        //->setFormatter(Format::datetime( $datetime, $date_sql)), // works at UPDATE, not by NEW INSERT
     
          /* (!) works at NEW INSERT, not by UPDATE */
          ->setFormatter( function ($val, $data) {
            return $val ?
              Format::datetime( $datetime, $date_sql)($val, $data)
              : date('c'); // take today's date
          }),     
     
        /* (todo) Upload (siehe editor/example/advanced/upload.html */
     
        /* letztes Field Update mySql Timestamp */
        Field::inst('alle_zzzz.zzzz_upd')
          ->validator(Validate::dateFormat($datetime))
          ->getFormatter(Format::datetime( $date_sql, $datetime))
          ->setValue(date($date_sql, time())) // kein ',' am Ende
        );
     
    // superadmin (kein where)
    if($_SESSION['dashboard']['web'] !== '%') $editor->where('alle_zzzz.webx_idn', $_SESSION['dashboard']['web']);
     
    $editor
    //->where('alle_zzzz.webx_idn', $_POST['webx_idn']) /* does not work => undefined index: pdoField */
    //->where('alle_zzzz.webx_idn', 'domain.com')     /* does work */
      ->debug(true)
        ->process($_POST)
        ->json();
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    You need to change the Editor Ajax from a string to:

    ajax: {
      url: "http://10.0.0.4/set-pepples/set-golf20/-admin/editor-alle_zzzz-ssp.php",
      data: function (d) {
        d.webx_idn = 'gclinz.at';
      }
    }
    

    Otherwise it won't be sending the webx_idn parameter when you create, edit or delete.

    Allan

  • mdesignmdesign Posts: 72Questions: 17Answers: 0

    this do not change anything.

    data: function (d) {
      d.webx_idn : 'gclinz.at',
     }
    

    leads to an SyntaxError

  • mdesignmdesign Posts: 72Questions: 17Answers: 0

    is there a way, where i can hire one of you guys ?

    i would open an ftp-account to an testserver. iM sure you would solve this and a few other things within a litte time. of course i would pay for this.

    i would love to work with editor, cause its so fast and fun editing data but now i spend weeks on some problems.

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    Like you need to add a comma to the line before to separate the options. Note the comma at the end of line 2 in Allan's code snippet. If you aren't able to resolve the syntax error then post your whole Datatables init code so we can take a look.

    Kevin

  • mdesignmdesign Posts: 72Questions: 17Answers: 0

    @kevin - i did already post the code here
    http://live.datatables.net/suquzuri/1/edit

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    edited December 2020

    Does that example show the changes you made that are causing the syntax error?

    I was mistaken above, that change is to the Editor config. Please post your full Editor config.

    Kevin

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

    I'm on my phone at the moment so a little limited, but did you replace the ajax option in your Editor config with the block I gave above? Simply adding the data function isn't enough. You need the ajax option to be an object with url and data options.

    If that doesn't fix it, then yes, send me a PM with server details and I'll take a look.

    Allan

  • mdesignmdesign Posts: 72Questions: 17Answers: 0
    edited December 2020

    thx allan - i will send you an pm the next days. happy new year to all kind supporters!

  • mdesignmdesign Posts: 72Questions: 17Answers: 0

    @allan - thx i did send you an PM with details.

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

    Hi,

    Many thanks for the PM and apologies for the delay in replying to you there. I've just done so now though.

    Regards,
    Allan

This discussion has been closed.