PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 16, 2022

[FIXED] CakePHP 3 Unable to emit headers when using associated data in control form

 January 16, 2022     cakephp, controls, forms     No comments   

Issue

I'm using CakePHP 3.6.7 and my problem is that when I use associated data in control form I got the unable to emit headers warning.

Here's my code:

AppController.php initialize function (If the user is logged in, i retrieve all his data from here)

public function initialize() {

parent::initialize();

// Retrieve user data for all views
if($this->request->getSession()->read('Auth.User')) {
    $this->user = $this->Users->get($this->request->getSession()->read('Auth.User.id'), [
        'contain' => ['UserPersonalData']
    ]); 
    $this->set('user', $this->user);
    $this->set('_serialize', 'user');
}

}

UserPersonalDataController.php initialize function

public function initialize()
{
    parent::initialize();

}

UserPersonalDataController.php edit function

public function edit()
{
    if ($this->request->is(['patch', 'post', 'put'])) {
        $userPersonalData = $this->Users->patchEntity($this->user, $this->request->getData());
        if ($this->Users->save($userPersonalData)) {
            $this->Flash->success(__('The user personal data has been saved.'));

            return $this->redirect($this->user->username);
        }
        $this->Flash->error(__('The user personal data could not be saved. Please, try again.'));
    }
}

edit.ctp, the following form will throw the unable to emit headers warning in debug mode

<div class="container" style="padding: 2rem">
<div class="users form large-9 medium-8 columns content">
    <?= $this->Form->create($user, [
        'class' => 'form-signin',
    ]); ?>
    <fieldset>
        <legend><?= __('Edit User') ?></legend>
        <?php
            echo $this->Form->control('user_personal_data.firstName', ['required' => true, 'class' => 'form-control', 'placeholder' => 'First Name', 'label' => false]);
            echo $this->Form->control('user_personal_data.lastName', ['required' => true, 'class' => 'form-control', 'placeholder' => 'Last Name', 'label' => false]);
            echo $this->Form->control('user_personal_data.phone', ['required' => true, 'class' => 'form-control', 'placeholder' => 'Phone', 'label' => false]);
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit'), ['class' => 'btn btn-lg btn-primary btn-block' ]) ?>
    <?= $this->Form->end() ?>
</div>

edit.ctp, the following form will not throw any warnings

<div class="container" style="padding: 2rem">
<div class="users form large-9 medium-8 columns content">
    <?= $this->Form->create($user, [
        'class' => 'form-signin',
    ]); ?>
    <fieldset>
        <legend><?= __('Edit User') ?></legend>
        <?php
            echo $this->Form->control('email', ['required' => true, 'class' => 'form-control', 'placeholder' => 'First Name', 'label' => false]);
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit'), ['class' => 'btn btn-lg btn-primary btn-block' ]) ?>
    <?= $this->Form->end() ?>
</div>

As I said, if I use associated data in control() form helper, it will throw that warning in debug mode. If I'm not in debug mode it will work just fine but I'm worried about it. Thanks for the help!

EDIT: Here's a screenshot of the warning: enter image description here


Solution

The problem is that, as Greg Schmidt said, I had a non well formated numeric value. Solved!



Answered By - Kigris
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing