none
Validating data on screen doesn't working RRS feed

  • Question

  • Hi,

    maybe someone could help me with my problem. The validation fires and the invalid data is identified with the red box (I have to press twice the save button, not idea why - I guess for the asynchronous calls -). However, when the data is corrected to pass the rule the property error does not clear. The red box remains around the field. Any idea what would be causing it not to clear?

    myapp.AddEditSet.created = function (screen) {
    
        screen.taSet.addChangeListener("Descripcion",
        function (e) {
            var accion = screen.Accion;
    
            var contentItem = screen.findContentItem("Descripcion");
            if (screen.taSet.Descripcion === "" || screen.taSet.Descripcion == null) {
    
                contentItem.validationResults = [
                new msls.ValidationResult(screen.taSet.details.properties.Descripcion, "This field is required.")];
            }
        });
    }

    Monday, July 7, 2014 12:18 PM

All replies

  • Uriel, you need to check if validation errors persist, and clear them. See the code sample below using 2 controls:

    myapp.MiembroHostNuevoEditar.created = function (screen) {
        // Establecer el nombre de la pantalla dinámicamente, según se edita o se agrega
        if (screen.MiembroHost.Id != null && screen.MiembroHost.Id != undefined) {
            screen.details.displayName = "Editar Host";
        }
        else {
            screen.details.displayName = "Nuevo Host";
        };
        //Validar Vigente y FechaBaja
        function validar() {
            if (!screen.MiembroHost.Vigente && screen.MiembroHost.FechaBaja == null) {
                screen.findContentItem("MiembroHost_Vigente").validationResults = [
                    new msls.ValidationResult(screen.MiembroHost.Vigente, "Si el host no está vigente, debe especificar la fecha de baja")
                ];
            }
            else if (screen.MiembroHost.Vigente && screen.MiembroHost.FechaBaja != null) {
                screen.findContentItem("MiembroHost_FechaBaja").validationResults = [
                    new msls.ValidationResult(screen.MiembroHost.FechaBaja, "Si el host está vigente, no debe especificar la fecha de baja")
                ];
            }
                //Si se corrigieron los errores, limpiar los mensajes
            else if (screen.findContentItem("MiembroHost_Vigente").hasValidationErrors || screen.findContentItem("MiembroHost_FechaBaja").hasValidationErrors) {
                screen.findContentItem("MiembroHost_Vigente").validationResults = [];
                screen.findContentItem("MiembroHost_FechaBaja").validationResults = [];
            };
        }
        //Agregar change listener al control, que dispare la función de validación
        screen.MiembroHost.addChangeListener("Vigente", validar);
        screen.MiembroHost.addChangeListener("FechaBaja", validar);
    };


    Monday, July 7, 2014 5:10 PM
  • Hi Nicolas, thanks for your answer. I believe you speak Spanish so I will speak it too. If not, I will re write this message in English.

    El código funciona bien, el tema es que no siempre me marca en rojo el campo. Es decir, parece haber problemas asincronicos que si aprieto el botón de guardar no salta nunca el evento que establecí en el changelistener. Ahora bien, si por ej, dentro de un textbox que tiene la validación, escribo una letra el changelistener se da cuenta y ejecuta la función y recién ahí, si borro el contenido me indica el campo en rojo. Sabes como solucionar el tema? 

    Desde ya, muchas gracias!

    Tuesday, July 8, 2014 3:35 PM
  • Uriel, es cierto, mi lengua nativa es el español. Estoy en Buenos Aires, y me interesa relacionarme con gente que desarrolla en LS. Me encontrás en @nlopedebarrios, y LinkedIn. Volviendo al tema: además de la validación del lado del cliente, deberías validar del lado del servidor. Aunque no haya validación cliente, la del servidor salta siempre, con un message box. Funciona en ambos clientes.

    El código que te pasé funciona como se espera en un Add/Edit Dialog corriente. Sin cargar nada y presionando Guardar, saltan los errores. Hay validaciones implícitas para los campos requeridos, por ejemplo. Otra cosa útil es establecer valores por defecto al crear la pantalla, y usar la propiedad placeholder en los campos de texto, para mejorar la UX. Si querés postéa el código que tiene el comportamiento descrito y lo vemos.

    Sorry about that, now, for the rest of the world:

    The code above works as expected.

    You should use server validation as well, even if no custom client validation is coded, a message box will show up, preventing the user for saving with errors. Other things to consider are setting default values when the entity is created and using the placeholder property for text fields, improving the UX.


    Tuesday, July 8, 2014 6:16 PM
  • Well, Im gonna talk in english so everybody can understand it. First of all, thanks for your reply. I do know about the server validation, but it throws a message box instead of the red square and I need to show the red square. So, for example, I have the following code

    myapp.AddEditAccionesAtribItem.created = function (screen) {
        //Validar Modulo
        function validarModulo() {
            if (screen.AccionesAtrib.Modulo == null) {
                screen.findContentItem("Modulo").validationResults = [
                    new msls.ValidationResult(screen.AccionesAtrib.Modulo, "This field is required.")
                ];
            }
            //Si se corrigieron los errores, limpiar los mensajes
            else if (screen.findContentItem("Modulo").hasValidationErrors) {
                screen.findContentItem("Modulo").validationResults = [];
            };
        }
        screen.AccionesAtrib.addChangeListener("Modulo", validarModulo);
    
        //Validar NombreColumna
        function validarNombreColumna() {
            if (screen.AccionesAtrib.ta_String == null) {
                screen.findContentItem("ta_String").validationResults = [
                    new msls.ValidationResult(screen.AccionesAtrib.ta_String1, "This field is required.")
                ];
            }
            //Si se corrigieron los errores, limpiar los mensajes
            else if (screen.findContentItem("ta_String").hasValidationErrors) {
                screen.findContentItem("ta_String").validationResults = [];
            };
        }
        //Agregar change listener al control, que dispare la función de validación
        screen.AccionesAtrib.addChangeListener("ta_String", validarNombreColumna);
    };

    The ´Modulo´ validation works as expected, but ´NombreColumna´ not. If I click on save button, the "ta_string" control dont display the red square while the "Modulo" control does. 

    Any idea why?

    Thanks in advance.

    Thursday, July 10, 2014 12:29 PM
  • Hello again,

    Try adding a breakpoint and inspecting ta_String value. Maybe you should check for null or undefined. Is it a nullable property?

    myapp.AccionesAttribItem.created = function (entity) {
        // Set defaults
        entity.ta_String = null; //or some value, you can even use a query to retrieve it
    
    };

    If it is nullable, you might want to try assigning null on entity created (in the Entity designer, go to the HTML Client tab). Or, better off, a default value that the user might be forced to change or accept.

    HTH,

    Nicolás.


    Thursday, July 10, 2014 1:28 PM
  • Sorry for my bad explanation of the situation I have. Here it goes again: When Im trying to add a new record, I press the add button and it open the AddEdit screen. Because LS by default doesnt check validations on lookup fields, I must set them (which I did with the code posted before). 

    Well, if I press the save button just after the AddEdit screen appears, the "NombreColumna" validation dont work (the "Modulo" validation does). However, if I click on the "NombreColumna" (its a lookup field) and not choose anything, the validation seems to work (the red square appers) and if I put a breakpoint at that line, it stops the execution. 

    If Im not clear enough describing the problem, please tell me and Im gonna be more explicit.

    Thanks.

    Thursday, July 10, 2014 1:52 PM
  • Uriel,

    Your explanation was clear, except for the part that one of the fields is a lookup field. I believe it is not supported, yet. Please refer to this post, where one of the LS Team members suggests the same as I do: setting a default value.

    good luck.

    Thursday, July 10, 2014 2:39 PM
  • Nicolas, a lookup field is the following (its like a combo box). Notice that after clicking the save button, only Modulo appears with the red square, and not the NombreColumna. They are both lookups, so I have no idea why its not working.

    myapp.AddEditAccionesAtribItem.created = function (screen) {
        //Validar Modulo
        function validarModulo() {
            if (screen.AccionesAtrib.Modulo == null) {
                screen.findContentItem("Modulo").validationResults = [
                    new msls.ValidationResult(screen.AccionesAtrib.Modulo, "This field is required.")
                ];
            }
            //Si se corrigieron los errores, limpiar los mensajes
            else if (screen.findContentItem("Modulo").hasValidationErrors) {
                screen.findContentItem("Modulo").validationResults = [];
            };
        }
        screen.AccionesAtrib.addChangeListener("Modulo", validarModulo);
    
        //Validar NombreColumna
        function validarNombreColumna() {
            if (screen.AccionesAtrib.ta_String == null) {
                screen.findContentItem("ta_String").validationResults = [
                    new msls.ValidationResult(screen.AccionesAtrib.ta_String1, "This field is required.")
                ];
            }
            //Si se corrigieron los errores, limpiar los mensajes
            else if (screen.findContentItem("ta_String").hasValidationErrors) {
                screen.findContentItem("ta_String").validationResults = [];
            };
        }
        //Agregar change listener al control, que dispare la función de validación
        screen.AccionesAtrib.addChangeListener("ta_String", validarNombreColumna);
    };

    Thursday, July 10, 2014 3:18 PM
  • I didn't mean that I don't know what a lookup field is, I meant you didn't mentioned that before. Why is working for Modulo and not for NombreColumna, I don't know.

    I had the same issue in one of my projects, and used the workaround posted above.

    Thursday, July 10, 2014 5:43 PM