Question

In datamapper you can use $u->error_message('custom', 'This is a custom error message.'); to set an error message when validation fails upon a save.

However I would like to be able to use this method to return message even in cases when the validation did not strictly fail - like warning or status messages after a save, that I want to return to the user.

Anyone knows a way to achieve this?

Was it helpful?

Solution 2

Thank you for your replies, by looking at the datamapper source code i found out that validations fails when $item->error->all is not empty. Therefore i could add a method in the datamapper library that does exactly the same thing as $item->error_message() except populating the $item->error->all array. like this:

/**
     * Status Message
     *
     * Adds an status message to this objects error object.
     *
     * @param string $field Field to set the error on.
     * @param string $error Error message.
     */
    public function status_message($field, $error)
    {
        if ( ! empty($field) && ! empty($error))
        {
            // Set field specific value
            $this->error->{$field} = $this->error_prefix . $error . $this->error_suffix;

            // Add field error to errors all list
            // $this->error->all[$field] = $this->error->{$field};

            // Append value to error message string
            $this->error->string .= $this->error->{$field};
        }
    }

OTHER TIPS

Couldn't you simply parse a value to the view file upon success?

Controller:

/* Validated code */

/* Updates / Inserts */

$data['success_update'] = TRUE;

View:

<? if ( ! empty($success_update)): ?>
<div id="success_update">
    Thingie updated successfully
</div>
<? endif; ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top