Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

262
Vistas
Yii2 validate an ActiveForm with javascript

I have a working Active Form, which can be submitted, and validated via Yii PHP. However, I would like to determine if the form is valid, when clicking a Next button.

I can pass error messages to the user via this:

$("#form").yiiActiveForm("validate", true);

But this function doesn't return anything; I don't know if there are indeed any errors or not. I tried this:

$error_count = document.getElementsByClassName("help-block").length

but this does not work; the errors are counted before the UI has updated. If I press the button again, a second time, then error_count is what I'd expect.

This doesn't seem to do anything:

$("#form").yiiActiveForm("validate");

I also tried this:

$('#form').on('afterValidate', function (event, messages, errorAttributes) {}

But this is only triggered after the fact so I'm not sure what to do.

Any advice would be appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you need to react to button you simply need to combine both events:

let isNextClicked = false;

$('.next-btn').on('click', function(e) {
    e.preventDefault();
    isNextClicked = true;
    $("#form").yiiActiveForm("validate", true);
});

$('#form').on('afterValidate', function(event, messages, errorAttributes) {
    if (!isNextClicked) {
        //validation wasn't triggered by clicking next
        return;
    }

    // reset the state
    isNextClicked = false;

    if (errorAttributes.length > 0) {
        //there are errors so we won't let user to continue
        //we can also show some alert() here
        return;
    }
    
    // ... code to show the next tab of form ...
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda