Set a columns value in php editor server side based on the value of another column.

Set a columns value in php editor server side based on the value of another column.

itramitram Posts: 41Questions: 14Answers: 0

How to set a column value in php editor server side based on the value of another column.
I tried (but it doesn't work):

Field::inst('myTable.col2')->set(function ($val, $data){
     $setResult = false;
     if ($val >= $data['data']['myTable']['col1']){
        $setResult = $data['data']['myTable']['col1'];
     }
     return $setResult; 
})

So, I need to apply col1 to col2 if the value of col2 is bigger than the col1 value, otherwise, return false to prevent user modification.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,443Questions: 1Answers: 10,053 Site admin

    Field->set() flags whether the field is settable or not. You want Field->setValue(). See the docs here.

    Note that if you return false from setValue() it will write false to the db. Do you need to write $val (i.e. the col2 value submitted) if the condition doesn't match? If so, return that in such a case.

    Allan

  • itramitram Posts: 41Questions: 14Answers: 0
    edited October 2022

    Thanks Allan...
    But may I pass arguments to the function? as:

    Field::inst('myTable.col2')->setValue(function ($val, $data){
    ...
    

    I get system error if I pass arguments to it. I can only make it work without arguments, but I need the arguments to apply the value depending on the condition.
    This is the error I get:
    Fatal error: Uncaught ArgumentCountError: Too few arguments to function {closure}(), 0 passed in /var/www/html/panel/assets/lib/datatables-editor/lib/Editor/Field.php on line 860 and exactly 2 expected in /var/www/html/panel/controllers/test.php

  • itramitram Posts: 41Questions: 14Answers: 0

    further to my previous question, The user must be unable to directly modify col2, that's why I tried to set->(false). Col2 is to be modified only if the condition applies when the user modifies col1, but not otherwise.
    So, I don't know how to accomplish both requirements.

  • itramitram Posts: 41Questions: 14Answers: 0

    Well, no idea if there is a better way to do it, but I ended with :

    ->on('preEdit', function ($editor, $id, $values) {
            $col2 = min($values['col2'], $values['col1']);
            $editor
                ->field('col2')
                ->setValue($col2);
        })
    

    But the problem to prevent the user modifies col2 value directly remains.
    If I set Field::inst('col2')->set(false), col2 is not modified according the condition in the code above.
    Not using 'preEdit', nor with 'postEdit'.
    is there any way to prevent user edit or to set the pre-user edit value?

  • allanallan Posts: 61,443Questions: 1Answers: 10,053 Site admin
    Answer ✓

    Yup - that is exactly the right ay to do it!

    You can also in that function do $editor->field('col2')->set(false); if the condition means that the data for col2 should not be updated (assuming I've understood the issue correctly).

    Allan

  • itramitram Posts: 41Questions: 14Answers: 0

    Thanks Allan

Sign In or Register to comment.