Submit with hidden input with default NULL returns empty string

Notice: This thread is very old.
TomasHalasz
Bronze Partner | 79
+
0
-

Hi,

I have problem with NULL default value in hidden input. It seems that Nette 2.1.2 doesn't insert right value for NULL hidden input. INSERT command generated by Nette have instead of NULL only empty string '' and there is problem when column which should be NULL has foreign key for relation to another table.

I have these form:

protected function createComponentChatForm()
  {
	    $form = new Nette\Application\UI\Form;
	    $form->addHidden('users_id');
	    $form->addSubmit('send', 'messages.chat.send');
	    $form->onSuccess[] = $this->chatFormSucceeded;
	    return $form;
  }
  public function chatFormSucceeded($form)
  {
	    $_this =$this->presenter;
	    $values = $form->getValues();
	    dump($values);
  }

I have got this result:

Nette\ArrayHash #ea5b
users_id => ""

When I dump($values->users_id == NULL); I have got result “TRUE”.

When I made insert of $values to MySQL where is relationship on column ‘users_id’ I'v got error: ‘Integrity constraint violation’ because Nette tries this insert:

INSERT INTO `chat_messages` (`users_id`) VALUES ('')

Column users_id have definition:
users_id int(11) DEFAULT NULL

Have anyone idea where is mistake ? I made this workaround:

if ($values->users_id == NULL)
   $values->users_id = NULL;

But I would like to know what is wrong.

Thanks

BigCharlie
Member | 283
+
0
-

You've a mistake in your code. Do you know the difference between == and === operators? Have you tried this is_null($values->users_id)?

duke
Member | 650
+
+1
-

Assuming none of your users has id 0, you can use this:

$values->users_id = $values->users_id ?: NULL;

… which is equal to this:

if (!$values->users_id) {
	$values->users_id = NULL;
}

But if 0 is valid users_id, you will have to do this:

if ($values->users_id === '') {
	$values->users_id = NULL;
}
TomasHalasz
Bronze Partner | 79
+
0
-

Thanks guys. Yes I have mistake in compare users_id with NULL, but the main problem is that Nette made INSERT command which has

INSERT INTO `chat_messages` (`users_id`) VALUES ('')

instead

INSERT INTO `chat_messages` (`users_id`) VALUES (NULL)

or am I wrong when I expecting that Nette made solution for this automaticly (replace '' with NULL for insert command when users_id in form has default NULL value) ?

duke
Member | 650
+
0
-

When you submit your form, it will set '' value to your input. How can it know that by submitting empty string you don't actually mean empty string but NULL?

TomasHalasz
Bronze Partner | 79
+
0
-

Because of this is default behavior of hidden input:

addHidden($name, $default = NULL)

here in documentation

TomasHalasz
Bronze Partner | 79
+
0
-

and of course I'v tried

addHidden('users_id', NULL)

with same result…

duke
Member | 650
+
0
-

Thing is, if you do not specify default value of input, NULL will be used. So the value of NULL has a special meaning of “not specified” here (and when form is submitted, all its non-disabled inputs values are specified and thus cannot end up being NULL). It doesn't mean that if the empty content (such as empty string) is submitted that it will be automatically converted to NULL. To achieve such behavior, you are supposed to do the conversion manually.

Edit:

addHidden('users_id', NULL);

… is same as:

addHidden('users_id');

Last edited by duke (2014-04-26 19:00)

TomasHalasz
Bronze Partner | 79
+
0
-

Ok, thanks now I understand. I though that Nette is making more then it really does :-)

Last edited by TomasHalasz (2014-04-26 19:06)