Jump to content

How to clear (sticky) Session errors manually?


Recommended Posts

Having a problem with errors staying (1 page reload) to long in $session. 
On the Inputfield I call ->getErrors(true); to clear the errors for the Inputfield. Works Fine !
 
But the message in session (on top of the admin) stays 1 page save longer. And I don't know how to get rid of it. 
Initially I though to delete it from $session directly with:

$key = $field->getErrorSessionKey(); // gets the session error key from Inputfield 
$session->remove($key) // No succes :-(

Can somebody help me out here ?
 

<?php

public function init() {
    $this->addHookAfter('ProcessPageEdit::buildForm', $this, 'afterPageEditBuildForm');
}

public function afterPageEditBuildForm(HookEvent $event) {

    $form = $event->return;
    $page = $event->object->getPage();

    if (!$page->id) return;

    // return if wrong template
    $template = $page->template;
    if (strpos($template->name, 'element_') !== 0) return;

    // table field with defaults
    $rootParent = $page->rootParent;
    $table = $rootParent->settings;

    $allowed = array(
        'FieldtypePageTitle',
        'FieldtypeText',
        'FieldtypeURL',
        );

    foreach ($template->fields as $f) {

        // only allowed fields
        if (!in_array($f->type, $allowed)) continue;

        // Inputfield
        $obj = $form->getChildByName($f->name);

        if ($obj->value) {
            $obj->getErrors(true);
            continue;
        }

        // Find row in rootParent with default value
        $row = $table->get("template={$template->name}, field={$f->name}");

        // Skip if no row is found
        if (!count($row) || !$row) continue;

        // clear errors & set trackChange
        $obj->getErrors(true);
        $obj->error($this->_('Standaard waarde toegevoed, nog niet opgeslagen.'));
        $obj->resetTrackChanges();
        $obj->trackChange('value');
        $obj->value = $row->value;

    }
}
Link to comment
Share on other sites

Here's my take on the problem, but I might be in over my head...

When looking at the PW source code for ProcessPageEdit.php, the buildForm function seems to be called on line 112, before the processSave function (which calls processInput) is called on line 115. So when hooking after buildForm, the errors have not yet been set by processInput, so you can't purge them with getErrors(true). Setting the hook on the processInput function might do the trick?

Link to comment
Share on other sites

ProcessInput Is the first method that get's called after a post. That method cleans the data from $input->post, the function compares the posted value with the value in DB. When different, change tracking is called to tell PW that the value has been changed and should be saved and make the data available. The errors I talk about are the errors I set manually the the Inputfield object.

Link to comment
Share on other sites

  • 3 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...