How can I display a warning message on the Odoo 10 website? - odoo-10

I want to make a condition of a field value in an Odoo 10 form, but I don't know how to do it. I tried to implement this function in models:
#api.model
def website_form_input_filter(self, request, values):
if 'titre' in values:
values['titre'] = values['titre'].strip()
if len(values['titre']) < 3:
raise ValidationError(
'Text must be at least 3 characters long')
return values
How can I do it?

Related

Add one spot to a value

Its possible to book appointments in my app and I get the queue line value from this code to display for the users in the app:
Text = $"Spot {_Class.Slots.WaitList}";
What im trying to get here is to show the queue that person has. The issue is that the value im getting from that line is the value that says how many people there are in the line. So if a person books an appointment the value has to increase from e.g 2 to 3.
EDIT:
The value im getting from this code I get from the database:
Text = $"Spot {_Class.Slots.WaitList}";
The value is 1 (just an example) but i need the value to be +1 every time.
I tried to do this:
Text = $"Spot {_Class.Slots.WaitList + "1"}";
But it added 10 instead of 1.
What's the type of WaitList field/property?
I don't see a reason why this wouldn't work:
Text = $"Spot {_Class.Slots.WaitList + 1}";

extract value for calculated attribute

within Rails4, the following logic determines an attribute
if #items.count == 1
value = "disc1"
elsif #items.count == 2
value = "disc2"
else
end
which would then need to be accessed
#cluster.value
however this syntax does not work as the value is not a method. How can this variable be employed to extract the object's thus-named attribute?
It would be better if you just conditionally call methods:
if #items.count == 1
#cluster.disc1
elsif #items.count == 2
#cluster.disc2
else
# ...
end
Still, if you want to go that route:
#cluster.public_send value
Or if #cluster is an ActiveRecord model and you want to fetch the stored value in the corresponding table, you could:
#cluster[value]

Text input value as a number

I want input field value to be a number instead of string.
A simple scenario is you have 2 input fields and submit button on a page. When you click submit you should get sum of numbers keyed in both the input fields and not appended strings.
I tried using "number_field_tag" for input type=number but the value is still a String and not Fixnum what I want.
As Surya stated in his comment, what comes from the form fields is always a string. But you can do something like this in the controller action that processes the form (presuming you're using Rails):
def process_form
#result = params[:first_field].to_i + params[:second_field].to_i
end

Getting model value and generated ID from within simple_form custom input

I'm trying to make a custom input type with simple_form that will implement combobox-type functionality using jQuery-Autocomplete
. What I need to do is output a hidden field that will hold the ID of the value selected and a text field for the user to type in.
Here's what I have so far:
class ComboboxInput < SimpleForm::Inputs::Base
def input
html = #builder.hidden_field(attribute_name, input_html_options)
id = '' #what?
value = '' #what?
return "#{html}<input class='combobox-entry' data-id-input='#{id}' value='#{value}'".html_safe
end
end
I need to get the ID of the hidden field that simple_form is generating to place as an HTML attribute on the text entry to allow the JavaScript to "hook up" the two fields. I also need to get the value from the model to prepopulate the text input. How do I do this from within my custom input?
I'm looking for the id as well, but I did get the value:
def input
current_value = object.send("#{attribute_name}")
end
I just found a hokey id workaround:
html = #builder.hidden_field(attribute_name, input_html_options)
id = html.scan(/id="([^"]*)"/).first.first.to_s
I know it's a hack, but it does work. Since we don't have access directly to this type of resolution, it is likely to keep working even if the underlying id creation code changes.

Ruby: How to get a class based on class name and how can I get object's field based on field name?

Question 1
How to get a class given a class name as a string ?
For example, say Product class has do_something method:
str = "product"
<what should be here based on str?>.do_something
Question 2
How to get object's field given a field name as a string ?
For example, say Product class has price field:
str = "price"
product = Product.new
product.<what should be here based on str?> = 1200
Jacob's answer to the first question assumes that you're using Rails and will work fine if you are. In case you're not you can call Kernel::const_get(str) to find an existing constant by name.
send is a pure ruby. There's no need to intern your strings with send though (convert them to symbols), straight strings work fine.
Use capitalize and constantize:
str.capitalize.constantize.do_something
Use send:
product.send(str + '=', 1200)

Resources