0

Hello!
I try to catch a validation message with a $.parseJSON(xhr.responseText).errors but an allert message hows me "undefined" and I dont understand why.

hier are my validations:

validates :upload_file_name,  :presence   => true,
                                :format     =>{:with => %r{\.(pdf)$}i,:message =>"It should be a pdf File"}

  validates_uniqueness_of :upload_file_name, :message => "blabla"

  validates :upload_file_size,  :inclusion  => {:in =>1.megabytes..20.megabytes,:message=>"bla"}

json:

format.json{ render json: {error: @upload.errors.full_messages}, :status =>422}

and javascript for that:

 $('#fileupload').fileupload({       


            error: function(xhr){
              var errors = $.parseJSON(xhr.responseText).errors
              alert(errors)         
            }
                  });

Thanks in advanced

2
Contributors
1
Reply
4
Views
4 Years
Discussion Span
Last Post by LastMitch
0

try to catch a validation message with a $.parseJSON(xhr.responseText).errors but an allert message hows me "undefined" and I dont understand why.

You forgot to put an s in errors.

The error has an s in the end here: $.parseJSON(xhr.responseText).errors

From this:

format.json{ render json: {error: @upload.errors.full_messages}, :status =>422}

to this:

format.json{ render json: {errors: @upload.errors.full_messages}, :status =>422}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.