Statistics
| Branch: | Tag: | Revision:

root / cloudcms / static / cloudcms / js / form-errors.js @ 148c25ed

History | View | Annotate | Download (2.3 kB)

1
(function($){
2
  
3
  $.fn.formCheckBoxes = function(options) {
4
    
5
    return this.each(function() {
6
      var $this = $(this);
7
      var el = $('<span class="checkbox-widget" />');
8
      
9
      if ($this.prev().length > 0) {
10
        var lbl = $this.prev()[0];
11
        if (lbl.nodeName == "LABEL" || lbl.nodeName == "label") {
12
            $(lbl).addClass("checkbox-label");
13

    
14
            $(lbl).click(function(){
15
                el.toggleClass("checked");
16
            })
17
        }
18
      }
19
      $this.hide();
20
      
21
      if ($this.is("checked")) {
22
        el.addClass("checked");  
23
      }
24

    
25
      el.click(function() {
26
        el.toggleClass("checked");
27
        $this.attr('checked', el.hasClass("checked"));
28
      })
29

    
30
      $this.after(el);
31
    });
32

    
33

    
34
  }
35

    
36
  $.fn.formErrors = function(options) {  
37
    return this.each(function() {
38
        var $this = $(this);
39

    
40
        // does the field has any errors ?
41
        var errors = $this.find(".errorlist");
42
        if (errors.length == 0) {
43
            return;
44
        }
45
        
46
        // create the custom error message block
47
        // and copy the contents of the original
48
        // error list
49
        var el = $('<div class="form-error" />');
50
        errors.find("li").each(function(){
51
            el.html(el.html() + $(this).text() + "<br />");
52
        })
53
        
54
        var formel = $this.find("input, select");
55
        var lbl = $this.find("label");
56
        var form = $this.closest("form");
57

    
58

    
59
        // append element on form row 
60
        // and apply the appropriate styles
61
        formel.closest(".form-row").append(el);
62
        errors.remove();
63
        var left = formel.width();
64
        var top = formel.height();
65
        var marginleft = lbl.width();
66
        
67
        // identify the position
68
        // forms with innerlbales class
69
        // display the label within the input fields
70
        if ($(form).hasClass("innerlabels")) {
71
            marginleft = 0;
72
        }
73
        
74
        var styles = {
75
            left: left + "px", 
76
            top: top + "px", 
77
            width: formel.outerWidth() - 10,
78
            marginLeft: marginleft,
79
            marginBottom: 5
80
        }
81
        
82
        if (formel.attr("type") != "checkbox") {
83
            el.css(styles);
84
        } else {
85
            el.css("margin-top", "5px");
86
        }
87
    });
88

    
89
  };
90
})( jQuery );