Mailinglist Archive: yast-devel (59 mails)

< Previous Next >
Re: [yast-devel] Webyast - models in webclient and webservice - summary
  • From: Ladislav Slezak <lslezak@xxxxxxx>
  • Date: Mon, 08 Mar 2010 20:15:05 +0100
  • Message-id: <4B954CB9.6030805@xxxxxxx>
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
validations which checks attributes before save same behavior as ActiveRecord

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

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.

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
- use error_messages_for() ViewHelper function for creating/displaying
validation errors

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


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
To unsubscribe, e-mail: yast-devel+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: yast-devel+help@xxxxxxxxxxxx

< Previous Next >
Follow Ups