1 /**
2  * Skadi.d Web Framework
3  * Form Component
4 
5  * Authors: Faianca
6  * Copyright: Copyright (c) 2015 Faianca
7  * License: MIT License, see LICENSE
8  */
9 module skadi.components.form.element;
10 
11 import skadi.components.form.elementInterface;
12 import skadi.components.form.form;
13 import skadi.components.validation.validatorInterface;
14 
15 abstract class Element : ElementInterface
16 {
17 
18 protected:
19 
20 	Form _form;
21 	string _name;
22 	string _value;
23 	string _label;
24 	string[string] _attributes;
25 	string[] _filters;
26 	ValidatorInterface[] _validators;
27 
28 public:
29 
30 	/**
31 	 * @param string name
32 	 * @param array attributes
33 	 */
34 	this(string name, string[string] attributes = null)
35 	{
36 		this._name = name;
37 	}
38 
39 	/**
40 	 * Sets the parent form to the element
41 	 */
42 	ElementInterface setForm(Form form)
43 	{
44 		this._form = form;
45 		return this;
46 	}
47 
48 	/**
49 	 * Returns the parent form to the element
50 	 */
51 	Form getForm()
52 	{
53 		return this._form;
54 	}
55 
56 	/**
57 	 * Sets the element name
58 	 */
59 	ElementInterface setName(string name)
60 	{
61 		this._name = name;
62 		return this;
63 	}
64 
65 	/**
66 	 * Returns the element name
67 	 */
68 	string getName()
69 	{
70 		return this._name;
71 	}
72 
73 	/**
74 	 * Adds a filter to current list of filters
75 	 */
76 	ElementInterface addFilter(string filter)
77 	{
78 		this._filters ~= filter;
79 		return this;
80 	}
81 
82 	/**
83 	 * Returns the element filters
84 	 *
85 	 * @return string[]
86 	 */
87 	string[] getFilters()
88 	{
89 		return this._filters;
90 	}
91 
92 	/**
93 	 * Adds a validator to the element
94 	 */
95 	ElementInterface addValidator(ValidatorInterface validator)
96 	{
97 		this._validators ~= validator;
98 		return this;
99 	}
100 
101 	/**
102 	 * Returns the validators registered for the element
103 	 */
104 	ValidatorInterface[] getValidators()
105 	{
106 		return this._validators;
107 	}
108 
109 	/**
110 	 * Sets the element label
111 	 */
112 	ElementInterface setLabel(string label)
113 	{
114 		this._label = label;
115 		return this;
116 	}
117 
118 	/**
119 	 * Returns the element label
120 	 */
121 	string getLabel()
122 	{
123 		return this._label;
124 	}
125 
126 	/**
127 	 * Sets a default value in case the form does not use an entity
128 	 * or there is no value available for the element in _POST
129 	 */
130 	ElementInterface setDefault(string value)
131 	{
132 		this._value = value;
133 		return this;
134 	}
135 
136 	/**
137 	 * Returns the default value assigned to the element
138 	 */
139 	string getDefault()
140 	{
141 		return this._value;
142 	}
143 
144 	/**
145 	 * Magic method __toString renders the widget without atttributes
146 	 */
147 	override string toString()
148 	{
149 		return "teste :D";
150 	}
151 }