When a PHTML template (belonging to a Loki Component) is rendered, an instance of the ComponentViewModel class is automatically inserted as $viewModel
. This object allows you to retrieve a value via $viewModel->getValue()
which actually comes from the ComponentRepository but which is filtered and validated on the fly. This works great if your component only deals with a single value (or an array of values of the same type). If you have the need to retrieve data in another way (by using other ComponentViewModel methods or via other ViewModels or via the Block), automatic filtering and validation is not applied.
Whenever the PHTML template allows for some kind of user interaction (like via an HTML <input/>
element), the value of that input could automatically be synced with the Alpine.js component (<input @input="setValue" />
) and submitted via a POST-request to the server (<input @change="submit" />
). At that moment, the input value will be filtered and validated as well.
The POST-request is made to the Loki\Components\Controller\Index\Html
controller class, which delegates saving the input to the Loki\Components\Util\Controller\RepositoryDispatcher
class. Filtering and validation occurs within this class: First filtering and then validation.
If validation fails, the repository method saveValue()
is not called. If validation succeeds, the value is saved. In both cases, the next phase of the AJAX call is executed: Rendering all relevant targets (aka blocks).