0

Hey I tried to outsource my error messages into a xml-File, so changing them or adding some new messages for new elements gets easier.

My xml-file looks like this:

   <messages>
      <element>
        <name>name</name>
        <pflicht>Text bei Pflicht.</pflicht>
        <format>email</format>
        <formattext>Text bei falschem Format</formattext>
      </element>
      <element>
        <name>postleitzahl</name>
        <pflicht>Required</pflicht>
        <format>plz</format>
        <formattext>Bitte Postleitzahl angeben.</formattext>
      </element>
   </messages>

And the resulting function should be written for each element-tag and look like this: For example:

  jQuery(".validation").rules("add",{
        messages:{
            name :{
                required: "Text bei Pflicht",
                email: "Text bei falschem Format"
            }
            postleitzahl:{
                required: "Required.",
                plz: "Bitte Postleitzahl angeben."
            }
  });

How can I achieve that?

Thanks in advance.

1 Answer 1

1
var myXML;
    $.ajax({
        type: "GET",
        url: "message.xml",
        dataType: "xml",
        success: function(xml) {
            alert($(xml).find('element'));
            }
        });

    var nm = returnValue("name");
    var frm = returnValue("format");
    jQuery(".validation").rules("add",{
        messages:{
           nm:{
               required: returnValue("pflicht"),
               frm : returnValue("formattext")
            }
        }
    })

    function returnValue(_tag){
       return myXML.find(_tag).text();
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I don't only want to read the message texts from the xml-file but also the name and the format.
I don't get it to work. Firebug says: missing : after property id returnValue("name"):{
Found another solution. The assignation on the left side of the colon doesn't work. I had to assign the rules for each textfield separately. Then I have the name to add the rules and for the format I solved the problem with a switch case. But your answer got me in the right direction. Thanks a lot.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.