How to call the jquery validation custom module prepare form function field on magento

Posted: November 17, 2011 in Magento

Example:

app\code\local\Cataloggallery\Gallerycheck\Block\Template\Edit\Tab\General.php

This is my custom module file folder structure on General Tab file.

$fieldset->addField(’Sku’, ‘text’, array(
‘name’ => ‘Sku’,
‘class’=>’checkskuval’,
‘label’ => Mage::helper(’Gallerycheck’)->__(’Sku’),
‘title’ => Mage::helper(’Gallerycheck’)->__(’Sku’),

‘required’ => true,

));

The above fieldset is my prepareform field.

I want to check the Sku value is already exist or not.

On that same file bottom of the file i have write the following Jquery script for validation purpose

$js_url= Mage::getBaseUrl(’js’);
?>
<script src=”<?php echo $js_url;?>jquery.js” type=”text/javascript\” ></script>
<script type=”text/javascript\”>
jQuery.noConflict();
(function($) {
$(function()
{
$(document).ready(function()
{

var urlth=’<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB); ?>galleryajax.php?’;
$(’.checkskuval’).blur(function()
{
//alert(“hai”);
var parametri=”skuvalue=”+jQuery(’#Gallery_Sku’).val();

jQuery.ajax({
type: “POST”,
url: urlth,
data: parametri,
dataType: ‘html’,
timeout: 1000,
async:false,
success: function(msg){
if(msg==’Please Enter Valid Sku’)
{
alert(msg);
//var error=’Please Enter Valid Sku’;
//$(’.validation-advice’).html(msg);
// document.getElementById(’Gallery_Sku’).value.select();
//document.getElementById(’Gallery_Sku’).focus();
return false;
}
else
{
//alert(msg);
return false;
}

}
});

});
});
});
})(jQuery);
</script>

The following script is galleryajax.php file script.

<?php
require_once(“app/Mage.php”);
Mage::app();
if($_REQUEST[‘skuvalue’]!=”)
{
$skuval= trim ($_REQUEST[‘skuvalue’]);

$product = Mage::getModel(‘catalog/product’)->loadByAttribute(‘sku’,$skuval);
/*echo “<pre>”;
print_r($product);
exit;
if($product==”)*/

/*$val=$product->getData();
echo “<pre>”;
print_r($val);
exit;*/
if($product==”)
{
echo “Please Enter Valid Sku”;
}
else
{
echo “1”;
}
exit;
}

 

?>

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s