Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #3587 (closed defect: wontfix)

Opened 3 years ago

Last modified 10 months ago

div tag added by validation methods

Reported by: sebastien@goetzilla.info Assigned to: David
Priority: normal Milestone: 2.x
Component: ActionPack Version: 1.0.0
Severity: minor Keywords:
Cc: sebastien@goetzilla.info, daniel@collectiveidea.com, masterkain, carlivar

Description

The validation methods (validates_uniqueness_of or validates_presence_of for example) add a div tag around the input that is validated.

I think it would be wiser to add a class parameter to the input. If a class parameter is already present, a new class value should be added to the actual value.

For example, instead of : <div class="fieldWithErrors"><input id="account_username" class="big" name="account[username]" size="30" type="text" value="" /></div>

it should be : <input id="account_username" class="big fieldWithErrors" name="account[username]" size="30" type="text" value="" />

In fact, the div that is added can totally break the html source, for example if a fieldset or a p tag is set around the input tag, the html validator from the w3c gets hurt !

Change History

01/24/06 01:21:13 changed by marcel

  • status changed from new to closed.
  • resolution set to wontfix.

You can customize this behavior. Look at the code example in the comments of #2210.

01/24/06 06:21:12 changed by sebastien@goetzilla.info

  • status changed from closed to reopened.
  • resolution deleted.

I have to reopen the ticket.

I saw that it was possible to customize this behavior, but I really think that this method should be modified in the trunk, because the default behavior is very bad, and totally breaks the html consistency.

I think it should be possible to give default values, for example a div="myDiv" options would put a div called myDiv around the field, and class="myClass" should add myClass to the class attribute of the field, and so on.

If no option is set, the default behaviour of setting a div would be applied, so old code won't break.

I think it's too bad to have to add extra code by hand to each Rails application in order to correct a bad behaviour, when this code could be added to the trunk.

03/11/06 00:36:04 changed by montanaharkin@yahoo.com

Could this be fixed by switching to a <span>?

03/11/06 07:43:11 changed by sebastien@goetzilla.info

Why not.

But imho, adding a class to the field is semantically more correct.

03/24/06 14:26:30 changed by daniel@collectiveidea.com

I added a patch that covers this and other invalid code #4392.

Changing to <span>, which I did, is the easiest fix that doesn't change the functionality.

I agree that adding a class to the <input>/<textarea> is the better method but after some testing by a coworker, it was too hard to get consistent behavior across browsers.

03/25/06 08:42:20 changed by sebastien@goetzilla.info

  • milestone changed from 1.1 to 1.2.

What was the problem about consistent behavior across browsers ?

(follow-up: ↓ 14 ) 03/25/06 21:00:44 changed by daniel@collectiveidea.com

  • cc changed from sebastien@goetzilla.info to sebastien@goetzilla.info daniel@collectiveidea.com.

Changing the <div> to <span> doesn't change anything.

Putting the class on the <input> caused all sorts of problems, and the CSS involved was much more complex.

07/08/06 05:38:08 changed by bitsweat

  • milestone changed from 1.2 to 1.x.

Any progress on a replacement? We can't change until after 1.2 anyway to preserve backward compatibility.

05/26/07 02:46:50 changed by tpope

  • component changed from ActiveRecord to ActionPack.

06/16/07 18:12:23 changed by luminousbit

There have been dozens of tickets about this basic issue and it would be nice to see something done about it. Many patches and alternate ideas have been submitted, but they are constantly rejected. The issue here is a simple one: - The generators commonly wrap input fields in <p> tags - The fieldWithErrors wraps those inputs with div tags inside the <p> tags - <div>s inside <p>s breaks validations - Rails as a whole should not be generating code that will not validate without hacks (especially not something as common as this)

I don't know what needs to be done to convince someone to incorporate some sort of fix. It's going to break backwards compatibility, but that's certainly better than doing it wrong.

06/16/07 20:12:30 changed by masterkain

  • cc changed from sebastien@goetzilla.info daniel@collectiveidea.com to sebastien@goetzilla.info, daniel@collectiveidea.com, masterkain.

06/18/07 01:17:05 changed by wycats

Fixing this is the first thing I do on a new Rails project. At the very least, we should make it possible to more easily modify what happens. My personal solution is:

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|

msg = instance.error_message if html_tag =~ /<(input|textarea|select)[>]+class=/

class_attribute = html_tag =~ /class=['"]/ html_tag.insert(class_attribute + 7, "error ")

elsif html_tag =~ /<(input|textarea|select)/

first_whitespace = html_tag =~ /\s/ html_tag[first_whitespace] = " class='error' "

end html_tag

end

This is more robust than a lot of the other solutions because it works with elements that already have classes (simply appending the error class).

I'm not going to submit a patch along these lines because I don't think it'll do any good. Some official guidance would be nice.

08/09/07 05:59:17 changed by yemartin

Every time out-of-the-box-Rails generates invalid HTML, God kills a kitten... Help save the kittens, please fix this in trunk.

(Just adding my vote to the "This must be fixed" side. And I think just using <SPAN> instead of <DIV> is a good and simple fix for this).

(in reply to: ↑ 7 ; follow-up: ↓ 16 ) 08/09/07 23:12:40 changed by carlivar

Replying to daniel@collectiveidea.com:

Changing the <div> to <span> doesn't change anything.

Oh yes it does. <div>'s have a line break. <span>'s don't.

My form looks terrible after Rails adds its divs... unwanted breaks.

Some info here:

http://ethilien.net/archives/fixing-divfieldwitherrors-in-ruby-on-rails/

<span> makes far more sense.

08/09/07 23:13:05 changed by carlivar

  • cc changed from sebastien@goetzilla.info, daniel@collectiveidea.com, masterkain to sebastien@goetzilla.info, daniel@collectiveidea.com, masterkain, carlivar.

(in reply to: ↑ 14 ) 08/10/07 02:58:15 changed by danielmorrison

Replying to carlivar:

Replying to daniel@collectiveidea.com:

Changing the <div> to <span> doesn't change anything.

Oh yes it does. <div>'s have a line break. <span>'s don't. My form looks terrible after Rails adds its divs... unwanted breaks.

carliver: That is easily solved with one line of CSS (.fieldWithErrors { display: inline; }). That's a very quick change (and should be added to the scaffold.css that ships with Rails).

Invalid code, on the other hand has lots of bad effects, and there's no excuse for it. Rails 2.0 is a perfect time to fix it.

However, after some experimentation I think this method of adding a class to the input is to difficult to make look good (which was my original comment). While I dont' remember the specifics (my comment is over a year old), the CSS involved to get the same look that Rails ships with today was highly complex.

That's why my year-old patch #4392 simply changes the <span> to a <div>. It keeps the rules simple, and only requires a small change (if any) to existing apps.

You might not like the other changes in my patch (#4392) but I'd be happy to split it up if I though it would help.

08/10/07 03:07:09 changed by danielmorrison

I realize I have my span/div backwards in my last comment, and the CSS too (display: table; which is already in scaffold.css)

Like I said, over a year ago...

12/17/07 02:50:39 changed by toolmantim

  • status changed from reopened to closed.
  • resolution set to wontfix.

Probably best to consolidate this with #4392 seeing as there's no patch over this-a-way.