Formal\Form::execute PHP Method

execute() public method

public execute ( )
    function execute()
    {
        # Obtaining morphology from model object
        $oMorpho = $this->getMorpho();
        $this->aErrors = [];
        $oMorpho->elements()->reset();
        foreach ($oMorpho->elements() as $oElement) {
            # If element is readonly, skip process
            if ($oElement->option("readonly")) {
                continue;
            }
            $sPropName = $oElement->option("prop");
            # posted value is fetched, then passes to element before persistance
            if ($oElement->posted()) {
                $sPostValue = $this->postValue($sPropName);
                $oElement->setValue($sPostValue);
                $sValue = $oElement->value();
                $this->modelInstance()->set($sPropName, $sValue);
            } else {
                $oElement->setValue($this->modelInstance()->get($sPropName));
            }
        }
        $oMorpho->elements()->reset();
        foreach ($oMorpho->elements() as $oElement) {
            $aValidation = $oElement->optionArray("validation");
            if (empty($aValidation)) {
                continue;
            }
            $sValue = $oElement->value();
            foreach ($aValidation as $sValidation) {
                # If element is readonly, skip process
                if ($oElement->option("readonly")) {
                    continue;
                }
                $sParam = false;
                if (strpos($sValidation, ":") !== false) {
                    $sValidation = strtok($sValidation, ":");
                    $sParam = strtok(":");
                }
                $sMethod = "validate" . ucfirst(strtolower($sValidation));
                if (!method_exists($this, $sMethod)) {
                    throw new \Exception("\\Formal\\Form::execute(): no validation method for '" . htmlspecialchars($sValidation) . "'");
                }
                if ($sParam === false) {
                    $mValid = $this->{$sMethod}($sValue, $oMorpho, $oElement);
                } else {
                    $mValid = $this->{$sMethod}($sValue, $oMorpho, $oElement, $sParam);
                }
                if ($mValid !== true) {
                    $this->declareError($oElement, $mValid);
                    break;
                    # one error per element per submit
                }
            }
        }
        # Calling validation hook if defined
        if (($aHook = $this->option("hook.validation")) !== false) {
            call_user_func($aHook, $this, $oMorpho);
        }
        if (!$this->refreshed() && empty($this->aErrors)) {
            # Model object is persisted
            # Last chance to generate a confirm message corresponding to what *was* submitted ("Creating", instead of "Editing")
            if ($this->floatingModelInstance()) {
                $this->sDisplayMessage = \Formal\Core\Message::notice($this->modelInstance()->humanName() . " <i class='" . $this->modelInstance()->icon() . "'></i> <strong>" . $this->modelInstance()->label() . "</strong> has been created.", "", false);
                $bWasFloating = true;
            } else {
                $bWasFloating = false;
                $this->sDisplayMessage = \Formal\Core\Message::notice("Changes on <i class='" . $this->modelInstance()->icon() . "'></i> <strong>" . $this->modelInstance()->label() . "</strong> have been saved.", false, false);
            }
            $this->modelInstance()->persist();
            if ($bWasFloating === false) {
                # Title is generated now, as submitted data might have changed the model instance label
                $this->sDisplayTitle = "Editing " . $this->modelInstance()->humanName() . "<i class=" . $this->modelInstance()->mediumicon() . "></i><strong>" . $this->modelInstance()->label() . "</strong>";
            }
            $this->bPersisted = true;
        } else {
            $this->bPersisted = false;
        }
    }