Forums

Empty field when writing to database

rafaelscouto 27 Aug, 2019
Hello

I have a form that I use for both new entries and for editing them.

My question is in the Upload field.

In the editing event, when I don't fill the field, it updates and clears the record that already exists there.

How do I create a condition where, if the field is empty, do not update that column in the table in the database?
GreyHead 27 Aug, 2019
Hi rafaelscouto ,

I assume that this is ChronoForms and not ChronoConnectivity?

There is a FAQ on handling File Upload edits, please check that - basically you need to check if the new submission is empty and, if so, remove it from the form data before saving.

Bob
rafaelscouto 27 Aug, 2019
Good morning, I'm using the same CC. Will this FAQ work the same way?
GreyHead 27 Aug, 2019
Hi rafaelscouto,

Probably, it may be a bit more complicated, but the same principals should work OK.

Bob
Max_admin 02 Sep, 2019
You need to unset the field from the data provider before saving, you can use a PHP action:
$result = $this->data;
if(empty($result['field_name'])){
unset($result['field_name']);
}
return $result;
then use {var:php_name} as the data provider of your save data.

The code assumes that the data provider was initially {data:}
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Actucom 12 Nov, 2019
Hi everyone,
I am trying to do the same type of thing but I can't get it to work.
First I managed to unset the empty field. Then I used {var:php_name} as the data provider, but the "save data" still updates the record with an empty value.
Then I tried another approach. :I emptied the "gcb" array from {var:php_name}'s $result. The I tried to set the "Rules list" of the "Update conditions" by using  ...
id/in:{var:php_name:gcb}
... instead of the usual « id/in:{data:gcb} »
But it gives an SQL error : « 1064 SQL syntax error [...] near '''' at line 1 ».

How can I get the "Save data" to iterate through the "gcb" array of the PHP Data provider (instead of {data:gcb} ) ?
Thank you
Actucom 14 Nov, 2019
Hi. I managed not to update a column in the database table if the field is empty.
Based on Max's code I use a PHP function "php_name" for emptying the "gcb" array.[pre]$result = $this->data;
if(empty($result['field_name'])){
$result['gcb'] = array(0);
}
return $result;[/pre]
Then, into the "Save data", under "Update conditions", I use this rules list :
id/in:{var:php_name.gcb}
(no change to "Data provider" nor "Model name").[br]If there's a more direct way, I'd love to know it. Anyway, it works for me.
rafaelscouto 18 Nov, 2019
It worked for me.
I did a test with a password field, how can I from this code generate a hash?
GreyHead 19 Nov, 2019
Hi rafaelscouto,

There is PHP in the Joomla JUser class that can be used to manage password hashes.

Bob
rafaelscouto 19 Nov, 2019
Since I'm not a programmer, it gets harder for me.

I use this code to generate a password hash, but I don't know how to put the two codes together.

$password = $this->data["password"];
$pass = JUserHelper::hashPassword($password);
return $pass;
GreyHead 20 Nov, 2019
Hi rafaelscouto,

What exactly do you need to do? And is this using ChronoForms or ChronoConnectivity?

Bob
rafaelscouto 20 Nov, 2019
I'm using chronoconecivity

I use this code in an "edit" event. I have a form with multiple fields, and I need this "password" field not updated when nothing is filled in.
$result = $this->data;if(empty($result['password'])){unset($result['password']);}return $result;

With the above code solves the problem, but when I fill the field it does not create the hashPassword, then I tried to use the code below, but without success
$password = $this->data["password"];$pass = JUserHelper::hashPassword($password);return $pass;
GreyHead 21 Nov, 2019
Hi rafaelscouto,

You won't be saving the password itself so you can combine the two pieces of code in your 'if' statement and either return nothing or return the new hash.

Bob
rafaelscouto 21 Nov, 2019
My solution:
$password = $_POST["password"];
$pass = JUserHelper::hashPassword($password);

if(empty($_POST["password"])){
unset($_POST["password"]);
} else {
$this->data["password"] = $pass;
return $this->data;
}
This topic is locked and no more replies can be posted.