Silverstripe 4.1.1 - Submitting a form after performing an ajax request fails










1















I have a standard silverstripe form that uses jquery validate to validate the required field. I need to perform some other validation prior to the form being submitted so have used the submithandler of jquery validate to perform an ajax request and if a successful result comes back, to submit the form as per the code below:



$('form').validate(
rules:
**MY FIELD RULES**
,
submitHandler: function(form)
$.ajax(
url: **MY URL**,
success: function(response)
var resp = JSON.parse(response);
if(resp.ErrorMessage == '')
form.submit();
else
alert(resp.ErrorMessage);

,
);

);


When submitting the form using the code above, the Ajax function is called and returns correctly, however, when the form is submitted, I receive the following message:




ERROR [UNKNOWN TYPE, ERRNO 404]:

IN POST MY FORM URL

Line in

Trace

=====

SilverStripeDevCliDebugView->renderTrace()

DetailedErrorFormatter.php:119


SilverStripeLoggingDetailedErrorFormatter->output(404, , , , )

DetailedErrorFormatter.php:54


SilverStripeLoggingDetailedErrorFormatter->format(Array)

HTTPResponse.php:355


SilverStripeControlHTTPResponse->outputBody()

HTTPResponse.php:283


SilverStripeControlHTTPResponse->output()

index.php:26




However, if I place the form submit before the ajax call like so:



$('form').validate(
rules:
**MY FIELD RULES**
,
submitHandler: function(form)
form.submit();
$.ajax(
url: **MY URL**,
success: function(response)
var resp = JSON.parse(response);
if(resp.ErrorMessage == '')
// form.submit();
else
alert(resp.ErrorMessage);

,
);

);


The form submission works correctly.



I do have custom routing in this project, but I don’t think this is what is causing the problem as the form submission works in the second example. However, the routing.yml is as follows:



---
Name: myproject
---
SilverStripeCoreManifestModuleManifest:
project: mysite
SilverStripeSiteConfigSiteConfig:
extensions:
- SalonVisionSalonVisionSiteConfigExtension
---
Name: modelascontrollerroutes2
Before:
- '#modelascontrollerroutes'
After:
- '#adminroutes'
- '#graphqlroutes'
---
SilverStripeControlDirector:
rules:
'$Salon/$Location/$URLSegment//$Action/$ID/$OtherID': 'SalonVisionSalonVisionCModelAsController'


and I am using $form->setFormAction($location->baseLocationURL().$form->FormAction()); to add the correct $Salon/$Location to the action URL.



I have tried disabling the security token with $form->disableSecurityToken(); just incase that was causing the problem but not joy.



Furthermore:



As I understand it, when you submit a Silverstripe form, the original form method is called (the one that generates the form) and then the form action method is called.



If I display the form page in my browser and then add a php die statement at the end of the form method, then submit the form, the die message is shown. If I place the die message at the start of the form action method, the error above is shown.



This means that the problem must lie when silverstripe is trying to call the form action method. I don’t know how this works internally but any thoughts would be greatly appreciated.










share|improve this question




























    1















    I have a standard silverstripe form that uses jquery validate to validate the required field. I need to perform some other validation prior to the form being submitted so have used the submithandler of jquery validate to perform an ajax request and if a successful result comes back, to submit the form as per the code below:



    $('form').validate(
    rules:
    **MY FIELD RULES**
    ,
    submitHandler: function(form)
    $.ajax(
    url: **MY URL**,
    success: function(response)
    var resp = JSON.parse(response);
    if(resp.ErrorMessage == '')
    form.submit();
    else
    alert(resp.ErrorMessage);

    ,
    );

    );


    When submitting the form using the code above, the Ajax function is called and returns correctly, however, when the form is submitted, I receive the following message:




    ERROR [UNKNOWN TYPE, ERRNO 404]:

    IN POST MY FORM URL

    Line in

    Trace

    =====

    SilverStripeDevCliDebugView->renderTrace()

    DetailedErrorFormatter.php:119


    SilverStripeLoggingDetailedErrorFormatter->output(404, , , , )

    DetailedErrorFormatter.php:54


    SilverStripeLoggingDetailedErrorFormatter->format(Array)

    HTTPResponse.php:355


    SilverStripeControlHTTPResponse->outputBody()

    HTTPResponse.php:283


    SilverStripeControlHTTPResponse->output()

    index.php:26




    However, if I place the form submit before the ajax call like so:



    $('form').validate(
    rules:
    **MY FIELD RULES**
    ,
    submitHandler: function(form)
    form.submit();
    $.ajax(
    url: **MY URL**,
    success: function(response)
    var resp = JSON.parse(response);
    if(resp.ErrorMessage == '')
    // form.submit();
    else
    alert(resp.ErrorMessage);

    ,
    );

    );


    The form submission works correctly.



    I do have custom routing in this project, but I don’t think this is what is causing the problem as the form submission works in the second example. However, the routing.yml is as follows:



    ---
    Name: myproject
    ---
    SilverStripeCoreManifestModuleManifest:
    project: mysite
    SilverStripeSiteConfigSiteConfig:
    extensions:
    - SalonVisionSalonVisionSiteConfigExtension
    ---
    Name: modelascontrollerroutes2
    Before:
    - '#modelascontrollerroutes'
    After:
    - '#adminroutes'
    - '#graphqlroutes'
    ---
    SilverStripeControlDirector:
    rules:
    '$Salon/$Location/$URLSegment//$Action/$ID/$OtherID': 'SalonVisionSalonVisionCModelAsController'


    and I am using $form->setFormAction($location->baseLocationURL().$form->FormAction()); to add the correct $Salon/$Location to the action URL.



    I have tried disabling the security token with $form->disableSecurityToken(); just incase that was causing the problem but not joy.



    Furthermore:



    As I understand it, when you submit a Silverstripe form, the original form method is called (the one that generates the form) and then the form action method is called.



    If I display the form page in my browser and then add a php die statement at the end of the form method, then submit the form, the die message is shown. If I place the die message at the start of the form action method, the error above is shown.



    This means that the problem must lie when silverstripe is trying to call the form action method. I don’t know how this works internally but any thoughts would be greatly appreciated.










    share|improve this question


























      1












      1








      1








      I have a standard silverstripe form that uses jquery validate to validate the required field. I need to perform some other validation prior to the form being submitted so have used the submithandler of jquery validate to perform an ajax request and if a successful result comes back, to submit the form as per the code below:



      $('form').validate(
      rules:
      **MY FIELD RULES**
      ,
      submitHandler: function(form)
      $.ajax(
      url: **MY URL**,
      success: function(response)
      var resp = JSON.parse(response);
      if(resp.ErrorMessage == '')
      form.submit();
      else
      alert(resp.ErrorMessage);

      ,
      );

      );


      When submitting the form using the code above, the Ajax function is called and returns correctly, however, when the form is submitted, I receive the following message:




      ERROR [UNKNOWN TYPE, ERRNO 404]:

      IN POST MY FORM URL

      Line in

      Trace

      =====

      SilverStripeDevCliDebugView->renderTrace()

      DetailedErrorFormatter.php:119


      SilverStripeLoggingDetailedErrorFormatter->output(404, , , , )

      DetailedErrorFormatter.php:54


      SilverStripeLoggingDetailedErrorFormatter->format(Array)

      HTTPResponse.php:355


      SilverStripeControlHTTPResponse->outputBody()

      HTTPResponse.php:283


      SilverStripeControlHTTPResponse->output()

      index.php:26




      However, if I place the form submit before the ajax call like so:



      $('form').validate(
      rules:
      **MY FIELD RULES**
      ,
      submitHandler: function(form)
      form.submit();
      $.ajax(
      url: **MY URL**,
      success: function(response)
      var resp = JSON.parse(response);
      if(resp.ErrorMessage == '')
      // form.submit();
      else
      alert(resp.ErrorMessage);

      ,
      );

      );


      The form submission works correctly.



      I do have custom routing in this project, but I don’t think this is what is causing the problem as the form submission works in the second example. However, the routing.yml is as follows:



      ---
      Name: myproject
      ---
      SilverStripeCoreManifestModuleManifest:
      project: mysite
      SilverStripeSiteConfigSiteConfig:
      extensions:
      - SalonVisionSalonVisionSiteConfigExtension
      ---
      Name: modelascontrollerroutes2
      Before:
      - '#modelascontrollerroutes'
      After:
      - '#adminroutes'
      - '#graphqlroutes'
      ---
      SilverStripeControlDirector:
      rules:
      '$Salon/$Location/$URLSegment//$Action/$ID/$OtherID': 'SalonVisionSalonVisionCModelAsController'


      and I am using $form->setFormAction($location->baseLocationURL().$form->FormAction()); to add the correct $Salon/$Location to the action URL.



      I have tried disabling the security token with $form->disableSecurityToken(); just incase that was causing the problem but not joy.



      Furthermore:



      As I understand it, when you submit a Silverstripe form, the original form method is called (the one that generates the form) and then the form action method is called.



      If I display the form page in my browser and then add a php die statement at the end of the form method, then submit the form, the die message is shown. If I place the die message at the start of the form action method, the error above is shown.



      This means that the problem must lie when silverstripe is trying to call the form action method. I don’t know how this works internally but any thoughts would be greatly appreciated.










      share|improve this question
















      I have a standard silverstripe form that uses jquery validate to validate the required field. I need to perform some other validation prior to the form being submitted so have used the submithandler of jquery validate to perform an ajax request and if a successful result comes back, to submit the form as per the code below:



      $('form').validate(
      rules:
      **MY FIELD RULES**
      ,
      submitHandler: function(form)
      $.ajax(
      url: **MY URL**,
      success: function(response)
      var resp = JSON.parse(response);
      if(resp.ErrorMessage == '')
      form.submit();
      else
      alert(resp.ErrorMessage);

      ,
      );

      );


      When submitting the form using the code above, the Ajax function is called and returns correctly, however, when the form is submitted, I receive the following message:




      ERROR [UNKNOWN TYPE, ERRNO 404]:

      IN POST MY FORM URL

      Line in

      Trace

      =====

      SilverStripeDevCliDebugView->renderTrace()

      DetailedErrorFormatter.php:119


      SilverStripeLoggingDetailedErrorFormatter->output(404, , , , )

      DetailedErrorFormatter.php:54


      SilverStripeLoggingDetailedErrorFormatter->format(Array)

      HTTPResponse.php:355


      SilverStripeControlHTTPResponse->outputBody()

      HTTPResponse.php:283


      SilverStripeControlHTTPResponse->output()

      index.php:26




      However, if I place the form submit before the ajax call like so:



      $('form').validate(
      rules:
      **MY FIELD RULES**
      ,
      submitHandler: function(form)
      form.submit();
      $.ajax(
      url: **MY URL**,
      success: function(response)
      var resp = JSON.parse(response);
      if(resp.ErrorMessage == '')
      // form.submit();
      else
      alert(resp.ErrorMessage);

      ,
      );

      );


      The form submission works correctly.



      I do have custom routing in this project, but I don’t think this is what is causing the problem as the form submission works in the second example. However, the routing.yml is as follows:



      ---
      Name: myproject
      ---
      SilverStripeCoreManifestModuleManifest:
      project: mysite
      SilverStripeSiteConfigSiteConfig:
      extensions:
      - SalonVisionSalonVisionSiteConfigExtension
      ---
      Name: modelascontrollerroutes2
      Before:
      - '#modelascontrollerroutes'
      After:
      - '#adminroutes'
      - '#graphqlroutes'
      ---
      SilverStripeControlDirector:
      rules:
      '$Salon/$Location/$URLSegment//$Action/$ID/$OtherID': 'SalonVisionSalonVisionCModelAsController'


      and I am using $form->setFormAction($location->baseLocationURL().$form->FormAction()); to add the correct $Salon/$Location to the action URL.



      I have tried disabling the security token with $form->disableSecurityToken(); just incase that was causing the problem but not joy.



      Furthermore:



      As I understand it, when you submit a Silverstripe form, the original form method is called (the one that generates the form) and then the form action method is called.



      If I display the form page in my browser and then add a php die statement at the end of the form method, then submit the form, the die message is shown. If I place the die message at the start of the form action method, the error above is shown.



      This means that the problem must lie when silverstripe is trying to call the form action method. I don’t know how this works internally but any thoughts would be greatly appreciated.







      javascript php jquery ajax silverstripe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 14:10









      Barry

      3,10571637




      3,10571637










      asked Nov 12 '18 at 14:06









      PhillBexPhillBex

      435




      435






















          0






          active

          oldest

          votes











          Your Answer






          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: "1"
          ;
          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263856%2fsilverstripe-4-1-1-submitting-a-form-after-performing-an-ajax-request-fails%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263856%2fsilverstripe-4-1-1-submitting-a-form-after-performing-an-ajax-request-fails%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo