User form validation using a POPO

The name of the pictureThe name of the pictureThe name of the pictureClash 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);










share|improve this question









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.























    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);










    share|improve this question









    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.





















      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);










      share|improve this question









      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






      share|improve this question









      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.











      share|improve this question









      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.









      share|improve this question




      share|improve this question








      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.




















          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.






          share|improve this answer




















            Your Answer





            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            Progrock is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            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






























            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.






            share|improve this answer
























              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.






              share|improve this answer






















                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.






                share|improve this answer












                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 hours ago









                Your Common Sense

                2,958524




                2,958524




















                    Progrock is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    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.













                     


                    draft saved


                    draft discarded














                    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













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    Confectionery