Using EXISTS & DEVOID for POST variables and Mapper fields

77 views
Skip to first unread message

ethanpil

unread,
Nov 1, 2016, 2:53:25 PM11/1/16
to f3-fra...@googlegroups.com
Just wondering the group's opinion on how to check if a field exists and has a value in POST or in a Mapper... I have used all three below. 

Typical cases, A field is POSTed but is empty, A field is pulled from DB but is NULL or empty. Want to detect this.

 Some questions:
  • Whats the most common method? (Am I missing something)
  • Whats the pro & con of each method?

if ( $f3->exists('POST.data',$data) && isset($data) )
if ( $f3->exists('POST.data') && $f3->get('POST.data') )
if ( $f3->exists('POST.data') && !$f3->devoid('POST.data') )


if ( $mapper->exists('data',$data) && isset($data) )
if ( $mapper->exists('data') && $mapper->get('data') )
//There is no devoid() for mapper as far as I know

ethanpil

unread,
Nov 2, 2016, 12:31:45 PM11/2/16
to Fat-Free Framework
No opinions on such a common task? How does every verify existence of data points?

Vahrokh Vain

unread,
Nov 2, 2016, 3:42:20 PM11/2/16
to Fat-Free Framework
I am not using the mapper and I get sent JSON data as input to my PHP scripts, that's why I couldn't tell you :)

Nuwanda

unread,
Nov 2, 2016, 4:28:46 PM11/2/16
to Fat-Free Framework
If you've upgraded to PHP 7 you can simply use the null coalescing operator:

[scroll down a bit]

http://php.net/manual/en/migration70.new-features.php

ikkez

unread,
Nov 2, 2016, 6:47:43 PM11/2/16
to Fat-Free Framework
I am using devoid for most form-related checks:

if (!$f3->devoid('POST.email', $email)) {
  echo $email
; // it's existing and not empty
}

for mapper I don't use "exists". Most times I know which properties are defined an which are not. If you're using sql, the table schema dictates it for every record. For document based storages, like Mongo or Jig, it should be enough to check it with isset($mapper['data'])

ethanpil

unread,
Nov 2, 2016, 10:19:05 PM11/2/16
to f3-fra...@googlegroups.com
So devoid will return false if POST.email doesnt exists? I will try it.
Reply all
Reply to author
Forward
0 new messages