Joomla form validation error is missing error message

I am trying to develop my first component for Joomla. I have installed Joomla 3 and everything is going well.

I want to add a form validation (client side) in the frontend where I have a submit form.

My code:

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// Add form validation
JHTML::_('behavior.formvalidation');
?>
<form class="form-validate" id="myForm" method="post">
   <input name="email" type="text" class="required validate-email" size="30" />
   <button type="submit" class="validate">Submit form</button>
</form>

      

The check runs, but shows no message error - just the field. HTML for error field:

<div id="system-message-container">
   <div id="system-message" class="alert alert-error">
      <h4 class="alert-heading"></h4>
      <div></div>
   </div>
</div>

      

So how do you add text to the validation? Do I need to create a language file for my component?

+3


source to share


3 answers


You need to change the submit button of the form as input

Try this -

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// Add form validation
JHTML::_('behavior.formvalidation');
?>
<form class="form-validate" id="myForm" method="post">
   <input name="email" type="text" class="required validate-email" size="30" />
  <input type="submit" name="submit" value="Submit" />
</form>

      

Update : - You can try this also -



<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// Add form validation
JHTML::_('behavior.formvalidation');
?>
<form name="adminForm" id="myForm" method="post" onsubmit="return submitbutton();">
   <input  id="email" name="email" type="text" class="required validate-email" size="30" />
   <button type="submit" class="validate">Submit form</button>
</form>
<script type="text/javascript">
/* Override joomla.javascript, as form-validation not work with ToolBar */
function submitbutton() {   
    var f = document.adminForm;
    if (document.formvalidator.isValid(f)) {
        document.adminForm.submit(); 
        return true;
    }
    else {
        var msg = new Array();
        msg.push('Invalid input, please verify again!');           
        if($('email').hasClass('invalid')){
            msg.push('<?php echo JText::_('Invalid Email')?>');
        }
        alert (msg.join('\n'));
        return false;
    }     
}
</script>

      

This will validate the form on the client side, but not on the server side. For more information check this - http://docs.joomla.org/Form_validation

+2


source


also make sure you add this code to index.php



<jdoc:include type="message" />

      

+1


source


I had this problem. The problem is you don't have a label associated with the form field, the Joomla validation code generates an error message from the label text.

If you don't want the shortcut to show, set it to hidden (if you are using bootstrap use a hidden class). You must add a for clause to the label. If you also give it an id, which is set to the input id + '-lbl', it will optimize the tag lookup speed.

<form class="form-validate" id="myForm" method="post">
    <label class="hidden" for="inputid" id="inputid-lbl">Form label</label>
    <input id="inputid" name="email" type="text" class="required validate-email" size="30" />
    <button type="submit" class="validate">Submit form</button>
</form>

      

0


source







All Articles