CSRF token mismatch. in cake php 3.7

I created simple form when i can click on submit button it was thrown an Exception
CSRF token mismatch.Cake\Http\Exception\InvalidCsrfTokenException

I am new in this forum also and cake php. Please anyone suggestion to get rid of this error.
I tried but i don’t know where exactly changes in the cake php code.

Try using:

$this->Form and then form.submit via javascript. Much easier…

or if you have already loaded js… then xhr. must follow:

setRequestHeader('X-CSRF-Token', <?= json_encode($this->request->param('_csrfToken')); ?>);

Cheers

1 Like
<?php 

echo $this->Form->create(
  null,
  [
    "url"=>["action"=>"save"],
    "class"=>"form-horizontal"
  ]
);

?>
Thanks for reply
I am doing this code and define function save() in the controller.

Full code please.

What about $this->Form->end() in the end?

<div class="panel panel-default">
  <div class="panel-heading">Create Book</div>
  <div class="panel-body">

<?php 

echo $this->Form->create(
  null,
  [
    "url"=>["action"=>"save"],
    "class"=>"form-horizontal"
  ]
);




?>

  <div class="form-group">
<label class="control-label col-sm-2" for="name">Name:</label>
<div class="col-sm-10">
  <input type="name" class="form-control" id="name" placeholder="Enter name">
</div>
  </div>
  <div class="form-group">
<label class="control-label col-sm-2" for="author">Author:</label>
<div class="col-sm-10">
  <input type="author" class="form-control" id="author" placeholder="Enter author">
</div>
  </div>
  <div class="form-group">
<label class="control-label col-sm-2" for="email">Email ID:</label>
<div class="col-sm-10">
  <input type="email" class="form-control" id="email" placeholder="Enter Email ID">
</div>
  </div>
  <div class="form-group">
<label class="control-label col-sm-2" for="description">Description:</label>
<div class="col-sm-10">
 <textarea class="form-control" name="description" id="description" placeholder="Enter Description"></textarea>
</div>
  </div>
  <div class="form-group"> 
<div class="col-sm-offset-2 col-sm-10">
  <button type="submit" name="submit" class="btn btn-success">Submit</button>
</div>
  </div>
<?php echo $this->Form->end() ?>
</div>
</div>

Here is Full code.

Try using Form->control()

I understand you are having a hard type creating forms with the correct class names.
You could easily create templates for your controls. [https://book.cakephp.org/3.0/en/views/helpers/form.html]

Let me give you an example. Hang on…

 <?php

$myTemplate = [
    'label' => '<label class="control-label col-sm-2">{{text}}</label>'
    'inputContainer' => '<div class="col-sm-10">{{content}}</div>',
];

$this->Form->setTemplates($myTemplate)

You may wrap them in their respective DIVs.

$this->Form->control('name').
$this->Form->control('author').
$this->Form->control('name').
$this->Form->control('email', ['label' => 'Email ID']).
$this->Form->control('description', ['type' => 'textarea']).
$this->Form->submit('Submit', ['class' => 'btn btn-success']).
$this->Form->end() ?>

CSRF token requires that you let Cake know which fields to expect. Giving a sudden HTML is not what Cake would expect.

Questions? :slight_smile:

I do it same things in sample code but here is video show that he will create one form same above code then post to controller.

It’s shown this code

Please check and why he do successfully do the same code.

Did you try what I wrote?

There are tonnes of “incorrect” tutorials on the internet.

1 Like

yes, i tried your code how can apply $mytemplate.

Did it work?

Any errors?

If it worked mark this closed or post the error.

I have created form as you said as well as submit data successfully but how to style those elements.

That’s an entirely different question. Have you read the relevant part of the manual?

I wrote the code sample for you, showed you docs of templating… rtfm

1 Like

Thanks for your help it will give me right direction. Yes your code is working fine. Thanks :innocent:

You’re welcome :slight_smile: