Current Record: field__pushValue #105

Table of Contents Example References The field__pushValue() delegate class method can be used to transform a field value as entered in...

Current Record: field__pushValue #105

Table of Contents Example References The field__pushValue() delegate class method can be used to transform a field value as entered in...

[Permalink]
Table of Contents

The field__pushValue() delegate class method can be used to transform a field value as entered in the edit form into a format that can be stored in the database.. 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__pullValue method which performs the inverse operation and is used to transform the value in the database into a format that can be edited in the edit form.

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