logo
down
shadow

how to write validation in sencha touch / ext js model


how to write validation in sencha touch / ext js model

By : Patrik Bobor
Date : November 22 2020, 11:12 AM
seems to work fine I know this is an old question, but I ran into it while dealing with the same problem and wanted to share my approach.
I created a method in the model, which does the custom validation, something like:
code :
Ext.define('app.model.TeamEmpAssignment', {
    extend : 'Ext.data.Model',
    config : {
        // config here
    },

    checkDates: function (errors) {
        // assuming the dates are timestamps
        if (this.get('beginDateTime') > this.get('endDateTime')) {
            errors.add(Ext.create('Ext.data.Error', {
                field  : 'beginDateTime',
                message: 'Begin date can\'t be after end date'
            }));
        }
    }

});
var record = Ext.create('Ext.data.Errors');
// set some values here
var errors = record.validate();
record.checkDates(errors);

console.log(errors.isValid());
Ext.define('app.model.TeamEmpAssignment', {
    extend : 'Ext.data.Model',
    config : {
        // config here
    },

    checkDates: function (errors) {
        // assuming the dates are timestamps
        if (this.get('beginDateTime') > this.get('endDateTime')) {
            errors.add(Ext.create('Ext.data.Error', {
                field  : 'beginDateTime',
                message: 'Begin date can\'t be after end date'
            }));
        }
    },

    validate: function () {
        var errors = this.callParent(arguments);

        this.checkDates(errors);

        return errors;
    }

});


Share : facebook icon twitter icon
How to add a custom validation rule to a model in Sencha Touch

How to add a custom validation rule to a model in Sencha Touch


By : elysems
Date : March 29 2020, 07:55 AM
To fix this issue I think it's one of the slight errors in the documentation. I got them to work by adding some code
code :
if (Ext.data) {
    Ext.data.validations.custom = function (config, value) {
        if (config && Ext.isFunction(config.fn)) {
            //this should be the model
            if (config.self) {
                return config.fn.call(config.self, value);
            } else {
                return config.fn(value);
            } 
        }
        else 
        {
            return false;
        }
    };
    Ext.data.validations.customMessage = "Error";
}
{ 
    type: 'custom', field: 'SomeField', message: "Your field is bad",
    fn: function (SomeFieldValueForThisInstance) {
       //Add some validation code.  The this pointer is set to the model object
       //so you can call this.get("SomeOtherFieldToCheck")
       //or any other instance method

       //if the field is good
       return true;
       //else
       return false;
    }
}
        if (typeof this.init == 'function') {
            this.init();
    YourModel.prototype.init = function () {
        var i, len;
        if (this.validations) {
            for (i = 0, len = this.validations.length; i < len; i++) {
                this.validations[i].self = this;
            }
        }
    };
Sencha touch 2.0 : Custom model validation with stores

Sencha touch 2.0 : Custom model validation with stores


By : Chris Phillips
Date : March 29 2020, 07:55 AM
I hope this helps . I found this solution (in the Sencha Touch 1.0 forum, but it worked for me in ST2): http://www.sencha.com/forum/showthread.php?122680-Conditional-fields-validations
It requires an override of the validate() method of the Model class, but if you don't mind then this is easier than what I exposed earlier...
Sencha Touch 2.1 - form validation using Model

Sencha Touch 2.1 - form validation using Model


By : phuc le
Date : March 29 2020, 07:55 AM
hop of those help? I am assuming since you have done console.log of errors.items you must have seen rec.message value using debugger to understand why is it undefined.
Anyways this is how I would do:
code :
var data="";
errors.each(function (item, index, length) {
  // Each item in the errors collection is an instance of the Ext.data.Error class.
  data = data + '|'+item.getField()+' - '+ item.getMessage() +'|';
});
Ext.Msg.alert("Validation Failed", data);
Sencha Touch simple model validation matcher

Sencha Touch simple model validation matcher


By : Shreenidhi Hegde
Date : March 29 2020, 07:55 AM
To fix this issue This expression will validate one of the following conditions is true
value is 100 value is a double digit number 10-99 value is any single digit number 0-9 value is letters A thru F
I have nested json,how to write model and store ?? sencha touch 2

I have nested json,how to write model and store ?? sencha touch 2


By : typo_12
Date : March 29 2020, 07:55 AM
I hope this helps . Can you change the server structure to a standard extjs json store format like this? If so, your store setup will be really easy:
code :
{
    "content":[
        {
            "shipping_type":"ups_ground",
            "amount":"7.06",
            "currency_code":"USD",
            "data":[],
            "tnt":"Monday, 7\/15 at 11:00pm"
        },
        {
            "shipping_type":"ups_next_day_air_saver",
            "amount":"26.44",
            "currency_code":"USD",
            "data":[],
            "tnt":"Monday, 7\/15 at 3:00pm"
        },
        {
            "shipping_type":"ups_next_day_early_a.m.",
            "amount":"63.84",
            "currency_code":"USD",
            "data":[],
            "tnt":"Monday, 7\/15 at 8:30am"
        },
        {
            "shipping_type":"ups_next_day_air",
            "amount":"30.99",
            "currency_code":"USD",
            "data":[],"tnt":
            "Monday, 7\/15 at 10:30am"
        }
    ]
}
Ext.define("ShippingModel", {
    extend: "Ext.data.Model",
    fields: [
        {name:"shipping_type", type:"string"},
        {name:"amount",        type:"float"},
        {name:"currency_code", type:"string"},
        {name:"data",          type:"string"},
        {name:"tnt",           type:"string"}//this may need to be a date, lookup date format for this yourself
    ]
});

Ext.create('Ext.data.Store', {
    model: 'ShippingModel',
    controller: 'news',
    proxy: {
        type: 'ajax',
        url: 'your url',
        reader: {
            type: 'json',
            root: 'content'
        },
        extraParams:{
            action:'getShippingRates'//you may not need the extraParams, just putting here for example
        }
    },
    autoLoad: true
});
Ext.define("MyWeirdlyStucturedDataReader", {
    extend: "Ext.data.reader.Json",

    extractData: function(root){

        var newStructure = [];

        for(var shippingType in root)
        {
            var newRecord = root[shippingType];
            newRecord.shipping_type = shippingType;
            newStructure.push(newRecord);
        }

        return this.callParent(newStructure);

    }

});
Related Posts Related Posts :
  • How to fix "Object is possibly undefined"?
  • How can one use TensorFlow.js tf.data.generator for remote data sources since generators can't use callbacks
  • How can I unescape a string from python using javascript
  • How to add days into my date using angular pipe?
  • Keydown not firing on page loaded in <object> element
  • Refetch queries in react-native with graphql
  • How to execute functions across objects identically to executing the function itself with a this reference
  • NGRX Effect withLatestFrom Typescript error ts2345 troubleshooting
  • Make a checkbox checked if its id is present in an array
  • Remove disabled class when response code is 1 in each element javascript
  • Creating an input select in an editable form
  • How to convert string of ASCII codes to string of characters?
  • Detect system time and set css animation start time in browser
  • How can I build a dataTable, with a dropbox filter on top, calculated on backend?
  • What needs to be done to make multiple screen support for Javascript happen?
  • How to format a full year date/time
  • this.props.match.params passed into child component after authorisation
  • Highcharts: create multiple series grouped my month and year using JSON data
  • JS: Keep variable values for onclick event attached inside for loop
  • Failed prop type. Required prop types are undefined in my redux store
  • How to not import Vue in main js and store
  • Javascript make table with rows and columns given by user and change each cells colour onclick
  • How do you fix "ERROR in Path must be a string. Received undefined" in webpack4/copy-webpack-plugin
  • Protractor Fetching Email with Mail Listener: TypeError: deferred.fulfill is not a function
  • date-time picker bootstrap Tempus Dominus
  • How do I get all radio button id's via the name
  • Using d3 in for loop
  • Impact an array of objects
  • JS split string and return index of each split
  • Javascript/JS - Referring to classes and variables dynamically within a change event loop
  • Query a specific GET field?
  • Mapping multiple form inputs of the same name into a JavaScript object
  • How can I not to return a field from object in JavaScript?
  • Creating and testing a single Javascript nested function
  • Formatting server side jQuery Datatable
  • How to show tooltips onmouseover in p:calendar
  • typescript - calling function at random intervals in ionic
  • Correct way to populate an array from a promise
  • JavaScript Mini-Max Sum - Returns 0
  • canvas not clearing while radius of circle decreasing..?
  • Dynamically Growing Dropdown Component with clickoutside directive
  • MultichoiceField in ModelForm for Django with DB relations
  • React Native "if" statement behaviour? Early exit or full evaluation?
  • Call Jquery function on Input tag having data-slider
  • How to process negative shifts in Caesars Cipher (Javascript)
  • Animating Line position in Konva
  • Why a state is not resolved as a variable in nextjs
  • How to efficiently evaluate element in javascript array by using reduce function
  • Google script web app goes blank page after submit
  • Event for the load-end of dynamically added CSS files through innerHTML
  • How to retrieve some text from a sibling div element in jQuery
  • How to display half star rating in my following custom angular star rating example?
  • Angular 8: ternary operation for img src attribute?
  • Is this proper JS for this J-Query expanding textArea? Is 'this' the same as event.target in JS also?
  • How to fix user fetching from a collection in discord.js?
  • Can I create an item id when adding it to a collection?
  • Temporarily disable a specific option after it has been selected (in any selector) for all instances of that selector in
  • Vis.js timeline item align property not working
  • How to select one element instead of all elements using 'clicked' in a Django for loop
  • How to check if 2 <img> tags contain the same image
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org