Editor: Upper Case On Submit

Editor: Upper Case On Submit

phatlixphatlix Posts: 13Questions: 4Answers: 0

Hello,

I am searching for the proper syntax to use in order to turn edited fields into uppercase when you submit the edit.

I have seen another question and Allan had mentioned using setFormatter uppercase(), but no matter what I try, I get an error.

I have tried the following...

        Field::inst( 'artist' )
            ->setFormatter(uppercase())
        Field::inst( 'artist' )
            ->setFormatter('Format::uppercase')
        Field::inst( 'artist' )
            ->setFormatter('Format::UppercaseString')
        Field::inst( 'artist' )
            ->getFormatter('Format::UppercaseString')
            ->setFormatter('Format::UppercaseString')

The error speaks to the method not being defined in Field.php (lib/Editor/Field.php). Do I need to write this function? Or I am just not setting this properly?

Thanks for your help.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    Answer ✓

    This example here should help. It's using a function called CapitaliseFirstLetter, which is then called in the setFormatter. Hopefully that'll get you going, if not, please let us know,

    Colin

  • phatlixphatlix Posts: 13Questions: 4Answers: 0

    Hi Colin,

    So write it my self? :smile:

    I did a search for the CapitaliseFirstLetter function in the forum and found another thread where they wrote the function to the Format.php.

    Following suit, I grabbed the php code related to ucwords which happened to be strtoupper. (https://www.w3schools.com/PHP/func_string_strtoupper.asp)

    Wrote the following to the bottom of /lib/Editor/Format.php

            /**
             * custom functions
             */
            public static function CapitaliseFirstLetter( $val ) {
                    return ucwords($val); 
            }
            
            public static function toUpperCase( $val ) {
                    return strtoupper($val);
            }
    

    Added the CapitaliseFirstLetter function to cuz... why not? Might add the rest of those functions just for completeness.

    Then formatted my "PHP implementation" file like so

    Field::inst( 'artist' )
                ->getFormatter('Format::toUpperCase')
                ->setFormatter('Format::toUpperCase')
                ->validator( Validate::notEmpty( ValidateOptions::inst()
                    ->message( 'Artist is required.' )
                ) ),
    

    Low and behold, that freak'n worked! :#

    Thank you Colin for the direction.

    Regards,

    Kenny

  • phatlixphatlix Posts: 13Questions: 4Answers: 0

    Letter manipulators I added to Format.php.

            public static function toUpperCase( $val ) {
                    return strtoupper($val);
            }
    
            public static function toLowerCase( $val ) {
                    return strtolower($val);
            }
    
            public static function strFirstLetterUpper( $val ) {
                    return ucfirst($val);
            }
    
            public static function strFirstLetterLower( $val ) {
                    return lcfirst($val);
            }
    
            public static function strFirstLetterEachWord( $val ) {
                    return ucwords($val);
            }
    

    Take care!

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    Ha, excellent, glad all sorted - thanks for reporting back,
    Colin

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    Rather than modifying the library file with your own methods, I’d suggest using a small anonymous function for your formatter here - see docs here.

    The reason being is that with the method you’ve used every time you upgrade the libraries, you’ll need to remember to transfer over the custom methods.

    Allan

  • phatlixphatlix Posts: 13Questions: 4Answers: 0

    Thanks Allan.

    I had thought about updates overwriting my customizations (and have documented all the spots I made modifications). Perhaps it would be a feature request, but would it not be possible to create a "custom" or "includes" directory, and keep all customization in there and have the system look to that location using includes or required calls?
    I'm already trying to do this with styling.

    Thanks for the tip Allan!

    Regards,

    Kernny

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    Hi Kernny,

    Nice idea - thanks. I’ve added it to our feature tracker.

    Allan

This discussion has been closed.