0
votes

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.

2
So where do you set the phone_buyer and carsign; Do you set it at your controller action and you want the save to stop if something went wrong; - gmponos
phone_buyer and carsign are set in another action and don't appear in this controller action or view. The save fails since the model tries to save these fields, which have no value. - schnüt

2 Answers

0
votes

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.

0
votes

Have you tried with the 'allowEmpty' key?

'phone_buyer' => array( 
    'required' => false,
    'allowEmpty' => false,
    'message' => 'Damit dich der Vermieter...'
)