Unchanged empty fields still being submitted

Unchanged empty fields still being submitted

borconiborconi Posts: 56Questions: 19Answers: 1

Hi.

I think I have found another small (but for me annoying bug).
I have set editor to only submit the changed fields and I was doing different operation on server side depending on which fields were submitted, however when trying to debug some malfunctions reported by user I realized that editor is submitting fields which weren't actually changed.

Taking a dig into it, it turns out that if a field value is set to nothing (empty) and it's not changed it's still submitted as changed, and it's all down to this function: field.compare( value, editData[ name ][ idSrc ]) ) where current value is reported as '' but the original value is reported as null.

I can think of 2 ways to fix this, first will mean to alter the multiGet to return a null in case of empty value the second approach which I have taken (and for some reason prefer) is to alter the _deepCompare function like this:

var _deepCompare = function (o1, o2) {
    if ( typeof o1 !== 'object' || typeof o2 !== 'object' ) {
        if ((o1 === '' && o2 === null) || (o1 === null && o2 === ''))
            return true
        else
            return o1 == o2;
    }

    var o1Props = _objectKeys( o1 );
    var o2Props = _objectKeys( o2 );

    if (o1Props.length !== o2Props.length) {
        return false;
    }

    for ( var i=0, ien=o1Props.length ; i<ien ; i++ ) {
        var propName = o1Props[i];

        if ( typeof o1[propName] === 'object' ) {
            if ( ! _deepCompare( o1[propName], o2[propName] ) ) {
                return false;
            }
        }
        else if (o1[propName] != o2[propName]) {
            return false;
        }
    }

    return true;
};

I haven't experienced this in Editor 1.6.2 only started getting notification from users recently and I have recently changed to 1.7.2 so I'm pretty confident that this was working slightly different in 1.6.2

Thank you.

Replies

  • allanallan Posts: 63,630Questions: 1Answers: 10,488 Site admin

    Hi,

    Thanks for this. Is the original value in the JSON data source actually null, or is it an empty string? From your description, I'm guessing that it is null.

    If that is the case, there is a discussion here that you might be interested in.

    Regards,
    Allan

  • borconiborconi Posts: 56Questions: 19Answers: 1

    Data is populated from PHP backend, for example one of the fields is a select which is populated like this from PHP:

        Field::inst( 'jobs_assigned.operativeid' )->options( function () use ( $db ) {
                                                         $out[] = array('value' => '', 'label' => 'Select from list');
                                                         global $owner; 
                                                        $attrList=$db->selectDistinct ('operatives',['id','name'],array( 'owner'=>$owner));
                                                        while ( $row = $attrList->fetch() ) 
                                                            {
                                                            $out[] = array(
                                                                    "value" => $row['id'],
                                                                    "label" => $row['name']
                                                            );}     
                                                        return $out;})
    

    On HTML/JS this it how the field is defined:

                            {label:"Operative",
                name:"jobs_assigned.operativeid",
                type:"select"},
    

    Assuming the operativeid field is not selected it does submit the value as changed ( null vs '')

    Hope it makes more sense now.

  • allanallan Posts: 63,630Questions: 1Answers: 10,488 Site admin

    Can you show me the JSON data that the client-side is loading please?

    Allan

  • borconiborconi Posts: 56Questions: 19Answers: 1

    Hi Allan.

    Json was to large to include it here but you can grab a copy from here:
    https://drive.google.com/file/d/1uX6KvXxoWRFkB4tkOh7y6vAGQYj9L_ey/view?usp=sharing

    Let me know if you need any information.

  • allanallan Posts: 63,630Questions: 1Answers: 10,488 Site admin

    Thanks for that! I'm not quite clear on where the null is coming into play I'm afraid. The original data doesn't have a null value in it, so its not there, but its possible it is something internal to Editor.

    To that end I've just checked this example to make sure it is working as expected and it does indeed appear to be.

    Are you able to give me a link to your page so I can trace it through? You can send me a PM by clicking my name above and then the "Send Message" button rather than making it public if you like.

    Regards,
    Allan

This discussion has been closed.