Usage example

Now let’s test our model using a small PHP script (in /usr/local/opnsense/mvc/script/ ):

// initialize phalcon components for our script

// include myModel and the shared config component
use myVendorName\myModule\myModel;
use OPNsense\Core\Config;

// create a new model, reading the model definition and the current data from our config.xml
$myMdl = new myModel();
$myMdl->exampleNumber =1;
$myMdl->contacts->someText = "just a test";

// add a new contact node
$node = $myMdl->contacts->entity->add();
$node->email = "";
$node->name = "my test user";

// perform validation on the data in our model
$validationMessages = $myMdl->performValidation();
foreach ($validationMessages as  $messsage) {
    echo "validation failure on field ". $messsage->getField()."  returning message : ". $messsage->getMessage()."\n";

// if validation succeeded, write data back to config
if ($validationMessages->count() == 0) {
    // serialize our model to the config file (config.xml)
    // (this raises an error on validation failures)
    $cnf = Config::getInstance();

If you fill in an invalid value to one of the validated fields, you can easily try the validation. Try to input the text “X” into the field exampleNumber to try out.

When inspecting our config.xml file, you will notice the following content has been added to the root:

        <name>my test user</name>
      <someText>just a test</someText>