1

I am trying to add custom form validation. This is what i am using.

<?php
    /* include file in magento scope */
    include_once 'app/Mage.php';
    Mage::app();
    $basePath= Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
    $media = Mage::getBaseDir('media');

    if($_POST['model_name']!='')
    {
        echo "form submitted thanks.";die;
    }
?>

<link media="all" href="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN).'adminhtml/default/default/boxes.css'; ?>" type="text/css" rel="stylesheet">
<link media="all" href="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN).'adminhtml/default/default/reset.css';?>" type="text/css" rel="stylesheet">

<script type="text/javascript" src="<?php echo $basePath?>js/prototype/prototype.js"></script>
<script type="text/javascript" src="<?php echo $basePath?>js/prototype/validation.js"></script>
<script type="text/javascript" src="<?php echo $basePath?>js/varien/js.js"></script>
<script type="text/javascript" src="<?php echo $basePath?>js/varien/form.js"></script>


    <div class="entry-edit">  
    <div class="entry-edit-head">
        <h4>Model Information</h4>
    </div>
    <div id="product_options_container">
        <div id="product_options_container_top"></div>
        <div id="product_options_container">
            <div id="product_options_container_top"></div>
            <div class="hor-scroll">
                <form enctype="multipart/form-data" method="post" id="model_add_two" name="model_add2">

                <table cellspacing="0" class="form-list">
                <tbody>
                <tr>
                    <td class="label"><label for="fonts_f_name">Model Name<span class="required">*</span></label></td>
                    <td class="value">
                        <input type="text"  name="model_name" class="input-text required-entry" id="model_name">
                    </td>
                </tr>              

                <tr>
                    <td class="label"><label for="fonts_f_name">Status<span class="required">*</span></label></td>
                    <td class="value">
                        <select class="required-entry" id="status" name="status">
                            <option selected="selected" value="">Select Status</option>
                            <option value="1">Enable</option> 
                            <option value="0">Disable</option>                         
                        </select>  
                    </td>
                 </tr>

                <tr>
                    <td class="label"><label for="short_price"></label></td>
                    <td class="value">
                <input name="add_button" id="add_button" type="submit" class="scalable save"><span>Save</span></button>  </td>
            </tr>
                </tbody>
            </table>
            </form>
          <div id="formSuccess" style="display:none;">&nbsp;</div>
            </div>
          <!-- End code-->
            <div>
            </div>
        </div>
    </div>
</div>



<script type="text/javascript">
//<![CDATA[
    var formId = 'model_add_two';
    var myForm = new VarienForm(formId, true);
    var postUrl = '<?php echo $_SERVER['PHP_SELF']; ?>';
    function doAjax() {
        if (myForm.validator.validate()) {
            new Ajax.Updater(
                { success:'formSuccess' }, postUrl, {
                    method:'post',
                    asynchronous:true,
                    evalScripts:false,
                    onComplete:function(request, json) {
                        Element.hide(formId);
                        Element.show('formSuccess');
                    },
                    onLoading:function(request, json){
                        Element.show('formLoader');
                    },
                    parameters: $(formId).serialize(true),
                }
            );
        }
    }

    new Event.observe(formId, 'submit', function(e){
        e.stop();
        doAjax();
    });
//]]>
</script>

enter image description here This is what i get after these steps. 1)click on save button - validation works. 2)fill all fields. and click on save button - form doesn't get submit. What is the issue here.

7
  • 1
    "When i click on Save button without adding any data to fields, validation works but form doesn't submit.". What does this mean? If you don't add any data of course the form is not going to submit. Because it does not pass the validation. – Marius Nov 7 '14 at 14:01
  • I am pretty sure you're also required to add the hidden 'formkey' for security validation. – tecjam Nov 7 '14 at 14:06
  • Please explain "When i directly add all fields and click on save button form gets submitted. What is the issue here?" – Renon Stewart Nov 7 '14 at 15:11
  • Where should the form be submitted to? You don't seem to have an action attribute in the form. – Melvyn Nov 8 '14 at 9:31
  • i am using ajax.updater to submit the form as i have this file on root directory of magento. – HungryDB Nov 8 '14 at 9:32
1

Couldn't find any solution for this. I had to use simple jQuery validation for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.