1

I need to add some form validation for telephone field on admin side for magento. I am looking for correct file to work on

There's already some validation for required fields on the admin sales_order_create I am trying to find the right file that I can work on for the page sales_order_create. I did some search but still I had no luck on my code base.

2 Answers 2

3

To add form validation, you'll have to create the file to work on, and hook it to the Magento layout.

Add JavaScript to Sales Order Create

To follow best practices, you'll have to create your own module if you don't have already. I'll start from bottom up, to evidentiate the core need:

Create a layout file

app/design/adminhtml/default/default/layout/mycustom/adminthml.xml

<?xml version="1.0"?>
<layout>
    <adminhtml_sales_order_create_index>
        <reference name="head">
            <action method="addJs">
                <script>mycustom/mycustom.js</script>
            </action>
        </reference>
    </adminhtml_sales_order_create_index>
</layout>

You can skip the mycustom folder if you'd like and name your js file as you want - but remember to change accordingly also in the next steps.

Now you need to reference the xml layout file you just created.

Create your module

Create app\code\local\Mycustom\Adminhtml\etc\config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mycustom_Adminhtml>
          <version>0.0.1</version>
        </Mycustom_Adminhtml>
    </modules>
    <adminhtml>
        <layout>
            <updates>
                <mycustom>
                    <!-- this will look for the XML file in the app/design/adminhtml/default/default/layout folder -->
                    <file>mycustom/adminhtml.xml</file>
                </mycustom>
            </updates>
        </layout>
    </adminhtml>
</config>

Again, you can also skip the folder and name the xml file as you want.

Now you need to reference your module. Create \app\etc\modules\Mycustom_Adminhtml.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mycustom_Adminhtml>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Adminhtml/>
                <Mage_Sales />
            </depends>
        </Mycustom_Adminhtml>
    </modules>
</config>

Now you can create your Javascript file \js\mycustom\mycustom.js and put your validation code in it. For JS form validations you can check this answers: Custom form Validation - Magento

What to be aware of/check:

  • Look into Configuration > Advanced > Advanced to see if your new module is listed and Enabled
  • Log files in /var/log folder
  • Typing errors
  • Root of mentioned paths is /web/ of your Magento installation folder

More detailed version here: Add JS to Magento Sales Order

1

You can enable template path hints for adminhtml (here is example), and then locate template file where the form fields are described. For frontend validation Magento uses CSS classes, which are telling what types of check to apply for the input fields. For telephone validation Magento has three types out-of-box:

  • 'validate-phoneStrict'

The error message for this will be Please enter a valid phone number. For example (123) 456-7890 or 123-456-7890.

  • 'validate-phoneLax'

The error message for this will be Please enter a valid phone number. For example (123) 456-7890 or 123-456-7890.

  • 'validate-fax'

The error message for this will be Please enter a valid fax number. For example (123) 456-7890 or 123-456-7890.

1
  • Good advice about validating phone fields, but the question also asks for correct file to work on. Enabling path hints will only give a hint on where he can hook. Commented Feb 14, 2015 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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