Page 1 of 1

mootools form validation does not work properly

Posted: Tue Jul 13, 2010 8:07 pm
by Mrazi
Greetings folks,

I'm facing the problem with form validation. I wrote following code into my template:

Code: Select all

<?php

defined('_JEXEC') or die('Restricted access');

JHTMLBehavior::formvalidation();
?>

<script type="text/javascript"  language="javascript">
function myValidate(f){
    if(document.formvalidator.isValid(f)){
        return true;
    }else{
        alert('Fill out input name.')
        return false;
    }
    return false;
}
</script>

<form action="index.php" method="post" name="adminForm" id="adminForm" class="form-validate" onsubmit="return myValidate(this);">
....
<input class="required text_area" type="text" name="inputName" id="albumName" size="32" maxlength="250" value="" />
...

I don't use html submit buttons. I used JToolBarHelper::save() and JToolBarHelper::cancel() instead in my view code.

Now everything works correctly. I do not fill out inputName input an alert message occurs, but despite that, the form is submited after I click ok.

Can anybody tell me why?
Thanx

Re: mootools form validation does not work properly

Posted: Fri Jul 16, 2010 10:59 am
by jompoint
I am not sure whether you have solved this problem already, but if you haven't - this might help.
Are you sure that myValidate haven't been called in the submit button as well - or in some way more than once.
Usually when I have such a problem I set many more alerts in the code, so I can make something like a simple traceroute. What I would do is write the following JavaScript code:

Code: Select all

function myValidate(f){
    alert("A");
    if(document.formvalidator.isValid(f)){
	alert("B");
        return true;
    }else{
	alert("C");
        alert('Fill out input name.')
        return false;
    }
    alert("D");
    return false;
}
Hope that would help.

Re: mootools form validation does not work properly

Posted: Tue Jul 20, 2010 10:42 pm
by Mrazi
Hi thanx for your answer. Yes I've already solved my problem. Javascript code was alright. I found somewhere here a post where some guy wrote that he faced the same problem like me. The problem occurs in the back-end part of my component. I'm using JToolBar to create submit buttons like "Save", "Add", "Edit" etc. And this is the core of the problem. These buttons are written in javascript (meaning their functionality). After clicking on them my validation js code is invoked, because those JToolBar buttons call it and after it it calls submit of the form regardless the returned value of the onsubmit event. So if you want to use the toolbar submit buttons you have to create your own like this:

Code: Select all



        $button = '<a class="toolbar" class="button validate" type="submit" onclick="javascript:
                adminForm.task.value=\'save\'; return myValidate(adminForm);" href="#">
                <span title="Save" class="icon-32-save"></span>Save</a>';

        $bar = & JToolBar::getInstance('toolbar');
        $bar->appendButton( 'Custom', $button);


Re: mootools form validation does not work properly

Posted: Wed Jul 21, 2010 8:37 am
by jompoint
You are welcome and thanks for the tip too.
Will keep that in mind. :)

Re: mootools form validation does not work properly

Posted: Thu Aug 26, 2010 1:40 am
by alex_funky_dj
Thanks for the tip :) !

Re: mootools form validation does not work properly

Posted: Wed Dec 29, 2010 11:05 am
by Neerav
Hi Guys,

Here is one good solution, working for me at least,

http://forum.joomla.org/viewtopic.php?p ... 6#p2356986

Re: mootools form validation does not work properly

Posted: Tue Jan 18, 2011 2:49 pm
by mindphp
Thanks for the tip