Current Record: field__pullValue #104

Table of Contents Example References The field__pullValue() delegate class method can be used to transform database from the database ...

Current Record: field__pullValue #104

Table of Contents Example References The field__pullValue() delegate class method can be used to transform database from the database ...

field__pullValue delegate class method

[Permalink]
Table of Contents

The field__pullValue() delegate class method can be used to transform database from the database for use in an edit/new record form. Sometimes it is the case that we want users to be able to work with data differently than it is stored in the database.

This method should always be accompanied by a corresponding field__pushValue method which performs the inverse operation and is used to transform the value in an edit form into a format that can be stored in the database.

Example

Given a field start_ip that stores an IP address in the database as an unsigned INT, we want to be able to work with the data on the edit form in a friendlier format - the standard IP address dot notation, so we define pullValue and pushValue methods for the field in the table's delegate class.

class tables_ip_blocks {

    /**
     * @param Dataface_Record $record The record we are pushing the value
     *        into
     * @param HTML_QuickForm_element $el The QuickForm widget that we are 
     *      retrieving the value from.
     */
    function start_ip__pushValue($record, $el){
        $val = ip2long($el->getValue());
        if ( $val !== false ){
            return sprintf('%u', $val );
        }
        return null;
    }
    
    function start_ip__pullValue($record, $el){
        $val = $record->val('start_ip');
        if ( $val )
            return long2ip($val);
        return $val;
    }
}

References

blog comments powered by Disqus
Powered by Xataface
(c) 2005-2024 All rights reserved