CodeIgniter Forums
Entities/Model - Validation and Calculation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Entities/Model - Validation and Calculation (/showthread.php?tid=77462)



Entities/Model - Validation and Calculation - Mr_Yekta - 09-03-2020

hi

i have this inputs
PHP Code:
$inputs = [
        'revenue' => 100,
        'cost' => 50,
        'tax' => '20',
    ]; 



and i need this outputs to save it on database
PHP Code:
$outputs = [
        'revenue' => 100,
        'cost' => 50,
        'tax' => 20,
        'EBITDA' => 50,
        'netProfit' => 40,
    ]; 

i'm using Entities and Model
1- where i must validate fileds?
2- where i must caculate data to set netProfit and EBITDA for save in database?


RE: Entities/Model - Validation and Calculation - Gary - 09-06-2020

1) One can place validation in the Entity so that validation is handled automatically, and is closely associated with the data it's validating (though, personally, because I find that in my typical usage, any given field frequently has several different conditions that sometimes do and sometimes don't apply, this started looking complicated... and I've resorted to having a separate 'validation class' that handles user input, which the Controller invokes separate to the Entity class (an approach that also has it's own inherent drawbacks)).

2) "Business rules" are typically supposed to be placed in the Model.