Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

267
Views
Yii2 valida un ActiveForm con javascript

Tengo un formulario activo en funcionamiento, que se puede enviar y validar a través de Yii PHP. Sin embargo, me gustaría determinar si el formulario es válido al hacer clic en el botón Next .

Puedo pasar mensajes de error al usuario a través de esto:

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

Pero esta función no devuelve nada; No sé si realmente hay errores o no. Intenté esto:

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

Pero esto no funciona; los errores se cuentan antes de que se actualice la interfaz de usuario. Si presiono el botón nuevamente, una segunda vez, entonces error_count es lo que esperaría.

Esto no parece hacer nada:

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

También probé esto:

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

Pero esto solo se activa después del hecho, así que no estoy seguro de qué hacer.

Cualquier consejo sería apreciado.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Si necesita reaccionar al botón, simplemente necesita combinar ambos eventos:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!