Re: Re: Re[2]: [PEAR] [QF] - setConstants() messign up elements value?
| From: | Sebastian Baran | Date: | Thu, 29 Jan 2004 13:41:21 +0000 |
| Subject: | Re: Re: Re[2]: [PEAR] [QF] - setConstants() messign up elements value? | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-10424@lists.php.net to get a copy of this message | ||
SpamPal marked previous Bertrand Mansion's answer as spam and I just
hit reply with such subject. Oops. ;)
Thursday, January 29, 2004, 2:08:00 PM, you wrote:
BM> <Sebastian.Baran@dialcom.com.pl> wrote :
>>
>>
>> Thursday, January 29, 2004, 1:32:24 PM, you wrote:
>>
>> BM> <Sebastian.Baran@dialcom.com.pl> wrote :
>>
>>>> Hi all,
>>>>
>>>>
>>>> I have the following (pseudo)code:
>>>>
>>>> [..]
>>>>
>>>> $sform->addElement('text', '_date', 'Date: ',
>>>> '');
>>>> $sform->setDefaults(array('_date'=>date('d.m.Y')));
>>>>
>>>> [..]
>>>>
>>>> if ($sform->validate()) {
>>>>
>>>> [..]
>>>>
>>>> // convert dd.mm.YYYY to YYYYmmdd format to put it into DB
>>>> $sform->applyFilter('_date', 'prepare_date');
>>>>
>>>> if ($add_mode) {
>>>> echo '1: '.$sform->getElementValue('_date');
>>>>
>>>> [put data into DB]
>>>> $id = mysql_insert_id($db->connection);
>>>>
>>>> $sform->setConstants(array("id"=>$id));
>>>>
>>>> echo '2: '.$sform->getElementValue('_date');
>>>> }
>>>> elseif ($edit_mode) {
>>>> [update record in DB]
>>>> }
>>>> }
>>>>
>>>> [..]
>>>>
>>>>
>>>> Everything's working fine except with '_date' field.
>>>>
>>>> 1) open page with form to add new record
>>>> 2) submit with correct values
>>>> 3) above code outputs:
>>>>
>>>> 1: 29.01.2004
>>>> 2: 20040129
>>>>
>>>> Why does seConstants() for "id" field change "_date" field
>>>> value?????
>>>> And more why, though getElementValue()
>>>>
>>>> "Returns the element's raw value such as submitted by the form (not
>>>> filtered), set by setDefaults() or setConstants(). "
>>>> (quoted from QF docs) I get filtered value of "_date" field, but only
>>>> after setConstants() even not touching "_date" field?
>>>>
>>>>
>>>> I'm confused. Any help appreciated.
>>
>> BM> setConstants() will trigger the 'updateValue' event in each form elements,
>> BM> that is the reason why the value is modified because it has been filtered
>> BM> beforehand.
>>
>> Thank you. Now I see. I didn't know that. I guess setDefaults()
>> triggers the same behavior.
>>
>> BM> Normally, you use setConstants() before your form is validated.
>> BM> In your case, I think there is a logic problem. I don't see why you would
>> BM> use setConstants() here.
>>
>> Yes, I usually setConstants() before form validation. But I do it here
>> cause I want user to be able to edit record after adding. So I need
>> recently added record ID set in form. If this is not correct way, I'll
>> be grateful if you tell me how to do it better. I'd prefer to not
>> redirect user back to list after inserting/updating record. Am I wrong
>> here?
>>
>> I'm new with QF (but not with PHP ;)) so I'll appreciate every advise
>> how to use it better.
BM> You could change your date value without using quickform filter but using
BM> the mysql date_format function (IIRC) or something like $values =
$form->>exportValues(); $values['_date'] = prepare_date($values['_date']);
That's the way I usually do it (export+substitute with prepared date).
But this time I'm doing something like configurable form so I wanted
to have INSERT/UPDATE part configurable too so such custom treatment
is not good here.
BM> Or maybe you could use another form for your edit form and set its constants
BM> as you want.
I'll try to do like this: after DB INSERT I'll redirect to the same
page with last inserted ID as parameter so user won't see anything but
form will be in "edit mode".
Thanks a lot for your help :-)
QF is great :-)
p.s.
Maybe it would be good idea to put note about
setConstants()/setDefaults() triggering the 'updateValue' into docs.
p.s.2
I always forget to do "Reply All" instead of "Reply" ;) Sorry.
--
Best regards,
Sebastian