PDA

View Full Version : How to prevent to insert in db only the mask if lineedit no filled



juliano.gomes
14th June 2015, 21:23
hello buddies,
I have 2 lineedits with mask, one with phone mask an another with birthday mask.
these two fields aren't required fields, so how can i prevent to insert in db only the mask when the user do not fill these fields?

I tryed this before my insertRecord, but do not work:



if (ui->leBirth->text() == " / / ")
{
ui->leBirth->clearMask();
ui->leBirth->clear();
}

if (ui->lePhone->text() == "( ) - ")
{
ui->lePhone->clearMask();
ui->lePhone->clear();
}


Thanks a lot.
Juliano

anda_skoa
20th June 2015, 12:32
Not sure why you posted code that you use to clear the widgets.

How do you process the values before insertion into the database?

Cheers,
_

ChrisW67
21st June 2015, 00:26
An empty line edit with a mask returns the mask with optional characters removed.


QLineEdit l;
l.setInputMask("00/00/0000;_");
// Displays "__/__/____" in the edit edit box
qDebug() << l.text();
// Output "//" not "00/00/0000"

l.setInputMask("(000) 000-0000;_");
// Displays "(___) ___-____" in the edit edit box
qDebug() << l.text();
// Output "() -" not "(000) 000-0000"

Neither mask guarantees a valid value even if one is provided.

BTW: QWidget::clearMask() has nothing to do with the line edit's input mask.

juliano.gomes
24th June 2015, 15:34
Ok, solved.

anda_skoa, The ui->object->clear() was an desperate act (kkk) when the clearmask not worked, sorry, my bad.

In my original code, i was setted the input mask in the form, in this format: phone: (xx)xxxx-xxxx, date: xx/xx/xxxx (and the clearMask() do not worked on code)

so i had to change my approach. I set up the mask in my form initialization as the ChrisW67 sugestion:


ui->leTelefone->setInputMask("(00)0000-0000;_");
ui->leAniversaio->setInputMask("00/00/0000;_");

And before insert or update the data in database, i made this verification:


if (ui->leTelefone->text() == "()-")
ui->leTelefone->setInputMask(""); // prevent to insert only the mask in database

if (ui->leAniversaio->text() == "//")
ui->leAniversaio->setInputMask("");

I don't know if this is the correct approach, but it worked!
thanks a lot!
Juliano

anda_skoa
24th June 2015, 16:00
I wonder why you change the mask instead of just not taking the value for insert?

Cheers,
_

juliano.gomes
24th June 2015, 17:24
because my mask was not setted on qlineedit keypress event (is this a good idea?). My masks was setted in project time, in the qlineedit property, in the form.

In this form, i have 3 fields, name, phone, birthday. Only the name field is required, so if the user fill only the name field, the qlineedit masks are saved in database. maybe my inputMask format was wrong? (in that format xx/xx/xxxx and (xx)xxxx-xxxx)

I have developing in others languages for some years, but my experience with C++ an Qt is only beginning, and i hope to begin in right way, so i apologies for my mistakes and thank for your comprehension.

Juliano

anda_skoa
24th June 2015, 21:33
My masks was setted in project time, in the qlineedit property, in the form.

Make sense, it probably doesn't need to change during runtime.

I am asking why you are clearing the input mask as it as nothing to do with the question you have been asking.

ChrisW67 showed you how to detect empty fields despite the mask, but instead of using that to detect when not to use the value you change the mask?

Cheers,
_