Setting A Default Value In Editor

Setting A Default Value In Editor

th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1
edited August 2020 in General

I have an editor instance and when the user opens the form I would like to set a select field with a default value of the logged in users ID. I only need to do this on create. I have a session variable (PHP) that contains the users email address. Using that session variable I would need to query a table and get the users ID and set it as the default in the editor form. I would like to use that session variable as I'm already passing it to other PHP pages for validation of the user, there permissions and if their account is still active.
I don't know where to start with this one but it seems it would be easy enough to do with datatables editor.

This question has accepted answers - jump to:

Answers

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1
    edited August 2020

    One more piece of info. I have a dependent() setup so if the user selects their ID from the dropdown the other dropdown gets populated with their full name and visa versa. I'd like to keep that functionality. So userid would be set on create and the user's full name would change based on the userid.

  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin
    Answer ✓

    The first thing to do is to get your PHP variable into Javascript - then you can use the fields.def option to set a default value for the field.

    For example:

    def: <?php echo json_encode( $_SESSION['userId'] ); ?>,
    

    Note that I used json_encode() to make sure that valid Javascript is output for the variable value (it will deal with the quoting of the string, etc).

    Allan

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    That worked and was simple :)
    I had to get rid of the double quotes as I'm using joined tables and this value was an index. I also changed from user_id to id, the index.

    echo json_encode( $_SESSION['id'], JSON_NUMERIC_CHECK )
    

    One thing I noticed was when opening the form the user_id was set corectly to the logged in user but if I cancel it and open it again it defaults to the first item in the select field.
    I'm wondering if that is because of the savestate setting I'm using. I can give you a log in if you'd like but I'll have to send it to you privately.

  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin

    That shouldn't happen due to state saving.

    So if you cancel the "create" command and then start a new one, it no longer shows the default? That shouldn't happen! If it didn't show the value on edit, that's because the value isn't present in the select list.

    I think I probably would need to be able to see a test case of that issue to understand what is going wrong.

    Thanks,
    Allan

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    Ok. I can give you a login but I need to send it privately.

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    Yes. The initial create shows the correct user id. When I cancel and try to create again the user id is changed. Everything in the select dropdown exists. Also I can see the correct value in the source code of the page in both scenarios.

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    Just so I'm clear, the id should only show on a create. On edit it should retain the original value.

  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin

    Oh I see!

    editor.on('initCreate', function () {
      editor.field('myIdFieldName').show();
    });
    
    editor.on('initEdit', function () {
      editor.field('myIdFieldName').hide();
    });
    

    For security you should also use server-side events to make sure that the id isn't written to on edit.

    Allan

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    I still didn't explain this well. Let me try again :smiley:
    On create the user's id should be the default in a select field. It can still be changed however as users sometimes take over someone else's case.
    A user may add case notes to another users case so on edit the id should not change but can still be changed by a user.
    So the editor select field for the user id should only be set to the current user when creating a new case but still have the ability to be changed and that same field should have the ability to be changed on edit.

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    Looks like I fixed my problem. I believe it has to do with dependent. I have the following...

     {
                    label: 'Status:',
                    type: 'select',
                    name: 'cases.status',
                    def: 2
                  }, {
                    label: 'ID:',
                    type: 'select',
                    name: 'cases.id',
                    def: "; echo json_encode( $_SESSION['id'], JSON_NUMERIC_CHECK ) ;
    print"
                  }, {
                    label: 'Full Name:',
                    type: 'select',
                    name: 'cases.full_name',
                    def: "; echo json_encode( $_SESSION['id'], JSON_NUMERIC_CHECK ) ;
    print"
                  }, {
                    label: 'Paper Number:',
    
    etc.-------
    
        editor.dependent(
            'cases.id',
            function(val, data, callback) {
              editor
              .field( 'cases.full_name' ).set( editor.field('cases.id').val() );
            },
            {
              event: 'keyup change'
            }
        );
        
        editor.dependent(
            'cases.full_name',
            function(val, data, callback) {
              editor
              .field( 'cases.id' ).set( editor.field('cases.full_name').val() );
            },
            {
              event: 'keyup change'
            }
        );
    

    Once I set the full_name to the id the default worked every time. Thank you once again for your help Allan.

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    Since full_name loads after id I think that was causing problems.

  • colincolin Posts: 15,112Questions: 1Answers: 2,583
    Answer ✓

    Glad all sorted, thanks for reporting back,

    Colin

This discussion has been closed.