1

I have some fields in Model, which i want to fill in controller (not give the user to input some data..). for example: curentUserId, or current date... I found 2 solutions.

echo $this->Form->input('delivered_by', array('type' => 'hidden', 'value'=> $_SESSION['id']));
  • make field hidden in view and give it a value

$_POST['data']['Sample']['delivered_by'] = 7777777;

  • give value in controller

  • if I comment in view, for example:

$this->Form->input('delivered_by');

I don't get field 'delivered_by' in data array in controller and i cant change it.

Is there any correct/wrong way? How do you others do that? Thank you, best regards,

these is how controller looks:

if ($this->request->is('post')) {


        //$this->request->data['coordinate_x'] = 7777777;   //=>>works with hidden field in view

        $this->Sample->create();
        $this->request->data['Sample']['delivered_by'] = 7;
        debug($_POST);

        if ($this->Sample->save($this->request->data)) {
            $this->Session->setFlash(__('The sample has been saved.'));
            //return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The sample could not be saved. Please, try again.'));
        }
    }

there is no change to 'delivered_by' variable, it stays blank;

array(
'_method' => 'POST',
'data' => array(
    'Sample' => array(
        'task_id' => '3',
        'delivered_by' => '',
        'date_of_delivery' => array(
            'month' => '09',
            'day' => '02',
            'year' => '2014'
        ),
        'sample_label' => '2',
        'coordinate_x' => '2',
        'coordinate_y' => '2',
        'coordinate_z' => ''
    )
)

)

3 Answers 3

1

you aren't using post to edit it with post , you can edit it in the controller like that :

$this->data['Sample']['delivered_by'] = 77777;
Sign up to request clarification or add additional context in comments.

3 Comments

i need to hide "echo $this->Form->input('delivered_by', array('type' => 'hidden'));" so i get these field in data array. and with $this->data['Sample']['delivered_by'] = 7; i cant update array, but with $_POST['data']['Sample']['delivered_by'] = 7; i change fields value (see it with debug($_POST);)
a question is:. can i somehow coment/remove that line from view, or it must be hidden so i get 'delivered_by' field in data array from view and than i can change that value?
now, after few hours of trying, i finnaly see that there are 2 arrays. one is POST and one is models array. i was watching POST array and there was not field 'delivered by', ofcourse.. tnx..
0

You don't need to send this data through POST. You already have it on the backend side.

You can implement such behaviors through callbacks on model when you don't have any outside dependency (like currentUserId).

When you have outside dependencies you should take your decisions on how best decouple it.

Comments

0
$sampleData = $this->request->data['Sample'];
$sampleData['delivered_by'] = 7;
if ($this->Sample->save($sampleData)) 
{
       $this->Session->setFlash(__('The sample has been saved.'));
       //return $this->redirect(array('action' => 'index'));
} 
else 
{
    $this->Session->setFlash(__('The sample could not be saved. Please, try again.'));
}

7 Comments

data array is not changable? but with my new variable I can do anything i want??
Yes you can change new variable but you cannot change this->data
Have you checked it using new variable?
i added now it and it works. BUT i need to have hidden: echo $this->Form->input('delivered_by', array('type' => 'hidden'));... if i remove this line from view in debug(); i dont see variable 'delivered_by'
If you remove from hidden field then you cant get in $this->data. Why are you remove this hidden field and what you want of this field?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.