Make WordPress Core

Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#20110 closed enhancement (wontfix)

Add a valued() function to encourage consistency in input fields

Reported by: jeremyfelt's profile jeremyfelt Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Template Keywords: close
Focuses: Cc:

Description (last modified by helenyhou)

A valued() function would fit well with the helper functions selected() and checked().

Makes it easier to add a value if it exists, and to escape the value with esc_attr().

Makes it easier to provide a placeholder text.

Use would be:

<input type="text" <?php valued( $possible_num, true, '4 digit number' ); ?>>

Which would result in:

<input type="text" value="1234" placeholder="4 digit number">

Or

<input type="text" placeholder="4 digit number">

(Depending on the original value of $possible_num)

Attachments (2)

20110.diff (1.0 KB) - added by jeremyfelt 13 years ago.
20110.2.diff (1.0 KB) - added by jeremyfelt 13 years ago.
Updated function

Download all attachments as: .zip

Change History (20)

@jeremyfelt
13 years ago

#1 @helenyhou
13 years ago

  • Description modified (diff)

#2 @scribu
13 years ago

  • Keywords 2nd-opinion added

I don't really see the benefit. Unlike checked and selected, writing value="" has no adverse effects, so there's no if statement necessary.

#3 @solarissmoke
13 years ago

Also, the proposed function would fail if you supplied it a falsy value, e.g., '0'.

(by fail I mean it wouldn't output a value when it should output "value='0'")

Last edited 13 years ago by solarissmoke (previous) (diff)

@jeremyfelt
13 years ago

Updated function

#4 @jeremyfelt
13 years ago

Good catch. New diff attached with fixed function that handles 0 correctly for both $value and $placeholder.

I think the benefit comes when writing several input fields for meta boxes. Would make code cleaner to read and easier to keep consistent.

#5 @nacin
13 years ago

Core doesn't use placeholder values, so I'm not sure how useful this is yet.

#6 @ocean90
13 years ago

Would make code cleaner to read

Really? With your function you need to check first if a placeholder or a value attribute is meant. And I'm not a fan of the name valued.

#7 @scribu
13 years ago

This doesn't seem more readable to me:

<input type="text" <?php valued( $possible_num, true, '4 digit number' ); ?>>

I have no idea what the second and third parameters do, unless I look up the definition for the valued() function.

The placeholder attribute has no place in the valued() function.

#8 follow-up: @scribu
13 years ago

In general, let's not make the same mistake we made with submit_button(), where we ended up with code like this:

submit_button( __( 'Apply' ), 'button-secondary action', false, false, array( 'id' => "doaction$two" ) );

You get +100 psychic points if you can guess what those two false values represent, without looking at the definition.

#9 in reply to: ↑ 8 @jeremyfelt
13 years ago

You get +100 psychic points if you can guess what those two false values represent, without looking at the definition.

That could be the case with many functions. I think the true/false for echo/return works here.

I do think placeholder is a useful addition for UI purposes, but see how it could be considered fluff in a function that could provide values for more than type="text".

With the default values of the function though, only this is required:

<input type="text" <?php valued( $possible_num ); ?>>

I don't have any reasons to leave out a blank value="", so it may be that the if statement is unnecessary. Of course once you take that out, it appears I may be overreaching a bit.

#10 follow-up: @ocean90
13 years ago

What should I do if I want placeholder and value?

#11 in reply to: ↑ 10 ; follow-up: @jeremyfelt
13 years ago

Replying to ocean90:

What should I do if I want placeholder and value?

You would supply 3 arguments.

#12 in reply to: ↑ 11 @ocean90
13 years ago

Replying to jeremyfelt:

You would supply 3 arguments.

Oops. Yes, I misread the function. :)

#13 @nacin
13 years ago

You get +100 psychic points if you can guess what those two false values represent, without looking at the definition.

I know that one controls <p> tags being wrapped or not, but I didn't know which. It turns out the other one is the name attribute.

That could be the case with many functions. I think the true/false for echo/return works here.

This works sometimes, but it gets old really quick.

In fairness, valued()'s third argument is the same as the third argument for checked() and selected() — whether to echo. Unfortunately, the first and second arguments do not have parity. In checked(), selected(), and disabled(), the values are compared to ascertain whether to issue the corresponding selector. They are designed to help with loops because the code gets messy here quick.

Not to pick at a function name itself, but neither attribute is called valued, while they are called checked, selected, and disabled. All three of those are also boolean attributes that control the input state, not something that requires a value.

If we are to consider a valued() function, we might as well just have an input() function that serves up the whole element. Generally, I think HTML is enough of an "API" all on its own.

#14 @azaozz
13 years ago

Replying to scribu:

In general, let's not make the same mistake we made with submit_button()...
...You get +100 psychic points if you can guess what those two false values represent...

Agreed. Don't see a point in replacing simple HTML attributes with PHP functions that generate them. This type of functions would always need lots of args to be able to cover the more common cases which makes the code a lot harder to read. On top of that they usually don't cover all possible cases making them redundant.

#15 @azaozz
13 years ago

  • Keywords close added; has-patch 2nd-opinion removed

#16 @scribu
13 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

#17 @scribu
13 years ago

That said, I have a generic html() function that takes care of using the appropriate esc_*() helpers. I'm still not sure if it was a good idea, though.

#18 @jeremyfelt
13 years ago

All good points, I'm convinced. Thanks for taking a look, all.

The only case where this now seems to make sense is the one esc_attr() can already handle, creating redundancy.

Note: See TracTickets for help on using tickets.