when "bool"\r
check_box 'custom_value', 'value', :name => field_name, :id => field_id\r
when "list"\r
- select 'custom_value', 'value', custom_field.possible_values.split('|'), { :include_blank => true }, :name => field_name, :id => field_id\r
+ select 'custom_value', 'value', custom_field.possible_values, { :include_blank => true }, :name => field_name, :id => field_id\r
end\r
end\r
\r
\r
class CustomField < ActiveRecord::Base\r
has_many :custom_values, :dependent => :delete_all\r
-\r
+ serialize :possible_values\r
+ \r
FIELD_FORMATS = { "string" => { :name => :label_string, :order => 1 },\r
"text" => { :name => :label_text, :order => 2 },\r
"int" => { :name => :label_integer, :order => 3 },\r
validates_uniqueness_of :name\r
validates_format_of :name, :with => /^[\w\s\'\-]*$/i\r
validates_inclusion_of :field_format, :in => FIELD_FORMATS.keys\r
- validates_presence_of :possible_values, :if => Proc.new { |field| field.field_format == "list" }\r
+\r
+ def initialize(attributes = nil)\r
+ super\r
+ self.possible_values ||= []\r
+ end\r
+ \r
+ def before_validation\r
+ # remove empty values\r
+ self.possible_values = self.possible_values.collect{|v| v unless v.empty?}.compact\r
+ end\r
+ \r
+ def validate\r
+ if self.field_format == "list"\r
+ errors.add(:possible_values, :activerecord_error_blank) if self.possible_values.nil? || self.possible_values.empty?\r
+ errors.add(:possible_values, :activerecord_error_invalid) unless self.possible_values.is_a? Array\r
+ end\r
+ end\r
\r
# to move in project_custom_field\r
def self.for_all\r
<%= error_messages_for 'custom_field' %>
-<script>
+<script type="text/javascript">
+//<![CDATA[
function toggle_custom_field_format() {
format = $("custom_field_field_format");
p_length = $("custom_field_min_length");
case "list":
Element.hide(p_length.parentNode);
Element.hide(p_regexp.parentNode);
- Element.show(p_values.parentNode);
+ Element.show(p_values);
break;
case "int":
case "string":
case "text":
Element.show(p_length.parentNode);
Element.show(p_regexp.parentNode);
- Element.hide(p_values.parentNode);
+ Element.hide(p_values);
break;
case "date":
case "bool":
Element.hide(p_length.parentNode);
Element.hide(p_regexp.parentNode);
- Element.hide(p_values.parentNode);
+ Element.hide(p_values);
break;
default:
Element.show(p_length.parentNode);
Element.show(p_regexp.parentNode);
- Element.show(p_values.parentNode);
+ Element.show(p_values);
break;
}
}
+
+function addValueField() {
+ var f = $$('p#custom_field_possible_values span');
+ p = document.getElementById("custom_field_possible_values");
+ var v = f[0].cloneNode(true);
+ v.childNodes[0].value = "";
+ p.appendChild(v);
+}
+
+function deleteValueField(e) {
+ var f = $$('p#custom_field_possible_values span');
+ if (f.length == 1) {
+ e.parentNode.childNodes[0].value = "";
+ } else {
+ Element.remove(e.parentNode);
+ }
+}
+
+//]]>
</script>
<!--[form:custom_field]-->
<%= f.text_field :min_length, :size => 5, :no_label => true %> -
<%= f.text_field :max_length, :size => 5, :no_label => true %><br>(<%=l(:text_min_max_length_info)%>)</p>
<p><%= f.text_field :regexp, :size => 50 %><br>(<%=l(:text_regexp_info)%>)</p>\r
-<p><%= f.text_area :possible_values, :rows => 5, :cols => 60 %><br>(<%=l(:text_possible_values_info)%>)</p>
+<p id="custom_field_possible_values"><label><%= l(:field_possible_values) %> <%= image_to_function "add.png", "addValueField();return false" %></label>
+<% (@custom_field.possible_values.to_a + [""]).each do |value| %>
+<span><%= text_field_tag 'custom_field[possible_values][]', value, :size => 30 %> <%= image_to_function "delete.png", "deleteValueField(this);return false" %><br /></span>
+<% end %>
+
+</p>
</div>\r
<%= javascript_tag "toggle_custom_field_format();" %>
<!--[eoform:custom_field]-->