1. Fabrik 3.8.1 has been released. It is mostly bug fixes and feature enhancements, but does include two new plugins (push notifications, and the sequence element). As usual we strongly recommend testing the new release on a sandbox if your application is mission critical, and always do an Akeeba backup before updating.
    Dismiss Notice

Question about upsert value of empty form field

Discussion in 'Community' started by econveyors, Jul 12, 2018.

  1. econveyors

    econveyors New Member

    Level: Community
    Here's my situation and the functionality I'm looking to accomplish:
    - I have a form that uses the upsert plugin to copy the form data to a second table.
    - For one specific field, I only want it to overwrite the second table's data if the form field contains a value.
    - If the form field is empty, I want the data that's already in the second table's field to remain untouched.

    I've tried using just the placeholder as well as several versions of code to check if the form field contains data, but what continues to happen is this:
    - When the form field contains a value, the field in the second table is successfully updated with the form field's value.
    - When the form field is empty, the field in the second table is updated, but it's blank... I'm assuming because the form field was left blank.

    Here's what I've tried. This code is being inserted into the "Value" field in the Upsert plugin and evaluated as PHP (obviously except for when I tried just the placeholder.)

    Placeholder
    Code (Text):
    {tablename___elementname}
    Example 1
    Code (Text):
    $formField = $formModel->fullFormData['tablename___elementname'];
    if(!empty($formField)):
        return $formField;
    endif;
    Example 2
    Code (Text):
    $formField = $formModel->fullFormData['tablename___elementname'];
    if(isset($formField)):
        return $formField;
    endif;
    Example 3
    Code (Text):
    $formField = $formModel->fullFormData['tablename___elementname'];
    if(isset($formField) && !empty($formField)):
        return $formField;
    endif;
    Example 4
    Code (Text):
    $formField = $formModel->fullFormData['tablename___elementname'];
    if($formField != ''):
        return $formField;
    endif;
    This feels like it should be pretty simple, but for some reason I can't figure out what I'm missing.

    Thanks!
     
  2. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Yeah, the way it works at the moment is if the value is empty, we use the default.

    What you could try is edit ...

    plugins/fabrik_form/upsert/upsert.php

    ... at line 165, modify the code to check to see if the returned value is false ...

    Code (Text):

                if ($upsert->upsert_eval_value[$i] === '1')
                {
                    $res = FabrikHelperHTML::isDebug() ? eval($v) : @eval($v);
                    FabrikWorker::logEval($res, 'Eval exception : upsert : ' . $v . ' : %s');

                    // if the eval'ed code returned false, skip this
                    if ($res === false)
                    {
                         continue;
                    }

                    $v = $res;
                }
     
    I'm not 100% what would happen if this is the only field being updated, so we wind up with no values to update, but this would at least be a start, even if it needs to be tweaked a little.

    Then in your eval'ed code, return false if you don't want to update.

    if this works, I'll implement it in github.
     
  3. econveyors

    econveyors New Member

    Level: Community
    Thank you hugh!
    This worked perfectly.

    I made the code change to upsert.php

    And changed my upsert value to:

    Code (Text):
    $formField = $formModel->fullFormData['tablename___elementname'];
    if(!empty($formField)):
        return $formField;
    else:
       return false;
    endif;
    I'm updating more than one field, so I can't speak to how it would work with only one field, but it does exactly what I needed in my case.

    Thank you!!
     
  4. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Can you do me a favor and test it with only one, see if it blows up?

    -- hugh
     
  5. econveyors

    econveyors New Member

    Level: Community
    Ok.

    My form has quite a few plugins going on, so I created a copy of my site to test upserting a single field with this method.

    I turned off all other form plugins and removed all fields from the Upsert plugin except for this one.

    I tested editing the record with a value in the form field and with the form field left empty.

    When there's a value in the form field, the record in the upserted table is updated as expected.

    When the form field is empty, the record in the upserted table is not updated, which is expected. I can see the correct value in the database. But, there is an error thrown on the screen.

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `upsertedtablename`.`foreign_key_element` = '###'' at line 3

    Hope this helps!
     
  6. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Thanks.

    Yeah, I thought that might be the case. If there's no fields to update, it'll try and execute a query like "UPDATE foo WHERE fk=123" or "INSERT foo", with no "SET something=whatever".

    I think I can work round that.

    -- hugh
     
  7. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    OK, at line 82, add this test ...

    Code (Text):

            $fields = $this->upsertData($upsertRowExists);

            // make sure we have at least one field to upsert
            if (empty($fields))
            {
                return true;
            }
     
    -- hugh
     
  8. econveyors

    econveyors New Member

    Level: Community
    Tested the additional code on both forms, with only a single field upserted and with multiple fields upserted.

    Everything works as expected and no errors thrown.
     
  9. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
  10. econveyors

    econveyors New Member

    Level: Community
    Great! Thanks for all your help!
     
    cheesegrits likes this.
  11. econveyors

    econveyors New Member

    Level: Community
    So, I seem to be experiencing a weird issue with this again.

    I did some testing and I believe the issue is only happening when the logged in user belongs to the Super Administrator group.

    When a user not in the Super Administrator group edits a record, the upserted field behaves as expected: when the form field is empty return false so the data in the upserted table is not overwritten.
    When a user in the Super Administrator group edits a record, the upserted field is overwritten when the form field is empty.

    Any thoughts on that?
     
  12. econveyors

    econveyors New Member

    Level: Community
  13. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Hey - sorry about the delay, I was on a much needed vacation with no Internet for a week.

    Looking at the code now, see if I can see anything obvious.

    -- hugh
     
  14. econveyors

    econveyors New Member

    Level: Community
    Glad you were able to take a break! :)

    Thanks for taking a look!
     
  15. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    I can't see anything obvious in the upsert code, we don't check ACL's anywhere.

    Is your code still the simple stuff you quoted previously?

    Can you put a 'dump' in there ...

    Code (Text):

    $formField = $formModel->fullFormData['tablename___elementname'];
    var_dump($formField);exit;
    if(!empty($formField)):
        return $formField;
    else:
       return false;
    endif;
     
    ... and make sure it runs your code, and you get an otherwise blank screen with the expected field value dumped.

    -- hugh
     
  16. econveyors

    econveyors New Member

    Level: Community
    Yes, the code is still what I posted before. Other things have changed in the application since the original post was resolved, but nothing that I can think of that would affect this and unfortunately I couldn't tell you exactly what changes have been made. I didn't think to test users with different access levels in the initial resolution so I can't say if it was happening before or not. I just happened to notice it when I was working on other parts of the application. It definitely only happens when a user is assigned to the Super Admin group. I've got a "Site Administrator" group which can do most things in the site except for the really technical stuff. I even tested having a Super Admin in both groups thinking maybe I could just add my Super Admins to the Site Admin group as a work around, but the issue still occurred.

    I tried the dump. Here's what it gave me.

    With a blank form field: string(0) ""
    With a non-blank form field: string(11) "XXXXXXXX999" - which is the correct value I entered
     
  17. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Hmmm, well I'm baffled then. When that field is an empty string (string(0) "") your code will then return false, and I don't see anything in the upsert plugin code that would behave any different for different group memberships - there literally is nowhere in that code that even looks at access levels or group membership.

    Try moving the var_dump ...

    Code (Text):

    $formField = $formModel->fullFormData['tablename___elementname'];
    if(!empty($formField)):
        return $formField;
    else:
       var_dump($formField);exit;
       return false;
    endif;
     
    ... and make sure the dump now only triggers when the field is empty.

    -- hugh
     
  18. econveyors

    econveyors New Member

    Level: Community
    So, I had some other items I needed to focus on for a bit, but I'm still stuck on this one.
    I moved the var_dump so it only triggers when the field is empty and it returned: string(0) ""

    It's quite possible I changed something elsewhere along the way between when this was first working and when I noticed the issue with editing a record as a Super User that would affect the functionality, I just don't know what that would be. I know... that's super helpful, right?
     

Share This Page