Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Klasa JS] Walidator
ShaXbee
post
Post #1





Grupa: Zarejestrowani
Postów: 29
Pomógł: 0
Dołączył: 19.10.2004
Skąd: Opole

Ostrzeżenie: (0%)
-----


Klasa wymaga frameworku Prototype.

Na potrzeby projektu próbuję stworzyć tandem walidatorów - jeden po stronie serwera w php, drugi po stronie klienta w JS 'podpinany' o ile to tylko możliwe.

Deklaracja pól do walidacji i sposobu ich sprawdzania ma się znajdować jedynie po stronie serwera, do klienta wysyłana jako doklejony kod JSON.

Klasa będzie oczywiście rozwijana, to co w tej chwili prezentuję można potraktować jako pre-alpha (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

Sposób użycia w kodzie HTML:
  1. <ul id="MyFormErrors">
  2. </ul>
  3.  
  4. <form id="MyForm">
  5. <input name="field1" />
  6. <input name="field2" />
  7. </form>
  8.  
  9. <script language="javascript">
  10. new Validator('MyForm', 'MyFormErrors', {
  11. field1: {
  12. errorMsg: 'Podaj prawidłową wartość liczbową (10-100)',
  13. validator:
  14. name: 'checkInt',
  15. params: {
  16. min: 10,
  17. max: 60
  18. }
  19. }
  20. },
  21. field2: {
  22. errorMsg: 'Podaj prawidlowy kod pocztowy',
  23. validator: {
  24. name: 'preg',
  25. params: {
  26. regexp: /^[0-9]{2}-[0-9]{3}$/
  27. }
  28. }
  29. }
  30. });


Kod klasy Validator:
Kod
Validator = Class.create();

Validator.prototype = {

    initialize: function(form, errors, fields) {

        this.form = $(form);
        this.errors = $(errors);

        this.fields = fields;

        $H(fields).each(function(field) {

            this.attach(field.key);
            field.value.error = null;

        }.bind(this));

    },

    attach: function(name, event) {

        node = this.form.firstChild;
        while(node) {

            if(node.name == name)
                Event.observe(node, 'change', this.validate.bind(this), true);

            node = node.nextSibling;

        }

    },

    validate: function(e) {

        obj = Event.element(e);
        field = this.fields[obj.name];

        if(!this[field.validator.name](obj.value, field.validator.params)) {

            if(field.error == null) {

                obj.addClassName('error');

                li = document.createElement('li');
                li.appendChild(document.createTextNode(field.errorMsg));

                this.errors.appendChild(li);

                field.error = li;

            }

        } else if(field.error != null) {

            obj.removeClassName('error');

            field.error.parentNode.removeChild(field.error);
            field.error = null;

        }

        return true;

    },

    preg: function(field, data) {

        return (field.match(data.regexp) != null);

    },

    checkInt: function(field, data) {

        return (!isNaN(parseInt(field)) && (data.min != undefined || field >= data.min) && (data.max != undefined || field <= data.max));

    },

    checkFloat: function(field, data) {

        return (!isNaN(parseInt(field)) && (data.min != undefined || field >= data.min) && (data.max != undefined || field <= data.max));

    }

}
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
nospor
post
Post #2





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Cytat
PS2. Skrypt przestał działać IE ale to tylko sprawa czasu
a pod operą testowales?
Cytat
JavaScript - http://shaxbee.superhost.pl/demo/admin.htm...&field2=asd
Inline script thread
Error:
name: TypeError
message: Statement on line 13: Type mismatch (usually a non-object value used where an object is required)
Backtrace:
Line 13 of linked script http://shaxbee.superhost.pl/demo/Common/JS...idator.class.js
this.attach(field);
Line 71 of linked script http://shaxbee.superhost.pl/demo/Common/JS/prototype.js
return __method.apply(object, args.concat($A(arguments)));
Line 299 of linked script http://shaxbee.superhost.pl/demo/Common/JS/prototype.js
iterator(value, index++);
Line 584 of linked script http://shaxbee.superhost.pl/demo/Common/JS/prototype.js
iterator(pair);
Line 303 of linked script http://shaxbee.superhost.pl/demo/Common/JS/prototype.js
this._each((function (value)
{
try
{
iterator(value, index++);
}
catch (e)
{
if (e != $continue)
throw e;
}
}
));
Line 16 of linked script http://shaxbee.superhost.pl/demo/Common/JS...idator.class.js
this.fields.each((function (field)
{
this.attach(field);
field.value.error = null;
}
).bind(this));
Line 23 of linked script http://shaxbee.superhost.pl/demo/Common/JS/prototype.js
this.initialize.apply(this, arguments);
Line 11 of inline#1 script in http://shaxbee.superhost.pl/demo/admin.htm...&field2=asd
validator = new Validator("testForm", {field1 : {errorMsg : "Nieprawidlowy kod pocztowy", validator : {name : "preg", params : {regexp : /^[0-9]{2}-[0-9]{3}$/}}}, field2 : {errorMsg : "Trinity said: You are NOT handy", validator : {name : "preg", params : {regexp : /^keymaker$/}}}});

i klops. Opera 9.02
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 5.10.2025 - 11:16