User form validation using a POPO
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I'm trying to sketch out a stripped down generic form validation example. I started by creating Plain Old PHP Objects (referred as POPOs) for form input with some classes such as a validator and html renderer class, that I then reduced to explicit functions.
The POPOs are useful in as much as you can comment and glance at expected properties. Looking at the code, one starts to wonder if I'd be better off with well defined arrays as inputs and not bother with the plain objects, as some of the object wrangling (the popo
functions) are effectively just wrappers for array functions, and perhaps an array would just do.
Are these plain objects muddying the waters?
Any comments welcome.
<?php
class UserForm
public $email;
class UserFormErrors extends UserForm
function map_array_to_popo(array $array, $object)
foreach($object as $key => $value)
$object->$key = isset($array[$key])
? $array[$key]
: null;
return $object;
function popo_has_empty_values($object)
$array = (array) $object;
return empty(array_filter($array));
function user_form_validate(UserForm $input)
$errors = new UserFormErrors;
if($input->email == '')
$errors->email = 'Email address is required.';
elseif(filter_var($input->email, FILTER_VALIDATE_EMAIL) === false)
$errors->email = 'Email address must be valid.';
return $errors;
function user_form_html_render(
$action = '',
UserForm $values,
UserFormErrors $errors
)
$escape = function($string)
return htmlspecialchars($string);
;
$error = function($messages) use ($escape)
return
empty($messages)
? ''
: '<ul><li>' .
implode('</li><li>', array_map($escape, $messages)) .
'</li></ul>';
;
?>
<form method='POST' action="<?= $action ?>">
<label for="email">Email address:</label>
<input type="text" name="email" id="email" value=
"<?= $escape($values->email) ?>">
<?= $error($errors->email) ?>
<input type="submit">
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
$input = array_filter($_POST, 'is_string');
$input = array_map('trim', $input);
$values = map_array_to_popo($input, new UserForm);
$errors = user_form_validate($values);
$valid = popo_has_empty_values($errors);
if($valid)
// Form values look good, do what you want with values.
echo 'Email address entered is: ' . $values->email;
else
// Display form with data and errors
user_form_html_render('', $values, $errors);
else
user_form_html_render('', new UserForm, new UserFormErrors);
php validation form
New contributor
Progrock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
2
down vote
favorite
I'm trying to sketch out a stripped down generic form validation example. I started by creating Plain Old PHP Objects (referred as POPOs) for form input with some classes such as a validator and html renderer class, that I then reduced to explicit functions.
The POPOs are useful in as much as you can comment and glance at expected properties. Looking at the code, one starts to wonder if I'd be better off with well defined arrays as inputs and not bother with the plain objects, as some of the object wrangling (the popo
functions) are effectively just wrappers for array functions, and perhaps an array would just do.
Are these plain objects muddying the waters?
Any comments welcome.
<?php
class UserForm
public $email;
class UserFormErrors extends UserForm
function map_array_to_popo(array $array, $object)
foreach($object as $key => $value)
$object->$key = isset($array[$key])
? $array[$key]
: null;
return $object;
function popo_has_empty_values($object)
$array = (array) $object;
return empty(array_filter($array));
function user_form_validate(UserForm $input)
$errors = new UserFormErrors;
if($input->email == '')
$errors->email = 'Email address is required.';
elseif(filter_var($input->email, FILTER_VALIDATE_EMAIL) === false)
$errors->email = 'Email address must be valid.';
return $errors;
function user_form_html_render(
$action = '',
UserForm $values,
UserFormErrors $errors
)
$escape = function($string)
return htmlspecialchars($string);
;
$error = function($messages) use ($escape)
return
empty($messages)
? ''
: '<ul><li>' .
implode('</li><li>', array_map($escape, $messages)) .
'</li></ul>';
;
?>
<form method='POST' action="<?= $action ?>">
<label for="email">Email address:</label>
<input type="text" name="email" id="email" value=
"<?= $escape($values->email) ?>">
<?= $error($errors->email) ?>
<input type="submit">
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
$input = array_filter($_POST, 'is_string');
$input = array_map('trim', $input);
$values = map_array_to_popo($input, new UserForm);
$errors = user_form_validate($values);
$valid = popo_has_empty_values($errors);
if($valid)
// Form values look good, do what you want with values.
echo 'Email address entered is: ' . $values->email;
else
// Display form with data and errors
user_form_html_render('', $values, $errors);
else
user_form_html_render('', new UserForm, new UserFormErrors);
php validation form
New contributor
Progrock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm trying to sketch out a stripped down generic form validation example. I started by creating Plain Old PHP Objects (referred as POPOs) for form input with some classes such as a validator and html renderer class, that I then reduced to explicit functions.
The POPOs are useful in as much as you can comment and glance at expected properties. Looking at the code, one starts to wonder if I'd be better off with well defined arrays as inputs and not bother with the plain objects, as some of the object wrangling (the popo
functions) are effectively just wrappers for array functions, and perhaps an array would just do.
Are these plain objects muddying the waters?
Any comments welcome.
<?php
class UserForm
public $email;
class UserFormErrors extends UserForm
function map_array_to_popo(array $array, $object)
foreach($object as $key => $value)
$object->$key = isset($array[$key])
? $array[$key]
: null;
return $object;
function popo_has_empty_values($object)
$array = (array) $object;
return empty(array_filter($array));
function user_form_validate(UserForm $input)
$errors = new UserFormErrors;
if($input->email == '')
$errors->email = 'Email address is required.';
elseif(filter_var($input->email, FILTER_VALIDATE_EMAIL) === false)
$errors->email = 'Email address must be valid.';
return $errors;
function user_form_html_render(
$action = '',
UserForm $values,
UserFormErrors $errors
)
$escape = function($string)
return htmlspecialchars($string);
;
$error = function($messages) use ($escape)
return
empty($messages)
? ''
: '<ul><li>' .
implode('</li><li>', array_map($escape, $messages)) .
'</li></ul>';
;
?>
<form method='POST' action="<?= $action ?>">
<label for="email">Email address:</label>
<input type="text" name="email" id="email" value=
"<?= $escape($values->email) ?>">
<?= $error($errors->email) ?>
<input type="submit">
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
$input = array_filter($_POST, 'is_string');
$input = array_map('trim', $input);
$values = map_array_to_popo($input, new UserForm);
$errors = user_form_validate($values);
$valid = popo_has_empty_values($errors);
if($valid)
// Form values look good, do what you want with values.
echo 'Email address entered is: ' . $values->email;
else
// Display form with data and errors
user_form_html_render('', $values, $errors);
else
user_form_html_render('', new UserForm, new UserFormErrors);
php validation form
New contributor
Progrock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I'm trying to sketch out a stripped down generic form validation example. I started by creating Plain Old PHP Objects (referred as POPOs) for form input with some classes such as a validator and html renderer class, that I then reduced to explicit functions.
The POPOs are useful in as much as you can comment and glance at expected properties. Looking at the code, one starts to wonder if I'd be better off with well defined arrays as inputs and not bother with the plain objects, as some of the object wrangling (the popo
functions) are effectively just wrappers for array functions, and perhaps an array would just do.
Are these plain objects muddying the waters?
Any comments welcome.
<?php
class UserForm
public $email;
class UserFormErrors extends UserForm
function map_array_to_popo(array $array, $object)
foreach($object as $key => $value)
$object->$key = isset($array[$key])
? $array[$key]
: null;
return $object;
function popo_has_empty_values($object)
$array = (array) $object;
return empty(array_filter($array));
function user_form_validate(UserForm $input)
$errors = new UserFormErrors;
if($input->email == '')
$errors->email = 'Email address is required.';
elseif(filter_var($input->email, FILTER_VALIDATE_EMAIL) === false)
$errors->email = 'Email address must be valid.';
return $errors;
function user_form_html_render(
$action = '',
UserForm $values,
UserFormErrors $errors
)
$escape = function($string)
return htmlspecialchars($string);
;
$error = function($messages) use ($escape)
return
empty($messages)
? ''
: '<ul><li>' .
implode('</li><li>', array_map($escape, $messages)) .
'</li></ul>';
;
?>
<form method='POST' action="<?= $action ?>">
<label for="email">Email address:</label>
<input type="text" name="email" id="email" value=
"<?= $escape($values->email) ?>">
<?= $error($errors->email) ?>
<input type="submit">
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
$input = array_filter($_POST, 'is_string');
$input = array_map('trim', $input);
$values = map_array_to_popo($input, new UserForm);
$errors = user_form_validate($values);
$valid = popo_has_empty_values($errors);
if($valid)
// Form values look good, do what you want with values.
echo 'Email address entered is: ' . $values->email;
else
// Display form with data and errors
user_form_html_render('', $values, $errors);
else
user_form_html_render('', new UserForm, new UserFormErrors);
php validation form
php validation form
New contributor
Progrock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Progrock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 3 hours ago


Your Common Sense
2,958524
2,958524
New contributor
Progrock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 4 hours ago


Progrock
1112
1112
New contributor
Progrock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Progrock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Progrock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
Personally I don't see any value in introducing such objects. Especially given you are inclined to functional programming, which is a real trademark of your code, whereas such "POPOs" indeed look alien in it.
Either way, there is one plain mistake in the code, an Error by no means is a Form, so UserForm never can be a parent for the UserFormErrors. Even for sake of sparing you from writing a couple lines of code.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Personally I don't see any value in introducing such objects. Especially given you are inclined to functional programming, which is a real trademark of your code, whereas such "POPOs" indeed look alien in it.
Either way, there is one plain mistake in the code, an Error by no means is a Form, so UserForm never can be a parent for the UserFormErrors. Even for sake of sparing you from writing a couple lines of code.
add a comment |Â
up vote
1
down vote
Personally I don't see any value in introducing such objects. Especially given you are inclined to functional programming, which is a real trademark of your code, whereas such "POPOs" indeed look alien in it.
Either way, there is one plain mistake in the code, an Error by no means is a Form, so UserForm never can be a parent for the UserFormErrors. Even for sake of sparing you from writing a couple lines of code.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Personally I don't see any value in introducing such objects. Especially given you are inclined to functional programming, which is a real trademark of your code, whereas such "POPOs" indeed look alien in it.
Either way, there is one plain mistake in the code, an Error by no means is a Form, so UserForm never can be a parent for the UserFormErrors. Even for sake of sparing you from writing a couple lines of code.
Personally I don't see any value in introducing such objects. Especially given you are inclined to functional programming, which is a real trademark of your code, whereas such "POPOs" indeed look alien in it.
Either way, there is one plain mistake in the code, an Error by no means is a Form, so UserForm never can be a parent for the UserFormErrors. Even for sake of sparing you from writing a couple lines of code.
answered 2 hours ago


Your Common Sense
2,958524
2,958524
add a comment |Â
add a comment |Â
Progrock is a new contributor. Be nice, and check out our Code of Conduct.
Progrock is a new contributor. Be nice, and check out our Code of Conduct.
Progrock is a new contributor. Be nice, and check out our Code of Conduct.
Progrock is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f206896%2fuser-form-validation-using-a-popo%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password