No space validation in text field.

Hi All Developers,

i need to create custom text field, and apply validation, that fields value should not have any space.

if user put space in the field and try to save the record, then system popup the error message.

any body have any idea? how we can achieve.

Thanks in advance  

Parents Reply Children
  • Hi


    Following the tutorial previously shared, you can have a working example with these steps: 



    1 - Created a new field in Studio to add the validation, on my test I've named it testfield and added it to the record view 

    https://www.screencast.com/t/gaJk4hIh

    2 - Create a custom error message label file: 

    Extension/application/Ext/Language/en_us.nospace_error_message.php

    <?php
    
    $app_strings['ERROR_NOSPACE_MESSAGE'] = 'Please remove empty spaces';

    3 - Extend the CreateView controller to add the validation by creating the file 

    custom/modules/Accounts/clients/base/views/create/create.js

    ({
        extendsFrom: 'CreateView',
        initialize: function (options) {
    
            this._super('initialize', [options]);
    
        //add custom message key
            app.error.errorName2Keys['nospace_message'] = 'ERROR_NOSPACE_MESSAGE';
    
                //add validation tasks
            this.model.addValidationTask('check_testfield_spaces', _.bind(this._doValidateTestFieldSpaces, this));
    
        },
    
            _doValidateTestFieldSpaces: function(fields, errors, callback) {
            // Validate testfield for spaces
            const testfield_c = this.model.get('testfield_c');
            if (testfield_c.includes(' ')) {
    
            errors['testfield_c'] = errors['testfield_c'] || {};
            errors['testfield_c'].nospace_message = true;
    
            }
    
    
            callback(null, fields, errors);
        },
    })

    4 - Extend the RecordView controller to add the validation by creating the file 

    custom/modules/Accounts/clients/base/views/record/record.js

    ({
        /* because 'accounts' already has a record view, we need to extend it */
             extendsFrom: 'AccountsRecordView',
             initialize: function (options) {
    
            this._super('initialize', [options]);
    
        //add custom message key
            app.error.errorName2Keys['nospace_message'] = 'ERROR_NOSPACE_MESSAGE';
    
                //add validation tasks
            this.model.addValidationTask('check_testfield_spaces', _.bind(this._doValidateTestFieldSpaces, this));
    
        },
    
            _doValidateTestFieldSpaces: function(fields, errors, callback) {
            // Validate testfield for spaces
            const testfield_c = this.model.get('testfield_c');
            if (testfield_c.includes(' ')) {
    
            errors['testfield_c'] = errors['testfield_c'] || {};
            errors['testfield_c'].nospace_message = true;
    
            }
    
    
            callback(null, fields, errors);
        },
    })

    5 - Run a quick repair and rebuild. 


    After this, the validation should work like this: 






    Can you give it a try to see if this matches your requirement? 

    I hope this helps. 

    André