get field type

get field type

lucianolunalucianoluna Posts: 11Questions: 3Answers: 0

Hello,
how to get the field type (text, textarea, upload, datetime, ecc.) via editor.field(name).????

I need to convert all text fields to uppercase before save and I'm using event "preSubmit"

editor.fields().forEach(function(elem) {
if field type is text or textarea // **************
editor.field( elem ).set( String(editor.field(elem).val()).toLocaleUpperCase() );
});

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    There's not an API call to get the configuration data, so there would be two options:

    1. make a note of the config that you used when initialising Editor. You could either do this with a constant or a function that returns the config

    2. delve into the Editor internal structures. This isn't really recommended, as that data is private and may change, but it would be consistent for each release (something like this)

    Colin

  • lucianolunalucianoluna Posts: 11Questions: 3Answers: 0

    Followed your suggestion n.1 and now can change only text fields to uppcase.

    The problem now is I see visually the fields changing correctly to uppercase but aren't saved in the DB in uppercase.

    I used the event "preSubmit" to execute this code:

            editor.fields().forEach(function(elem) {
                let fieldType = getFieldType(elem);
                if ( fieldType === "text" || fieldType === "textarea" ) {
                    editor.set(elem, String(editor.field(elem).val()).toLocaleUpperCase() );
                }
            });
    
  • allanallan Posts: 61,635Questions: 1Answers: 10,092 Site admin

    You'd need top use initSubmit if you want to change the value of a field before submission. By the time preSubmit happens, the field values have already been read.

    One thing, I'd suggest you actually do this conversion to uppercase on the server-side. It would be trivial to bypass on the client-side which might cause some issues for you?

    Allan

  • lucianolunalucianoluna Posts: 11Questions: 3Answers: 0

    yes initSubmit event solve the problem! thank you

    Have some hint how to do the conversion to uppercase on server-side?

  • allanallan Posts: 61,635Questions: 1Answers: 10,092 Site admin
    Answer ✓

    What are you using on the server-side? If you are using our PHP libraries for example, then you could use a custom set formatter along with PHP's strtoupper.

    Allan

  • lucianolunalucianoluna Posts: 11Questions: 3Answers: 0

    thank you

Sign In or Register to comment.