mootools form validation does not work properly

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Locked
Mrazi
Joomla! Apprentice
Joomla! Apprentice
Posts: 21
Joined: Thu Jul 01, 2010 2:27 pm

mootools form validation does not work properly

Post by Mrazi » Tue Jul 13, 2010 8:07 pm

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

User avatar
jompoint
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Tue Jul 13, 2010 1:20 pm
Location: Sofia, Bulgaria
Contact:

Re: mootools form validation does not work properly

Post by jompoint » Fri Jul 16, 2010 10:59 am

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.
http://www.jompoint.com/ - JomPoint - Joomla Customizations

Mrazi
Joomla! Apprentice
Joomla! Apprentice
Posts: 21
Joined: Thu Jul 01, 2010 2:27 pm

Re: mootools form validation does not work properly

Post by Mrazi » Tue Jul 20, 2010 10:42 pm

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);


User avatar
jompoint
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Tue Jul 13, 2010 1:20 pm
Location: Sofia, Bulgaria
Contact:

Re: mootools form validation does not work properly

Post by jompoint » Wed Jul 21, 2010 8:37 am

You are welcome and thanks for the tip too.
Will keep that in mind. :)
http://www.jompoint.com/ - JomPoint - Joomla Customizations

alex_funky_dj
Joomla! Intern
Joomla! Intern
Posts: 52
Joined: Sun Dec 18, 2005 7:26 pm

Re: mootools form validation does not work properly

Post by alex_funky_dj » Thu Aug 26, 2010 1:40 am

Thanks for the tip :) !

User avatar
Neerav
Joomla! Intern
Joomla! Intern
Posts: 94
Joined: Fri Jun 26, 2009 7:42 am
Location: Ahmedabad->Gujarat->India
Contact:

Re: mootools form validation does not work properly

Post by Neerav » Wed Dec 29, 2010 11:05 am

Hi Guys,

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

http://forum.joomla.org/viewtopic.php?p ... 6#p2356986
Neerav Dobaria
Freelance PHP/MySQL Developer
http://www.techna2.com/blog

mindphp
Joomla! Guru
Joomla! Guru
Posts: 828
Joined: Thu Mar 15, 2007 6:23 pm
Contact:

Re: mootools form validation does not work properly

Post by mindphp » Tue Jan 18, 2011 2:49 pm

Thanks for the tip


Locked

Return to “Joomla! 1.5 Coding”