Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'unlock' a field in a CakePHP form when it is part of a hasMany association

I have a form that represents a RewardModifier table in our database. That RewardModifier hasMany RewardOption.

My form is structured like this (image):

enter image description here

So, the RewardModifier can have many elements on the page, each with many RewardOption items.

The Problem

The problem is, that users can delete sections of this form using Javascript, which essentially removes it from the DOM. When they do that, it breaks the security component, because the POST'ed fields do not match the token supplied when the page was generated.

Now, I have been using unlockedFields to handle this before:

$this->Security->disabledFields = array(
   'PrjRewardModifier.reward_id',
   'PrjRewardModifier.title',
   'PrjRewardModifier.option_type',
   'PrjRewardOption.description',
   'PrjRewardOption.modifier',
   'PrjRewardOption.amount'
);

I know that disabledFields is deprecated, but we are using that for the time being.

When I debug the posted form data in the SecurityComponent, I see the following:

(int) 8 => 'PrjRewardModifier.0.reward_id',
(int) 9 => 'PrjRewardModifier.0.title',
(int) 10 => 'PrjRewardModifier.0.option_type',
(int) 11 => 'PrjRewardModifier.0.PrjRewardOption.0.description',
(int) 12 => 'PrjRewardModifier.0.PrjRewardOption.0.modifier',
(int) 13 => 'PrjRewardModifier.0.PrjRewardOption.0.amount'

I need to know how to edit the data being passed to unlockedFields so that it can disregard these fields that are keyed for hasMany relationships.

Thanks.

like image 570
Barry Chapman Avatar asked Nov 22 '25 11:11

Barry Chapman


2 Answers

I had a similar problem. I found adding (the equivalent of) this to the RewardModifier controller did the trick:

public function beforeFilter(){
     $this->Security->unlockedFields = array('RewardOption');
}
like image 173
Will Stone Avatar answered Nov 25 '25 04:11

Will Stone


Adding the following to the form code worked for me

$this->Form->unlockField('User.id');

Unlocking the fields from within the view files also helps declutter the controller's beforeFilter().

Source: /core-libraries/helpers/form.html#FormHelper::unlockField

like image 37
mehov Avatar answered Nov 25 '25 03:11

mehov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!