Buy Now
Sign in

Empty field when writing to database

rafaelscouto , August 27 2019, 02:28
R
rafaelscouto 173
August 27 2019, 02:28 #389000
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 64
August 27 2019, 09:23 #389003
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
ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
R
rafaelscouto 173
August 27 2019, 09:29 #389004
Good morning, I'm using the same CC. Will this FAQ work the same way?
GreyHead 64
August 27 2019, 10:40 #389005
Hi rafaelscouto,
Probably, it may be a bit more complicated, but the same principals should work OK.
Bob
ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
R
rafaelscouto 173
August 29 2019, 19:42 #389037
I couldn't make it work for me.
admin 28
September 02 2019, 21:17 #389088
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
If your main question got answered then please mark the answer using the button!
Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!
A
Actucom 4
November 12 2019, 15:20 #389972
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
A
Actucom 4
November 14 2019, 20:16 #390005
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.
$result = $this->data;
if(empty($result['field_name'])){
$result['gcb'] = array(0);
}
return $result;
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").
If there's a more direct way, I'd love to know it. Anyway, it works for me.

R
rafaelscouto 173
November 18 2019, 13:50 #390027
It worked for me.
I did a test with a password field, how can I from this code generate a hash?
GreyHead 64
November 19 2019, 11:39 #390049
Hi rafaelscouto,
There is PHP in the Joomla JUser class that can be used to manage password hashes.
Bob
ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
R
rafaelscouto 173
November 19 2019, 15:52 #390051
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 64
November 20 2019, 10:45 #390054
Hi rafaelscouto,
What exactly do you need to do? And is this using ChronoForms or ChronoConnectivity?
Bob
ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
R
rafaelscouto 173
November 20 2019, 11:10 #390055
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 64
November 21 2019, 10:47 #390070
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
ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
R
rafaelscouto 173
November 21 2019, 14:23 #390072
My solution:
$password = $_POST["password"];
$pass = JUserHelper::hashPassword($password);

if(empty($_POST["password"])){
unset($_POST["password"]);
} else {
$this->data["password"] = $pass;
return $this->data;
}