Announcement

Collapse
No announcement yet.

Custom JS Validation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom JS Validation

    Hello there, just starting with EspoCrm,

    What I need is kind of basic, we created a new entity, let's call it "Case"

    Case has a lot of fields, one of them is the "State" field (list type dropbox), that indicates if the "case" is closed or not.

    What I need is to make a validation before a user can save a "Case" like "Closed" , validate that some other fields are completed. (Normally I write some JS function to validate the onchange event or before the form submit)

    Already tried with dynamic forms, but no look , I keep ending showing or hidding the options, and that is not the use case.

    Any Ideas how to implement this simple validation?

    Thanks a lot and sorry for the newbie

  • #2
    Hello and welcome to EspoCRM and to the forum.

    You can accomplish your objective by using the dynamic handler class. https://docs.espocrm.com/development/dynamic-handler/

    With that class you can manipulate the front end far beyond the GUI dynamic logic feature.

    Comment


    • #3
      You can extend a field view: https://docs.espocrm.com/development...specific-field

      Then add a custom validation to that view. Here's an example how an additional 'thousandSeparator' validation is added for a varchar field: https://github.com/espocrm/espocrm/b...parator.js#L32

      Comment


      • #4
        Originally posted by telecastg View Post
        Hello and welcome to EspoCRM and to the forum.

        You can accomplish your objective by using the dynamic handler class. https://docs.espocrm.com/development/dynamic-handler/

        With that class you can manipulate the front end far beyond the GUI dynamic logic feature.
        Thanks a lot! This worked like a charm!!

        Only question I have is how to return an error on screen? This is my code so far:

        Code:
        define('custom:account-dynamic-handler', ['dynamic-handler'], function (Dep) {
        return Dep.extend({
        init: function () {
        this.controlFields();
        // invoke controlFields method every time assignedUserId gets changed
        this.recordView.listenTo(
        this.model, 'change:estado', this.controlFields.bind(this)
        );
        },
        controlFields: function () {
        let errors = [];
        if (this.model.get('categoria') == "0") {
        errors.push("Debe ingresar una categoria");
        } else {
        console.log("category set - do nothing")
        }
        if(errors.length) {
        }
        },
        });
        });

        Comment


        • espcrm
          espcrm commented
          Editing a comment
          Nice, a developer join. Welcome to EspoCRM, I can't help you; Coding is out of my league but looking forward to more code related post.

        • tothewine
          tothewine commented
          Editing a comment
          very informative

      • #5
        You're very welcome,

        try this:

        Code:
        this.recordView.notify('Not valid', 'error');
        or you can always insert an
        Code:
        alert('blablabla');
        statement but that will interrupt the program execution

        Saludos :-)
        Last edited by telecastg; 10-02-2020, 04:42 PM.

        Comment


        • #6
          This is awesome! Thanks for all the help!. Hope in the future I can help to integrate some library like swal for notifications.

          Comment

          Working...
          X