custom field validate function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • czcpf
    Senior Member
    • Aug 2022
    • 160

    custom field validate function

    Hello,

    I have a custom field I'm creating. Before the model is saved, I want to set the field value and ensure that it completes before validate returns. Setting the field value requires a 'thenable' function so it isn't synchronous.

    To make sure it 'waits' on finishing, I overwrote the validate function for my custom field as shown below. I also overwrote the 'fetch' function which is where the actual 'thenable' bit is happening. I included that below as well.

    Trouble is, I am always seeing 'invalid' when I attempt to save the record. If I remove 'async' and 'await' from the validate function it allows me to save the record but the custom field attributes are never set because it saves the record before this.fetch().then completes.... Can anyone advise?


    Code:
    /**
    * Validate field values.
    *
    * @return {boolean} True if not valid.
    */
    validate: async function () {
    
    let valid = Dep.prototype.validate.call(this);
    
    await this.fetch().then(
    attributes => {
    this.model.set(attributes, {silent: true});
    valid = false;
    }
    ).catch(
    (error) => {
    console.log(error);
    valid = true;
    }
    );
    
    return valid;
    
    },​
    Code:
    fetch: async function () {
    
    let data = Dep.prototype.fetch.call(this);
    
    await this.prepareMyFieldVal().then(
    object => {
    data[this.name] = object;
    }
    ).catch((error) => console.log(error));
    return data;
    },​







  • yuri
    Member
    • Mar 2014
    • 8467

    #2
    Hi,

    The method should return boolean as stated in JSDoc. It means you cannot use async.

    You can't use async validation in the frontend. I recommend to rely on serverside validation instead, or preload the need data beforehand.
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • czcpf
      Senior Member
      • Aug 2022
      • 160

      #3
      Thank you for your response. I was only using validate because it is called before entity is saved. Essentially, I needed to modify the custom field value before it’s saved to database.

      What I ended up doing was writing a custom beforeSave process and put it in common/hooks (similar to how VersionNumber) is set.

      it works, but this isn’t efficient because this hook gets called beforeSave for all entities even if the entity doesn’t contain the custom field type. So I loop through the entities fields and types and if it has it then I set the custom value and then $entity->set …

      My follow up question is, is there something like beforeSave hook but for a field on the backend ? Like a value setter or ORM before save ?

      Comment

      • yuri
        Member
        • Mar 2014
        • 8467

        #4
        > is there something like beforeSave hook but for a field on the backend

        There's no need, as looping through all fields is extremely fast.
        If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

        Comment

        Working...