Dne 9.3.2010 08:38, Josef Reidinger napsal(a):
[...]
> My idea was, that you should use save! in backend. It reports error on
> frontend
> and on frontend you can parse errors and create user friendly message for
> user. Of
> course we could have some predefined helper, which takes common validation
> string
> and translate it to default error message (not yet done, but it is easy to
> do).
OK, I have implemented it. It is in
ApplicationController::generate_error_messages.
Example usage:
1) Add validations into your REST backend model, example from the repositories
model:
# URL cannot be empty
validates_presence_of :url
# priority must be in range 0-200
validates_inclusion_of :priority, :in => 0..200
2) Use @model.save! in the REST controller (it returns 422 error code when
validation fails)
3) In the client controller:
if @repo.save
# display success
flash[:message] = _("Repository '#[email protected]}' has been updated.")
else
if @repo.errors.size > 0
# display errors from the response
flash[:error] = generate_error_messages @repo, attribute_mapping
else
# response doesn't contain details, show a generic error message
flash[:error] = _("Cannot update repository '#[email protected]}': Unknown
error")
end
end
Where attribute_mapping is a map
{
:id => _('Alias'),
:enabled => _('Enabled'),
...
}
It simply maps the attribute names to printable/translatable strings
(they should be same as the names which were displayed in the form).
The page will then contain error "URL can't be blank" if the passed
URL is empty.
--
Best Regards
Ladislav Slezák
Yast Developer
------------------------------------------------------------------------
SUSE LINUX, s.r.o. e-mail: [email protected]
Lihovarská 1060/12 tel: +420 284 028 960
190 00 Prague 9 fax: +420 284 028 951
Czech Republic http://www.suse.cz/
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]