Mailinglist Archive: yast-devel (59 mails)

< Previous Next >
Re: [yast-devel] Webyast - models in webclient and webservice - summary
  • From: Josef Reidinger <jreidinger@xxxxxxx>
  • Date: Tue, 9 Mar 2010 08:38:06 +0100
  • Message-id: <201003090838.06839.jreidinger@xxxxxxx>
Ladislav Slezak write:
On 11.1.2010 10:24, Josef Reidinger wrote:
why use it? smart to_xml and to_json so you don't need to define own method
you have hash with problematic keys (see time model for example how to
solve it)
validations which checks attributes before save same behavior as
ActiveRecord and

I have added some validations to the repository model in the REST service.
It's easy and it's well readable, e.g.:

validates_inclusion_of :priority, :in => 0..200

sets valid range for @priority attribute to 0-200.

But I'd like to improve error reporting to the client when validation fails
in save().

An ActiveModel object has errors() method which returns data about the failed
validations. It's even possible to convert them to XML using to_xml.

And here is the problem: to_xml returns invalid data which cannot
be parsed:

r = 'oss', 'oss', true
=> #<Repository:0x7fbd70deda70 @url="", @enabled=true, @keep_packages=false,
@autorefresh=true, @name="oss", @id="oss", @priority=99>
r.priority = 'sdfdsfd'
=> "sdfdsfd"
=> false
# this is correct, priority must be in range 0-200 and URL cannot be empty
x = r.errors.to_xml
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>\n <error:blank/>\n
REXML::UndefinedNamespaceException: Undefined prefix error found

^^^ The XML parsing fails here...

Hmm, it looks like bug.

Another (probably related) problem is that the error list doesn't
contain the failed attribute name:

=> [:blank, :inclusion]

After changing the validation statements to

validates_inclusion_of :priority, :in => 0..200, :message => 'invalid
validates_presence_of :url, :message => 'empty URL'

I get:

=> ["empty URL", "invalid priority"]

And to_xml produces parseable output:

x = r.errors.to_xml
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>\n <error>empty
URL</error>\n <error>invalid priority</error>\n</errors>\n"
=> {"errors"=>{"error"=>["empty URL", "invalid priority"]}}

But defining error messages in backend is wrong, they should be translated by
the frontend.

define error message in backend is good that it is well defined and could be
easily parsed in frontend.

Moreover I don't want to add a custom error message to every validation,
it could work automatically:

- the REST service should return ActiveResource::Errors#to_xml result
- the client should
- check ActiveResource::Validations#errors for errors

This work if you use save! on backend. Then it return 422 with correct xml and
it sets errors on frontend. If this doesn't work please report bug.

- use error_messages_for() ViewHelper function for creating/displaying
validation errors

I think that there is problem with localization

Any idea how to make it work?

Or do I need to define the same validations in the client model and provide
translations there? (The advantage would be no communication between client
and server in case of invalid input.)

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).



Best Regards

Ladislav Slez√°k
Yast Developer
SUSE LINUX, s.r.o. e-mail: lslezak@xxxxxxx
Lihovarsk√° 1060/12 tel: +420 284 028 960
190 00 Prague 9 fax: +420 284 028 951
Czech Republic

Josef Reidinger
YaST team
maintainer of perl-Bootloader, YaST2-Repair, parts of webyast
To unsubscribe, e-mail: yast-devel+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: yast-devel+help@xxxxxxxxxxxx

< Previous Next >
Follow Ups