Editor: how to display a string when field ist empty

Editor: how to display a string when field ist empty

rostrost Posts: 26Questions: 9Answers: 1

I'm using editor in read-only display mode. Everything fine, but not with empty fields: I would like to display an information if the field ist empty instead of showing nothing, beeit a "-".

As far as I understood, editor does not support rendering fields - but how can it be done anyway?

Would be very nice if you could pass an idea!

This question has an accepted answers - jump to answer

Answers

  • rostrost Posts: 26Questions: 9Answers: 1
    edited October 2020

    @rf1234: Thanks for helping - but what I'm looking for ist not a default value.

  • rostrost Posts: 26Questions: 9Answers: 1

    I would like to display a string like 'no value' when a field is empty, but not change data (which is why a default as proposed does not help). Maybe the pic attached helps to explain:

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    Answer ✓

    use a getFormatter on the server that returns "---" if the field is empty and a setFormatter that returns NULL or an empty string if the field contains "---".

    Field::inst( 'yourField' )
        ->getFormatter( function ( $val, $data, $opts ) {
            if ( $val <= '' ) {
                return '---';
            }
            return $val;
        } )
        ->setFormatter( function($val, $data, $opts) {
            if ( $val === '---' ) {
                return ''; //or null or whatever you need
            }
            return $val;
        }),
    
  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    edited October 2020

    Alternatively client side - doing just the same thing:

    editor
        .on('open', function (e, mode, action) { 
            if ( this.val('yourField') <= '' ) {
               this.set( {'yourField': '---'} );
            }
        })
        .on('initSubmit', function ( e, action ) {
             if ( this.val('yourField') ===  '---' ) {
               this.set( {'yourField': ''} );
            }
        })
    
  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin

    Yes - as you note there are no renders for Editor on the client-side. So either you need to us a server-side renderer or set a value like @rf1234 says.

    I think I'd probably take a slightly different approach though and use a custom field type plug-in for Editor that just will just display the data - not a disabled input as well. The read only display field can be used for exactly that. And you could put a condition into its set function that if val is an empty string, then write in the placeholder.

    Allan

  • rostrost Posts: 26Questions: 9Answers: 1

    Thanks a lot to @rf1234 and @allan !
    Finally - with your help - I solved my problem by this:

    Field::inst( 'f07_laufzeit_bis' )
                ->getFormatter( function ( $val, $data, $opts ) {
                        if ( $val <= '' ) {
                                return 'keine Angabe';
                        }
                        $date = new \DateTime( $val );
                        return date_format( $date, 'd.m.Y');
                } )
    
                ->setFormatter( function ( $val, $data, $opts ) {
                        if ( $val == 'keine Angabe' ) {
                                return NULL;
                        }
                        $date = new \DateTime( $val );
                        return date_format( $date, 'Y-m-d');
                } ),
    

    (fields with strings analogously)

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406

    Looking good!

    A long time ago I built these two formatters:
    - getFormatter: returns date in local language format en-GB or de-DE.
    - setFormatter: returns date in mysql dateTime format; input dates being en-GB or de-DE including long date formats.
    The setFormatter doesn't need to know what the user's language is as long as it is German or UK English (won't work with American date formats though!)

    function getFormatterDate(&$val, $format = null, $lang = null) {
        if ($val == null) {
            return '';
        } else {
            $date = new DateTime($val);
        }
        //if language doesn't get passed and we are in batch: we use German format
        if ( is_null($lang) ) {
            if ( isset($_SESSION['lang']) ) {
                $lang = $_SESSION['lang'];
            } else {
                $lang = 'de';
            }
        }
        if ( is_null($format) ) {
            if ($lang === 'de') {  
                return $date->format("d.m.Y");
            } else {
                return $date->format("d/m/Y");
            }
        } else { //other formats Ymd and dmy
            if ( $format === 'Ymd' ) {
                return $date->format("Ymd");
            } elseif ( $format === 'dmy') {
                if ($lang === 'de') {  
                    return $date->format("d.m.y");
                } else {
                    return $date->format("d/m/y");
                }
            } elseif ( $format === 'dm') {
                if ($lang === 'de') {  
                    return $date->format("d.m.");
                } else {
                    return $date->format("d/m/");
                }
            }
        }
        return $val;
    }
    
    function setFormatterDate($val = null) {  
        //Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: 
        //if the separator is a slash (/), then the American m/d/y is assumed; 
        //whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. 
        //If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.
        if ( is_null($val) ) {
            return $val;
        }
        if ( $val <= '' ) {
            return null;
        }
        $val = str_replace(
            ['Januar ', 'Februar ', 'März ', 'April ', 'Mai ', 'Juni ', 'Juli ',
                'August ', 'September ', 'Oktober ', 'November ', 'Dezember '],
            ['January ', 'February ', 'March ', 'April ', 'May ', 'June ', 'July ',
                'August ', 'September ', 'October ', 'November ', 'December '],
            $val);
        $val = str_replace('/', '-', $val);
        $val = str_replace('.', '-', $val);
        
        if ($val == '00-00-0000' || $val == '0000-00-00 00:00:00') {
            return null;
        }
        
        $dateTime = new DateTime($val);    
        return $dateTime->format('Y-m-d H:i:s');
    }
    
This discussion has been closed.