10

Is there a recommended way to represent booleans in HTML form hidden fields?

Is it usually a matter of existence, or should one use 1/0 or "true"/"false" strings?

Yarin
  • 173,523
  • 149
  • 402
  • 512
  • Well I know with PHP, hidden fields exist in the `$_POST` array even when empty so testing for existence (like with `isset`) wouldn't help there, you would either have to remove the field before form submission or have the server side script test for empty instead of existence. Not sure about other languages though. – Patrick Evans Sep 22 '13 at 19:07
  • @Patrick- right, i think that outright removal is probably the only way that's foolproof- which is how I think I normally handle it anyway, just always bugged me that there wasn't a standard approach one way or the other... – Yarin Sep 22 '13 at 19:48
  • Same, though my code usually uses 1 or 0 for form values since that's what gets stored in my mysql BOOLEAN fields anyway, and with php's type coercion will make them act like bools in if statements, where as with strings "true" and "false" they do not get coerced, so with php anyway no need for testing existence or testing the string values. So i guess it, till they have actual boolean input fields, it depends on what your language can do. Though this may be mute since they should be passed through validation and formatting so you would have to do actual type and value testing either way. – Patrick Evans Sep 22 '13 at 20:24
  • I suppose that shall be defined according to server side language for falsy values and simply `true` to mark boolean true. – Andriy Ivaneyko Mar 17 '16 at 03:10

2 Answers2

1

Would be useful if you are using PHP:

test.php?test=false (constructed with http_build_query( array( 'test'=>'false' ) )):

var_dump( $_REQUEST['test'] ); //string(5) "false"

var_dump( (bool)$_REQUEST['test'] ); //bool(true)

var_dump( $_REQUEST['test'] == FALSE ); //bool(false)

var_dump( $_REQUEST['test'] == "false" ); //bool(true)

--

test.php?test=0 (constructed with http_build_query( array( 'test'=>FALSE ) )):

var_dump( $_REQUEST['test'] ); //string(1) "0"

var_dump( (bool)$_REQUEST['test'] ); //bool(false)

var_dump( $_REQUEST['test'] == FALSE ); //bool(true)

MikeStack
  • 29
  • 3
1

That logic can be implemented with monkey patching of variable according to it's string value. In that case recommended way of identifying Boolean value depends on how that values are treated by server side. See monkey patching example in Ruby.

However you can avoid monkey patching with approach below:

Most likely all servers would work well with 'true' value to represent True (actually there are no matter as long as you work directly with string, but for convention it's clearly understandable) and empty string'' to represent False (because empty string '' would be treated as false in most cases, to do that you can define input[type=hidden] as placeholder of hidden value and assign corresponding value.

To define hidden input with Falsy value you have to set value attribute to empty string '', all other values would be treated as Truly.

<input type="hidden" name="is-truly" value="">

In that way most likely request.POST['is-truly'] value on the server would be treated as False ( because empty string '' is Falsy, but you have to double check that on server side)

NOTE: It's not recommended to use eval to verify variable type.

Community
  • 1
  • 1
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82