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.
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
Field->set()
flags whether the field is settable or not. You wantField->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
Thanks Allan...
But may I pass arguments to the function? as:
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
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.
Well, no idea if there is a better way to do it, but I ended with :
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?
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 forcol2
should not be updated (assuming I've understood the issue correctly).Allan
Thanks Allan