Random loose of data from Nette formular
- peto.v.
- Member | 2
Sometimes happens that the recieved data from the form is different from $_POST array. When I tried to reproduce this situation, the data was correct.
I analyzed the exception file where this bug was catched. I found out that the $_POST data was OK. The parameter of the handler ($form) contained the correct data:
httpData private => array(5) ▼ {
note => "úhrada 10.3.2014" (27)
id_credit_invoice_note_flag => "4"
note_end_date => ""
save => "Uložiť" (8)
_token_ => "****" (10)
}
The 3rd param of method saveCreditControlNote() does not recieved the proper value (“4”), but a NULL… This means that after getting the form values by method $form->getValues() key “id_credit_invoice_note_flag” was ignored and the value was replaced by NULL. The value for the key “note” was correct.
In a similar case the whole $_POST request was ignored and NULL values were returned from method $form->getValues(). The structure of POST request was correct, only the passed values were ignored…
Template form code:
{widget noteForm}
Component code:
$form = new \Nette\Application\UI\Form($this, 'noteForm');
$form->addProtection();
$finance = new \FinanceModel;
$namespace = \Nette\Environment::getSession('credit_control');
$form->addGroup($this->translate->_('Add note'));
$form->addTextArea('note', $this->translate->_('Note'), 30, 5)
->addRule(\Nette\Application\UI\Form::FILLED, $this->translate->_('Input note is required. '));
$form->addSelect('id_credit_invoice_note_flag', $this->translate->_('Type'), $finance->getCreditInvoiceNoteFlagSelectBox($namespace->id_company));
$form->addText('note_end_date', $this->translate->_('End note'));
$form->addSubmit('save', $this->translate->_('Save'));
$form->onSuccess[] = array($this, 'noteFormSubmitHandler');
return $form;
Handler code:
public function noteFormSubmitHandler($form)
{
if( $form['save']->isSubmittedBy() )
{
$form_value = $form->getValues();
// ...
$ret = $finance->saveCreditControlNote($namespace->id_company, $form_value['note'], $form_value['id_credit_invoice_note_flag'], $save_note_end_date);
}
$this->redirect('this');
}
Can anyone suggest a solution? Thanks for your answers.
- petr.pavel
- Member | 535
What do you do with $form_value before calling saveCreditControlNote() – could you be nulling the value there?
Also, do you set default values for the form?
Try logging $form->values in different stages of presenter and the form's life cycle. Then when the error occurs, you'll have more info to work with.
- peto.v.
- Member | 2
I checked, no operations are made to values in $form_value, they are just passed to method saveCreditControlNote(). I am not setting defaults for this form fields.
It is strange, because it is happening randomly, so it is almost impossible to reproduce this bug… When I tried to do so, everything was OK.
I am going to add logging, but the exception file is a good logger, too :)
As I describe earlier (in this post), I saw the POST array, and the Form
object that was passed to handler method. Everything was all right, data was
correct, till the $form->getValues()
method was called… After
that the magic behind this method decided to modified the form
submit array.
I know that this method do some magic in the background to prevent hacker attacts (or something like that), eg.: ignore fields that were not created by Nette Form. But in this case there is no reason to do any magic…
@petr.pavel, thaks for your advices.