# Xxx.valid? still true after xxx.errors.add(...)?

**URL:** <https://www.ruby-forum.com/t/xxx-valid-still-true-after-xxx-errors-add/165954>\
**Category:** Rails\
**Created:** [April 20, 2009, 11:17am UTC](https://www.ruby-forum.com/t/xxx-valid-still-true-after-xxx-errors-add/165954 "2009-04-20T11:17:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 20, 2009, 11:17am UTC](https://www.ruby-forum.com/t/xxx-valid-still-true-after-xxx-errors-add/165954/1 "2009-04-20T11:17:43Z")

</div>

Hi all

I have the following code in my controller:

```
if @comment.valid?
  captcha_url =

```

“[http://captchator.com/captcha/check\_answer/#{captcha\_code}/#{@comment.captcha}](http://captchator.com/captcha/check_answer/#%7Bcaptcha_code%7D/#%7B@comment.captcha%7D)”  
result = open(captcha\_url)  
unless result.read == “1”  
@comment.errors.add(:captcha, “Captcha wurde nicht korrekt  
eingegeben”)  
raise “#{@comment.valid?}”  
end  
end

Although I’m adding an error to :captcha, when I raise @comment.valid?  
on the next line this still gives me true! Why is that? I just added an  
error to @comment, didn’t I?!

Maybe it’s because I’m only using an attr\_accessor for captcha?

class Comment \< ActiveRecord::Base  
validates\_presence\_of :captcha  
attr\_accessor :captcha  
end

Thanks  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 20, 2009, 11:26am UTC](https://www.ruby-forum.com/t/xxx-valid-still-true-after-xxx-errors-add/165954/2 "2009-04-20T11:26:31Z")

</div>

Well, it seems that obj.valid? and obj.save remove all manually added  
errors?!

```
  @comment.errors.add_to_base("Captcha wurde nicht korrekt 

```

eingegeben")  
raise “valid? #{@comment.valid?} - errors:  
#{@comment.errors.inspect}”

results in “valid? false - errors: #\<ActiveRecord::Errors:0x23d028c  
@base=#\<Comment id: nil, commentable\_id: 38313, user\_id: 1, subject: “”,  
body: “”, created\_at: nil, updated\_at: nil, commentable\_type:  
“Article”\>, @errors={“body”=\>[“can’t be blank”], “subject”=\>[“can’t be  
blank”]}\>”

and

```
  raise "errors: #{@comment.errors.inspect}"

```

results in “errors: #\<ActiveRecord::Errors:0x1878538 @base=#\<Comment id:  
nil, commentable\_id: 38313, user\_id: 1, subject: “”, body: “”,  
created\_at: nil, updated\_at: nil, commentable\_type: “Article”\>,  
@errors={“base”=\>[“Captcha wurde nicht korrekt eingegeben”]}\>”

Why the heck does ActiveRecord remove my custom errors when calling  
.valid???

Thanks a lot for help  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 20, 2009, 12:11pm UTC](https://www.ruby-forum.com/t/xxx-valid-still-true-after-xxx-errors-add/165954/3 "2009-04-20T12:11:10Z")

</div>

On Apr 20, 10:26 am, Joshua M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> body: “”, created\_at: nil, updated\_at: nil, commentable\_type:  
> @errors={“base”=\>[“Captcha wurde nicht korrekt eingegeben”]}\>"
> 
> Why the heck does ActiveRecord remove my custom errors when calling  
> .valid???

Calling valid? recomputes the errors (there’s no difference at the end  
of the day between an error you added yourself and one from a ‘normal’  
validations. If you want to add errors yourself you should be doing so  
from a validation

Fred

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 20, 2009, 2:10pm UTC](https://www.ruby-forum.com/t/xxx-valid-still-true-after-xxx-errors-add/165954/4 "2009-04-20T14:10:52Z")

</div>

Frederick C. wrote:

> On Apr 20, 10:26ï¿½am, Joshua M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > body: “”, created\_at: nil, updated\_at: nil, commentable\_type:  
> > @errors={“base”=\>[“Captcha wurde nicht korrekt eingegeben”]}\>"
> > 
> > Why the heck does ActiveRecord remove my custom errors when calling  
> > .valid???
> 
> Calling valid? recomputes the errors (there’s no difference at the end  
> of the day between an error you added yourself and one from a ‘normal’  
> validations. If you want to add errors yourself you should be doing so  
> from a validation
> 
> Fred

OK, Thanks for the explanation 🙂
