The Joomla! Forum ™



Forum rules


Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
PostPosted: Tue Jul 13, 2010 8:07 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Jul 01, 2010 2:27 pm
Posts: 21
Greetings folks,

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

Code:
<?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


Top
 Profile  
 
PostPosted: Fri Jul 16, 2010 10:59 am 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Jul 13, 2010 1:20 pm
Posts: 15
Location: Sofia, Bulgaria
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:
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


Top
 Profile  
 
PostPosted: Tue Jul 20, 2010 10:42 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Jul 01, 2010 2:27 pm
Posts: 21
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:


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



Top
 Profile  
 
PostPosted: Wed Jul 21, 2010 8:37 am 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Jul 13, 2010 1:20 pm
Posts: 15
Location: Sofia, Bulgaria
You are welcome and thanks for the tip too.
Will keep that in mind. :)

_________________
http://www.jompoint.com/ - JomPoint - Joomla Customizations


Top
 Profile  
 
PostPosted: Thu Aug 26, 2010 1:40 am 
Joomla! Intern
Joomla! Intern

Joined: Sun Dec 18, 2005 7:26 pm
Posts: 52
Thanks for the tip :) !


Top
 Profile  
 
PostPosted: Wed Dec 29, 2010 11:05 am 
User avatar
Joomla! Intern
Joomla! Intern

Joined: Fri Jun 26, 2009 7:42 am
Posts: 94
Location: Ahmedabad->Gujarat->India
Hi Guys,

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

viewtopic.php?p=2356986#p2356986

_________________
Neerav Dobaria
Freelance PHP/MySQL Developer
http://www.techna2.com/blog


Top
 Profile  
 
PostPosted: Tue Jan 18, 2011 2:49 pm 
Joomla! Guru
Joomla! Guru

Joined: Thu Mar 15, 2007 6:23 pm
Posts: 725
Thanks for the tip

_________________
เว็บสอน php, รับพัฒนา Extension http://www.mindphp.com
Free Joomla Extension: http://www.mjform.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 



Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group