Good way of validate form

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












What is the good way of validating a form in Drupal 8 ?



I have a bunch of fields which are supposed to be filled by a positive numeric value (and that's all, no other input types, just positive int).



public function validateForm(array &$form, FormStateInterface $form_state) 
$values = $form_state->getValues();

foreach ($values as $method => $fee)
if ($method != 'Free order')
if (!is_numeric($fee)





The issue is when using $form_state->getValues() it also returns "submit", "form_token", "form_build", "form_id", "op" which is quite boring as those infos are not needed for performing validation.



So what I did is removing those four unwanted values with :



$values = array_splice($values, count($values) - 5, 5);


But it's really annoying, am I doing something wrong, or is just as it ? Because I think those values are really not necessary in the validation process.










share|improve this question



























    up vote
    2
    down vote

    favorite












    What is the good way of validating a form in Drupal 8 ?



    I have a bunch of fields which are supposed to be filled by a positive numeric value (and that's all, no other input types, just positive int).



    public function validateForm(array &$form, FormStateInterface $form_state) 
    $values = $form_state->getValues();

    foreach ($values as $method => $fee)
    if ($method != 'Free order')
    if (!is_numeric($fee)





    The issue is when using $form_state->getValues() it also returns "submit", "form_token", "form_build", "form_id", "op" which is quite boring as those infos are not needed for performing validation.



    So what I did is removing those four unwanted values with :



    $values = array_splice($values, count($values) - 5, 5);


    But it's really annoying, am I doing something wrong, or is just as it ? Because I think those values are really not necessary in the validation process.










    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      What is the good way of validating a form in Drupal 8 ?



      I have a bunch of fields which are supposed to be filled by a positive numeric value (and that's all, no other input types, just positive int).



      public function validateForm(array &$form, FormStateInterface $form_state) 
      $values = $form_state->getValues();

      foreach ($values as $method => $fee)
      if ($method != 'Free order')
      if (!is_numeric($fee)





      The issue is when using $form_state->getValues() it also returns "submit", "form_token", "form_build", "form_id", "op" which is quite boring as those infos are not needed for performing validation.



      So what I did is removing those four unwanted values with :



      $values = array_splice($values, count($values) - 5, 5);


      But it's really annoying, am I doing something wrong, or is just as it ? Because I think those values are really not necessary in the validation process.










      share|improve this question













      What is the good way of validating a form in Drupal 8 ?



      I have a bunch of fields which are supposed to be filled by a positive numeric value (and that's all, no other input types, just positive int).



      public function validateForm(array &$form, FormStateInterface $form_state) 
      $values = $form_state->getValues();

      foreach ($values as $method => $fee)
      if ($method != 'Free order')
      if (!is_numeric($fee)





      The issue is when using $form_state->getValues() it also returns "submit", "form_token", "form_build", "form_id", "op" which is quite boring as those infos are not needed for performing validation.



      So what I did is removing those four unwanted values with :



      $values = array_splice($values, count($values) - 5, 5);


      But it's really annoying, am I doing something wrong, or is just as it ? Because I think those values are really not necessary in the validation process.







      8 forms






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 hours ago









      Aporie

      10712




      10712




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          The FormState class has a method for that: FormState::cleanValues(). From its description, it's the method you should use.




          This function can be used when a module wants to store all submitted form values, for example, by serializing them into a single database column. In such cases, all internal Form API values and all form button elements should not be contained, and this function allows their removal before the module proceeds to storage. Next to button elements, the following internal values are removed by default.



          • form_id

          • form_token

          • form_build_id

          • op



          You simply call it before getting the submitted values.



          public function validateForm(array &$form, FormStateInterface $form_state) {
          $form_state->cleanValues();
          $values = $form_state->getValues();

          foreach ($values as $method => $fee)
          if ($method != 'Free order')



          If you need to add more keys to remove from the submitted values, you could use FormState::setCleanValueKeys(), but keep in mind that:



          • The array passed to the method needs to contain also the default array keys Drupal would remove

          • The method alters the array keys that are removed from FormState::cleanValues() for every Drupal form





          share|improve this answer


















          • 1




            Thanks @kiamlaluno, exactly what I was looking for !
            – Aporie
            2 hours ago










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "220"
          ;
          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: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f270957%2fgood-way-of-validate-form%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
          3
          down vote



          accepted










          The FormState class has a method for that: FormState::cleanValues(). From its description, it's the method you should use.




          This function can be used when a module wants to store all submitted form values, for example, by serializing them into a single database column. In such cases, all internal Form API values and all form button elements should not be contained, and this function allows their removal before the module proceeds to storage. Next to button elements, the following internal values are removed by default.



          • form_id

          • form_token

          • form_build_id

          • op



          You simply call it before getting the submitted values.



          public function validateForm(array &$form, FormStateInterface $form_state) {
          $form_state->cleanValues();
          $values = $form_state->getValues();

          foreach ($values as $method => $fee)
          if ($method != 'Free order')



          If you need to add more keys to remove from the submitted values, you could use FormState::setCleanValueKeys(), but keep in mind that:



          • The array passed to the method needs to contain also the default array keys Drupal would remove

          • The method alters the array keys that are removed from FormState::cleanValues() for every Drupal form





          share|improve this answer


















          • 1




            Thanks @kiamlaluno, exactly what I was looking for !
            – Aporie
            2 hours ago














          up vote
          3
          down vote



          accepted










          The FormState class has a method for that: FormState::cleanValues(). From its description, it's the method you should use.




          This function can be used when a module wants to store all submitted form values, for example, by serializing them into a single database column. In such cases, all internal Form API values and all form button elements should not be contained, and this function allows their removal before the module proceeds to storage. Next to button elements, the following internal values are removed by default.



          • form_id

          • form_token

          • form_build_id

          • op



          You simply call it before getting the submitted values.



          public function validateForm(array &$form, FormStateInterface $form_state) {
          $form_state->cleanValues();
          $values = $form_state->getValues();

          foreach ($values as $method => $fee)
          if ($method != 'Free order')



          If you need to add more keys to remove from the submitted values, you could use FormState::setCleanValueKeys(), but keep in mind that:



          • The array passed to the method needs to contain also the default array keys Drupal would remove

          • The method alters the array keys that are removed from FormState::cleanValues() for every Drupal form





          share|improve this answer


















          • 1




            Thanks @kiamlaluno, exactly what I was looking for !
            – Aporie
            2 hours ago












          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          The FormState class has a method for that: FormState::cleanValues(). From its description, it's the method you should use.




          This function can be used when a module wants to store all submitted form values, for example, by serializing them into a single database column. In such cases, all internal Form API values and all form button elements should not be contained, and this function allows their removal before the module proceeds to storage. Next to button elements, the following internal values are removed by default.



          • form_id

          • form_token

          • form_build_id

          • op



          You simply call it before getting the submitted values.



          public function validateForm(array &$form, FormStateInterface $form_state) {
          $form_state->cleanValues();
          $values = $form_state->getValues();

          foreach ($values as $method => $fee)
          if ($method != 'Free order')



          If you need to add more keys to remove from the submitted values, you could use FormState::setCleanValueKeys(), but keep in mind that:



          • The array passed to the method needs to contain also the default array keys Drupal would remove

          • The method alters the array keys that are removed from FormState::cleanValues() for every Drupal form





          share|improve this answer














          The FormState class has a method for that: FormState::cleanValues(). From its description, it's the method you should use.




          This function can be used when a module wants to store all submitted form values, for example, by serializing them into a single database column. In such cases, all internal Form API values and all form button elements should not be contained, and this function allows their removal before the module proceeds to storage. Next to button elements, the following internal values are removed by default.



          • form_id

          • form_token

          • form_build_id

          • op



          You simply call it before getting the submitted values.



          public function validateForm(array &$form, FormStateInterface $form_state) {
          $form_state->cleanValues();
          $values = $form_state->getValues();

          foreach ($values as $method => $fee)
          if ($method != 'Free order')



          If you need to add more keys to remove from the submitted values, you could use FormState::setCleanValueKeys(), but keep in mind that:



          • The array passed to the method needs to contain also the default array keys Drupal would remove

          • The method alters the array keys that are removed from FormState::cleanValues() for every Drupal form






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 hours ago

























          answered 2 hours ago









          kiamlaluno♦

          78.9k9125244




          78.9k9125244







          • 1




            Thanks @kiamlaluno, exactly what I was looking for !
            – Aporie
            2 hours ago












          • 1




            Thanks @kiamlaluno, exactly what I was looking for !
            – Aporie
            2 hours ago







          1




          1




          Thanks @kiamlaluno, exactly what I was looking for !
          – Aporie
          2 hours ago




          Thanks @kiamlaluno, exactly what I was looking for !
          – Aporie
          2 hours ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f270957%2fgood-way-of-validate-form%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          Long meetings (6-7 hours a day): Being “babysat” by supervisor

          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

          Confectionery