I am having some strange issues with a form in CakePHP.
I am trying to save data that I got from a form to the model Booking
. Some contact data, a message, a phone number and an email adress, standard stuff. The data is posted and I am trying to save it via
$this->Booking->save ($this->request->data);
But as I am trying to save, two fields don't pass the validation and stop the saving process:
phone_buyer
and carsign
.
The strange thing is that these two fields are not part of the form I am trying to save. They don't appear in $this->data
, as expected:
$this->request->data:
array(
'Booking' => array(
'question' => 'Lieber Vermieter,
ich würde gerne im angegebenen Zeitraum auf deinem Parkplatz parken. Ist das möglich? Ich freue mich auf deine Rückmeldung.
Liebe Grüße',
'start_date' => '30.10.2014',
'end_date' => '30.10.2014',
'sellerid' => '294',
'buyerid' => '301',
'parkid' => '10468',
'status' => 'Angebot',
'answer' => 'Blserntesn7x6 u fcgt c c ',
'ppemail_seller' => '*email*',
'phone_seller' => '0532732123',
'acceptrules' => '1',
'acceptrules2' => '1',
'id' => '1012'
)
)
...and they are not part of the form I am submitting, either. They are not even in any other form on the page. They do however exist as fields in the database and have validation rules in the model:
Booking.php (Model):
class Booking extends AppModel {
public $validate = array(
//(...)
'phone_seller' => array (
'rule' => array ('notEmpty'),
'message' => 'Damit dich der Mieter kontaktieren kann, brauchst du eine Telefonnummer.'
),
'ppemail_seller' => array (
'rule' => array ('notEmpty'),
'message' => 'Um das Geld der Miete zu empfangen, benötigst du ein PayPal-Konto.'
),
// These two are giving me headaches:
'carsign' => array(
'required' => array(
'rule' => array('notEmpty'),
'required' => false,
'message' => 'Du brauchst ein Autokennzeichen, um den Parkplatz zu buchen.'
)
),
'phone_buyer' => array(
'required' => array(
'rule' => array('notEmpty'),
'required' => false,
'message' => 'Damit dich der Vermieter kontaktieren kann, brauchst du eine Telefonnummer.'
)
)
);
}
But according to the documentation i have read until now, the rule notEmpty should not do anything if the corresponding field is not a part of the submitted form. To be sure, I added required=>false
, but they still throw errors at me.
I would be very glad for any help, thank you in advance. :)
Sorry if some things are hard to understand, english is not my native language.
Update:
Thanks for the suggestions, I found the problem. I wrote it in an answer below.
I found the problem.
The issue was that in some code before the save, $this->Booking->read();
had been used to get the contents of the database. Since that function overwrites data in the model (as I learned here), the data that I was trying to save was lost and replaced by contents of the database (in which these specific fields were missing). I replaced the function with $this->Booking->findById($id);
and everything worked fine.