Message from discussion
How to properly user JFormFieldFile???
Date: Sun, 25 Mar 2012 05:41:25 -0700 (PDT)
From: elin <elin.war...@gmail.com>
To: joomla-dev-cms@googlegroups.com
Message-ID: <12788280.83.1332679285691.JavaMail.geo-discussion-forums@vbat19>
In-Reply-To: <CABYB9+APvn3ujS+AZvnp04eVP5FCn2u2hONxXyk0rnq2KjkA+Q@mail.gmail.com>
References: <10052903.1387.1332517162010.JavaMail.geo-discussion-forums@vbut24>
<20089811.1745.1332538024443.JavaMail.geo-discussion-forums@vbxq27>
<28234284.147.1332589635515.JavaMail.geo-discussion-forums@yneo2> <14299204.354.1332615764190.JavaMail.geo-discussion-forums@vbhy1>
<CABYB9+APvn3ujS+AZvnp04eVP5FCn2u2hONxXyk0rnq2KjkA+Q@mail.gmail.com>
Subject: Re: [jcms] Re: How to properly user JFormFieldFile???
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_81_6152849.1332679285687"
------=_Part_81_6152849.1332679285687
Content-Type: multipart/alternative;
boundary="----=_Part_82_29733976.1332679285687"
------=_Part_82_29733976.1332679285687
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Required is not working because it assumes that you are going to save=20
something from $data in a field.=20
https://github.com/joomla/joomla-platform/blob/staging/libraries/joomla/for=
m/form.php#L1847
So the issue I think is in how required is attempting to validate in this=
=20
case. It needs to check $_FILES not $data.
If the field is not going to save something in a manner typical of other=
=20
fields the required is going to fail unless it is overriding that. =20
https://github.com/joomla/joomla-platform/blob/staging/libraries/joomla/for=
m/fields/file.php#L54
So ... possibly the field needs to handle validation for required itself.=
=20
Or you could make a custom rule that handles file validation including=20
dealing correctly with required.
=20
Elin=20
On Saturday, Marc)odel when you save
>
> to shift the file on disk somewhere.htCheers,
>
> Sam Moffatt
> http://pasamio.id.au
>
> On Sun, Mar 25, 2012 at 5:02 AM, elin <elin.war...@gmail.com> wrote:
> > I have to say that field has always been a mystery to me. It has never=
=20
> saved
> > and I guess you have figured out why but it's not clear what it should=
=20
> even
> > save. I think of it more like the field in the installer that you use t=
o
> > find the zip file but you don't ever actually store that information.
> > Usually the fields like the media or imagelist or filelist fields wou=
ld
> > just save the reference to the name and location of the file.
> >
> > I have always thought that what you should do is extend it to make a=20
> custom
> > field that is actually useful for the file types you want.
> >
> > Elin
> >
> >
> >> Thanks for the reply Elin. Of course I can share a little bit of code:
> >>
> >> so here is the form:
> >>
> >> <?xml version=3D"1.0" encoding=3D"utf-8"?>
> >> <form>
> >> <fieldset>
> >> <field name=3D"hotspots_kml_id" type=3D"text" default=3D"0"
> >> label=3D"JGLOBAL_FIELD_ID_LABEL"
> >> readonly=3D"true" class=3D"readonly"
> >> description=3D"JGLOBAL_FIELD_ID_=E2=80=8BDESC"/>
> >>
> >>
> >> ....
> >>
> >>
> >> <field name=3D"kml_file" type=3D"file" label=3D"COM_HOTSPOTS_K=
ML_FILE"
> >> description=3D"COM_HOTSPOTS_KML_=E2=80=8BFILE_DESC"=20
> required=3D"true"/>
> >>
> >>
> >> ....
> >>
> >>
> >> </fieldset>
> >>
> >> </form>
> >>
> >> my edit.php layout file:
> >>
> >> <form enctype=3D"multipart/form-data" action=3D"<?php echo
> >> JRoute::_('index.php?option=3D=E2=80=8Bcom_hotspots&view=3Dkml&=E2=80=
=8Bhotspots_kml_id=3D' .
> >> (int)$this->item->hotspots_=E2=80=8Bkml_id); ?>" method=3D"post" class=
=3D"form"
> >> name=3D"adminForm" id=3D"adminForm" >
> >>
> >> <div class=3D"width-60 fltlft">
> >> <fieldset class=3D"adminform">
> >> <legend><?php echo empty($this->item->hotspots_=E2=80=8Bkm=
l_id) ?
> >> JText::_('COM_HOTSPOTS_NEW_=E2=80=8BKML') :=20
> JText::sprintf('COM_HOTSPOTS_=E2=80=8BEDIT_KML',
> >> $this->item->hotspots_kml_id); ?></legend>
> >>
> >> <ul class=3D"adminformlist">
> >> ....
> >> <li>
> >> <?php echo=20
> $this->form->getLabel('=E2=80=8Bhotspots_kml_id');
> >> ?>
> >> <?php echo=20
> $this->form->getInput('=E2=80=8Bhotspots_kml_id');
> >> ?>
> >> </li>
> >> ....
> >>
> >> <li>
> >> <?php echo $this->form->getLabel('kml_=E2=80=8Bfil=
e'); ?>
> >> <?php echo $this->form->getInput('kml_=E2=80=8Bfil=
e'); ?>
> >>
> >> </li>
> >> </ul>
> >>
> >> .....
> >>
> >> <input type=3D"hidden" name=3D"task" value=3D""/>
> >> <?php echo JHTML::_('form.token'); ?>
> >> </form>
> >>
> >> So when you click on save, we go in the kml controller that extends
> >> JControllerForm:
> >> class HotspotsControllerKml extends JControllerForm {
> >> ....
> >> }
> >>
> >> If I don't override the save function, then we use the parent function
> >> which looks like this:
> >>
> >> public function save($key =3D null, $urlVar =3D null)
> >> {
> >>
> >> // Check for request forgeries.
> >> JRequest::checkToken() or jexit(JText::_('JINVALID_=E2=80=8BTO=
KEN'));
> >>
> >>
> >> // Initialise variables.
> >> $app =3D JFactory::getApplication();
> >> $lang =3D JFactory::getLanguage();
> >> $model =3D $this->getModel();
> >> $table =3D $model->getTable();
> >> $data =3D JRequest::getVar('jform', array(), 'post', 'array');
> >> $checkin =3D property_exists($table, 'checked_out');
> >> $context =3D "$this->option.edit.$this->=E2=80=8Bcontext";
> >> $task =3D $this->getTask();
> >>
> >> .....
> >>
> >>
> >>
> >> // Validate the posted data.
> >> // Sometimes the form needs some posted data, such as for=20
> plugins
> >> and modules.
> >> $form =3D $model->getForm($data, false);
> >>
> >> if (!$form)
> >> {
> >> $app->enqueueMessage($model->=E2=80=8BgetError(), 'error')=
;
> >>
> >> return false;
> >> }
> >>
> >> // Test whether the data is valid.
> >> $validData =3D $model->validate($form, $data);
> >>
> >> .....
> >>
> >> if ($validData =3D=3D=3D false)
> >> {
> >> ....
> >>
> >> }
> >>
> >> As you can see in this function we do:
> >> $data =3D JRequest::getVar('jform', array(), 'post', 'array');
> >>
> >> and this is all ok, but the file is not sent in the post array, but it=
=20
> is
> >> in the $_FILES array.
> >>
> >> And because of this the validation in the model fails.
> >>
> >> So as I said, I overwrote the save function and added a=20
> $data['kml_file']
> >> =3D true; if the user has submitted a file.
> >>
> >> The question is - should I overwrite this function? Or should I do the
> >> checks in the validate function?
> >>
> >> Since the validate function expects to receive the $form and the $data=
I
> >> think that we should not do any interactions with the $_POST and $_FIL=
ES
> >> arrays there, right?
> >>
> >> Am I doing something wrong here? Is there a better approach to this???
> >>
> >> Thanks in advance!
> >> Daniel
> >>
> >> On Friday, March 23, 2012 10:27:04 PM UTC+1, elin wrote:
> >>>
> >>> I'm not sure I understand what is happening. Maybe you could show us=
=20
> your
> >>> code or xml?
> >>>
> >>>
> >>> Are you saying that the field has been filled out but the validation =
is
> >>> saying it is empty?
> >>>
> >>>
> >>> Elin
> >>>
> >>>
> >>> On Friday, March 23, 2012 11:39:22 AM UTC-4, Daniel D. / compojoom=20
> wrote:
> >>>>
> >>>> Hey Guys,
> >>>> For the last hour I've been trying to figure out how to use a
> >>>> JFormFieldFile field that is set to required.
> >>>>
> >>>> The thing is when I save the form data, the save function complains=
=20
> that
> >>>> the file field is empty and because it is required -> the validation
> >>>> function returns false...
> >>>>
> >>>> I went around this by overriding the whole save function in the
> >>>> JControllerForm. (I just checked if the $_Files array contains the=
=20
> file and
> >>>> added it to the $data array.) But this approach feels really stupid=
=20
> for
> >>>> me??? I basically have the same save function + 3 lines that handle=
=20
> the file
> >>>> field.
> >>>>
> >>>> Is there any other way to let JForm know that we have a field that i=
s
> >>>> not in the post array? Preprocess form in the controller or somethin=
g?
> >>>>
> >>>> Regards,
> >>>> Daniel
> >
> > --
> > You received this message because you are subscribed to the Google Grou=
ps
> > "Joomla! CMS Development" group.
> > To view this discussion on the web, visit
> > https://groups.google.com/d/=E2=80=8Bmsg/joomla-dev-cms/-/=E2=80=8BNc8I=
pWwUHz8J<https://groups.google.com/d/msg/joomla-dev-cms/-/Nc8IpWwUHz8J>
> .
> >
> > To post to this group, send an email to joomla-dev-cms@googlegroups.=E2=
=80=8Bcom<joomla-dev-cms@googlegroups.com>
> .
> > To unsubscribe from this group, send email to
> > joomla-dev-cms+unsubscribe@=E2=80=8Bgooglegroups.com<joomla-dev-cms%2Bu=
nsubscribe@googlegroups.com>
> .
> > For more options, visit this group at
> > http://groups.google.com/=E2=80=8Bgroup/joomla-dev-cms?hl=3Den-GB<http:=
//groups.google.com/group/joomla-dev-cms?hl=3Den-GB>
> .
>
>
On Saturday, March 24, 2012 10:30:58 PM UTC-4, Samuel Moffatt wrote:
>
> You have to write something in your model to process and save it. This
> is primarily a PHP side effect that it'll be jammed into $_FILES and
> you have to use move_uploaded_file (or JFile::upload) to shift it
> somewhere on disk where you want to use it.
>
> No idea why the required thing isn't working, possibly a bug but you
> shouldn't need to subclass, just pick it up in the model when you save
> to shift the file on disk somewhere.
>
> Cheers,
>
> Sam Moffatt
> http://pasamio.id.au
>
> On Sun, Mar 25, 2012 at 5:02 AM, elin <elin.war...@gmail.com> wrote:
> > I have to say that field has always been a mystery to me. It has never=
=20
> saved
> > and I guess you have figured out why but it's not clear what it should=
=20
> even
> > save. I think of it more like the field in the installer that you use t=
o
> > find the zip file but you don't ever actually store that information.
> > Usually the fields like the media or imagelist or filelist fields wou=
ld
> > just save the reference to the name and location of the file.
> >
> > I have always thought that what you should do is extend it to make a=20
> custom
> > field that is actually useful for the file types you want.
> >
> > Elin
> >
> >
> >> Thanks for the reply Elin. Of course I can share a little bit of code:
> >>
> >> so here is the form:
> >>
> >> <?xml version=3D"1.0" encoding=3D"utf-8"?>
> >> <form>
> >> <fieldset>
> >> <field name=3D"hotspots_kml_id" type=3D"text" default=3D"0"
> >> label=3D"JGLOBAL_FIELD_ID_LABEL"
> >> readonly=3D"true" class=3D"readonly"
> >> description=3D"JGLOBAL_FIELD_ID_=E2=80=8BDESC"/>
> >>
> >>
> >> ....
> >>
> >>
> >> <field name=3D"kml_file" type=3D"file" label=3D"COM_HOTSPOTS_K=
ML_FILE"
> >> description=3D"COM_HOTSPOTS_KML_=E2=80=8BFILE_DESC"=20
> required=3D"true"/>
> >>
> >>
> >> ....
> >>
> >>
> >> </fieldset>
> >>
> >> </form>
> >>
> >> my edit.php layout file:
> >>
> >> <form enctype=3D"multipart/form-data" action=3D"<?php echo
> >> JRoute::_('index.php?option=3D=E2=80=8Bcom_hotspots&view=3Dkml&=E2=80=
=8Bhotspots_kml_id=3D' .
> >> (int)$this->item->hotspots_=E2=80=8Bkml_id); ?>" method=3D"post" class=
=3D"form"
> >> name=3D"adminForm" id=3D"adminForm" >
> >>
> >> <div class=3D"width-60 fltlft">
> >> <fieldset class=3D"adminform">
> >> <legend><?php echo empty($this->item->hotspots_=E2=80=8Bkm=
l_id) ?
> >> JText::_('COM_HOTSPOTS_NEW_=E2=80=8BKML') :=20
> JText::sprintf('COM_HOTSPOTS_=E2=80=8BEDIT_KML',
> >> $this->item->hotspots_kml_id); ?></legend>
> >>
> >> <ul class=3D"adminformlist">
> >> ....
> >> <li>
> >> <?php echo=20
> $this->form->getLabel('=E2=80=8Bhotspots_kml_id');
> >> ?>
> >> <?php echo=20
> $this->form->getInput('=E2=80=8Bhotspots_kml_id');
> >> ?>
> >> </li>
> >> ....
> >>
> >> <li>
> >> <?php echo $this->form->getLabel('kml_=E2=80=8Bfil=
e'); ?>
> >> <?php echo $this->form->getInput('kml_=E2=80=8Bfil=
e'); ?>
> >>
> >> </li>
> >> </ul>
> >>
> >> .....
> >>
> >> <input type=3D"hidden" name=3D"task" value=3D""/>
> >> <?php echo JHTML::_('form.token'); ?>
> >> </form>
> >>
> >> So when you click on save, we go in the kml controller that extends
> >> JControllerForm:
> >> class HotspotsControllerKml extends JControllerForm {
> >> ....
> >> }
> >>
> >> If I don't override the save function, then we use the parent function
> >> which looks like this:
> >>
> >> public function save($key =3D null, $urlVar =3D null)
> >> {
> >>
> >> // Check for request forgeries.
> >> JRequest::checkToken() or jexit(JText::_('JINVALID_=E2=80=8BTO=
KEN'));
> >>
> >>
> >> // Initialise variables.
> >> $app =3D JFactory::getApplication();
> >> $lang =3D JFactory::getLanguage();
> >> $model =3D $this->getModel();
> >> $table =3D $model->getTable();
> >> $data =3D JRequest::getVar('jform', array(), 'post', 'array');
> >> $checkin =3D property_exists($table, 'checked_out');
> >> $context =3D "$this->option.edit.$this->=E2=80=8Bcontext";
> >> $task =3D $this->getTask();
> >>
> >> .....
> >>
> >>
> >>
> >> // Validate the posted data.
> >> // Sometimes the form needs some posted data, such as for=20
> plugins
> >> and modules.
> >> $form =3D $model->getForm($data, false);
> >>
> >> if (!$form)
> >> {
> >> $app->enqueueMessage($model->=E2=80=8BgetError(), 'error')=
;
> >>
> >> return false;
> >> }
> >>
> >> // Test whether the data is valid.
> >> $validData =3D $model->validate($form, $data);
> >>
> >> .....
> >>
> >> if ($validData =3D=3D=3D false)
> >> {
> >> ....
> >>
> >> }
> >>
> >> As you can see in this function we do:
> >> $data =3D JRequest::getVar('jform', array(), 'post', 'array');
> >>
> >> and this is all ok, but the file is not sent in the post array, but it=
=20
> is
> >> in the $_FILES array.
> >>
> >> And because of this the validation in the model fails.
> >>
> >> So as I said, I overwrote the save function and added a=20
> $data['kml_file']
> >> =3D true; if the user has submitted a file.
> >>
> >> The question is - should I overwrite this function? Or should I do the
> >> checks in the validate function?
> >>
> >> Since the validate function expects to receive the $form and the $data=
I
> >> think that we should not do any interactions with the $_POST and $_FIL=
ES
> >> arrays there, right?
> >>
> >> Am I doing something wrong here? Is there a better approach to this???
> >>
> >> Thanks in advance!
> >> Daniel
> >>
> >> On Friday, March 23, 2012 10:27:04 PM UTC+1, elin wrote:
> >>>
> >>> I'm not sure I understand what is happening. Maybe you could show us=
=20
> your
> >>> code or xml?
> >>>
> >>>
> >>> Are you saying that the field has been filled out but the validation =
is
> >>> saying it is empty?
> >>>
> >>>
> >>> Elin
> >>>
> >>>
> >>> On Friday, March 23, 2012 11:39:22 AM UTC-4, Daniel D. / compojoom=20
> wrote:
> >>>>
> >>>> Hey Guys,
> >>>> For the last hour I've been trying to figure out how to use a
> >>>> JFormFieldFile field that is set to required.
> >>>>
> >>>> The thing is when I save the form data, the save function complains=
=20
> that
> >>>> the file field is empty and because it is required -> the validation
> >>>> function returns false...
> >>>>
> >>>> I went around this by overriding the whole save function in the
> >>>> JControllerForm. (I just checked if the $_Files array contains the=
=20
> file and
> >>>> added it to the $data array.) But this approach feels really stupid=
=20
> for
> >>>> me??? I basically have the same save function + 3 lines that handle=
=20
> the file
> >>>> field.
> >>>>
> >>>> Is there any other way to let JForm know that we have a field that i=
s
> >>>> not in the post array? Preprocess form in the controller or somethin=
g?
> >>>>
> >>>> Regards,
> >>>> Daniel
> >
> > --
> > You received this message because you are subscribed to the Google Grou=
ps
> > "Joomla! CMS Development" group.
> > To view this discussion on the web, visit
> > https://groups.google.com/d/=E2=80=8Bmsg/joomla-dev-cms/-/=E2=80=8BNc8I=
pWwUHz8J<https://groups.google.com/d/msg/joomla-dev-cms/-/Nc8IpWwUHz8J>
> .
> >
> > To post to this group, send an email to joomla-dev-cms@googlegroups.=E2=
=80=8Bcom<joomla-dev-cms@googlegroups.com>
> .
> > To unsubscribe from this group, send email to
> > joomla-dev-cms+unsubscribe@=E2=80=8Bgooglegroups.com<joomla-dev-cms%2Bu=
nsubscribe@googlegroups.com>
> .
> > For more options, visit this group at
> > http://groups.google.com/=E2=80=8Bgroup/joomla-dev-cms?hl=3Den-GB<http:=
//groups.google.com/group/joomla-dev-cms?hl=3Den-GB>
> .
>
>
------=_Part_82_29733976.1332679285687
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<br><div>Required is not working because it assumes that you are going to s=
ave something from $data in a field. </div><div><a href=3D"https://git=
hub.com/joomla/joomla-platform/blob/staging/libraries/joomla/form/form.php#=
L1847">https://github.com/joomla/joomla-platform/blob/staging/libraries/joo=
mla/form/form.php#L1847</a></div><div>So the issue I think is in how requir=
ed is attempting to validate in this case. It needs to check $_=
FILES not $data.</div><div><br></div><div>If the field is not going t=
o save something in a manner typical of other fields the required is going =
to fail unless it is overriding that. </div><div><a href=3D"htt=
ps://github.com/joomla/joomla-platform/blob/staging/libraries/joomla/form/f=
ields/file.php#L54">https://github.com/joomla/joomla-platform/blob/staging/=
libraries/joomla/form/fields/file.php#L54</a><br></div><div><br></div><div>=
So ... possibly the field needs to handle validation for required itself. O=
r you could make a custom rule that handles file validation including deali=
ng correctly with required.</div><div><br></div><div> </div><div>Elin&=
nbsp;<br><br>On Saturday, Marc)odel when you save<blockquote class=3D"gmail=
_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;p=
adding-left: 1ex;"><p>to shift the file on disk somewhere.htCheers,</p><p>S=
am Moffatt<br><a href=3D"http://pasamio.id.au" target=3D"_blank">http://pas=
amio.id.au</a></p><p></p><p>On Sun, Mar 25, 2012 at 5:02 AM, elin <<a hr=
ef=3D"mailto:elin.war...@gmail.com" target=3D"_blank">elin.war...@gmail.com=
</a>> wrote:<br>> I have to say that field has always been a mystery =
to me. It has never saved<br>> and I guess you have figured out why but =
it's not clear what it should even<br>> save. I think of it more l=
ike the field in the installer that you use to<br>> find the zip file bu=
t you don't ever actually store that information.<br>> Usually the=
fields like the media or imagelist or filelist fields would<br>> =
just save the reference to the name and location of the file.<br>><br>&g=
t; I have always thought that what you should do is extend it to make a cus=
tom<br>> field that is actually useful for the file types you want.<br>&=
gt;<br>> Elin<br>><br>><br>>> Thanks for the reply Elin. Of =
course I can share a little bit of code:<br>>><br>>> so here is=
the form:<br>>><br>>> <?xml version=3D"1.0" encoding=3D"utf=
-8"?><br>>> <form><br>>> <fields=
et><br>>> <field name=3D"=
hotspots_kml_id" type=3D"text" default=3D"0"<br>>> label=3D"JGLOBAL_F=
IELD_ID_LABEL"<br>>> &nbs=
p; readonly=3D"true" class=3D"readonly"<br>>>  =
; description=3D"JGLOBAL_FIELD_ID_=E2=
=80=8BDESC"/><br>>><br>>><br>>> &nbs=
p; ....<br>>><br>>><br>>> &=
nbsp; <field name=3D"kml_file" type=3D"file" lab=
el=3D"COM_HOTSPOTS_KML_FILE"<br>>> &nbs=
p; description=3D"COM_HOTSP=
OTS_KML_=E2=80=8BFILE_DESC" required=3D"true"/><br>>><br>>><=
br>>> ....<br>>><br>=
>><br>>> </fieldset><br>>><br>&g=
t;> </form><br>>><br>>> my edit.php layout file:<br>&g=
t;><br>>> <form enctype=3D"multipart/form-data" action=3D"<?=
php echo<br>>> JRoute::_('index.php?option=3D=E2=80=8Bcom_hotspots&am=
p;view=3Dkml&=E2=80=8Bhotspots_kml_id=3D' .<br>>> (int)$this->=
item->hotspots_=E2=80=8Bkml_id); ?>" method=3D"post" class=3D"form"<b=
r>>> name=3D"adminForm" id=3D"adminForm" ><br>>><br>>>=
<div class=3D"width-60 fltlft"><br>>>  =
; <fieldset class=3D"adminform"><=
br>>> &nb=
sp; <legend><?php echo empty($this->item->hotspots_=E2=80=8B=
kml_id) ?<br>>> JText::_('COM_HOTSPOTS_NEW_=E2=80=8BKML') : JText::sp=
rintf('COM_HOTSPOTS_=E2=80=8BEDIT_KML',<br>>> $this->item->hots=
pots_kml_id); ?></legend><br>>><br>>> &nbs=
p; <ul class=3D"adminfor=
mlist"><br>>> &nbs=
p; ....<br>>> &=
nbsp; <=
;li><br>>> &=
nbsp; <?php echo $=
this->form->getLabel('=E2=80=8Bhotspots_kml_id');<br>>> ?><b=
r>>> &nbs=
p; <?php echo $this->=
form->getInput('=E2=80=8Bhotspots_kml_id');<br>>> ?><br>>>=
; &=
nbsp; </li><br>>> &n=
bsp; ....<br>>><br>>>  =
; <li><br>>> &n=
bsp;  =
; <?php echo $this->form->getL=
abel('kml_=E2=80=8Bfile'); ?><br>>> =
&nb=
sp; <?php echo $this->form->getInput('kml_=E2=80=8Bfile'); ?=
><br>>><br>>> &nbs=
p; </li><br>>> =
</ul><br=
>>><br>>>  =
; .....<br>>><br>>> <input ty=
pe=3D"hidden" name=3D"task" value=3D""/><br>>> =
<?php echo JHTML::_('form.token'); ?><br>>> </form><br>&g=
t;><br>>> So when you click on save, we go in the kml contro=
ller that extends<br>>> JControllerForm:<br>>> class HotspotsCo=
ntrollerKml extends JControllerForm {<br>>> ....<br>>> }<br>>=
;><br>>> If I don't override the save function, then we use the pa=
rent function<br>>> which looks like this:<br>>><br>>> pu=
blic function save($key =3D null, $urlVar =3D null)<br>>>  =
; {<br>>><br>>> // =
Check for request forgeries.<br>>> &nb=
sp; JRequest::checkToken() or jexit(JText::_('JINVALID_=E2=80=8BTOKEN'));<b=
r>>><br>>><br>>> //=
Initialise variables.<br>>> $a=
pp =3D JFactory::getApplication();<br>>> &nb=
sp; $lang =3D JFactory::getLanguage();<br>>> =
$model =3D $this->getModel();<br>>> &nbs=
p; $table =3D $model->getTable();<br>>> &=
nbsp; $data =3D JRequest::getVar('jform', ar=
ray(), 'post', 'array');<br>>> =
$checkin =3D property_exists($table, 'checked_out');<br>>> &nbs=
p; $context =3D "$this->option.edit.$this->=
=E2=80=8Bcontext";<br>>> $task =
=3D $this->getTask();<br>>><br>>> .....<br>>><br>>&=
gt;<br>>><br>>> // Validate the posted data.<br>>> =
// Sometimes the form needs some posted dat=
a, such as for plugins<br>>> and modules.<br>>> &nb=
sp; $form =3D $model->getForm($data, false);<br>>&=
gt;<br>>> if (!$form)<br>>&g=
t; {<br>>> &=
nbsp; $app->enqueueMessage($model->=E2=
=80=8BgetError(), 'error');<br>>><br>>> &nbs=
p; return false;<br>>> &nb=
sp; }<br>>><br>>> &=
nbsp; // Test whether the data is valid.<br>>> &nbs=
p; $validData =3D $model->validate($form, $data);<br>=
>><br>>> .....<br>>><br>>>  =
; if ($validData =3D=3D=3D false)<br>>>  =
; {<br>>> ....<br>>><br>>> }<br>>&g=
t;<br>>> As you can see in this function we do:<br>>> $data =3D=
JRequest::getVar('jform', array(), 'post', 'array');<br>>><br>>&g=
t; and this is all ok, but the file is not sent in the post array, but it i=
s<br>>> in the $_FILES array.<br>>><br>>> And because of =
this the validation in the model fails.<br>>><br>>> So as I sai=
d, I overwrote the save function and added a $data['kml_file']<br>>> =
=3D true; if the user has submitted a file.<br>>><br>>> The que=
stion is - should I overwrite this function? Or should I do the<br>>>=
checks in the validate function?<br>>><br>>> Since the validat=
e function expects to receive the $form and the $data I<br>>> think t=
hat we should not do any interactions with the $_POST and $_FILES<br>>&g=
t; arrays there, right?<br>>><br>>> Am I doing something wrong =
here? Is there a better approach to this???<br>>><br>>> Thanks =
in advance!<br>>> Daniel<br>>><br>>> On Friday, March 23,=
2012 10:27:04 PM UTC+1, elin wrote:<br>>>><br>>>> I'm no=
t sure I understand what is happening. Maybe you could show us your<br>>=
>> code or xml?<br>>>><br>>>><br>>>> Are y=
ou saying that the field has been filled out but the validation is<br>>&=
gt;> saying it is empty?<br>>>><br>>>><br>>>>=
Elin<br>>>><br>>>><br>>>> On Friday, March 23, =
2012 11:39:22 AM UTC-4, Daniel D. / compojoom wrote:<br>>>>><br=
>>>>> Hey Guys,<br>>>>> For the last hour I've been=
trying to figure out how to use a<br>>>>> JFormFieldFile field=
that is set to required.<br>>>>><br>>>>> The thing=
is when I save the form data, the save function complains that<br>>>=
>> the file field is empty and because it is required -> the valid=
ation<br>>>>> function returns false...<br>>>>><br>=
>>>> I went around this by overriding the whole save function i=
n the<br>>>>> JControllerForm. (I just checked if the $_F=
iles array contains the file and<br>>>>> added it to the $data =
array.) But this approach feels really stupid for<br>>>>> me???=
I basically have the same save function + 3 lines that handle the file<br>=
>>>> field.<br>>>>><br>>>>> Is there an=
y other way to let JForm know that we have a field that is<br>>>>&=
gt; not in the post array? Preprocess form in the controller or something?<=
br>>>>><br>>>>> Regards,<br>>>>> Daniel=
<br>><br>> --<br>> You received this message because you are subsc=
ribed to the Google Groups<br>> "Joomla! CMS Development" group.<br>>=
To view this discussion on the web, visit<br>> <a href=3D"https://group=
s.google.com/d/msg/joomla-dev-cms/-/Nc8IpWwUHz8J" target=3D"_blank">https:/=
/groups.google.com/d/=E2=80=8Bmsg/joomla-dev-cms/-/=E2=80=8BNc8IpWwUHz8J</a=
>.<br>><br>> To post to this group, send an email to <a href=3D"mailt=
o:joomla-dev-cms@googlegroups.com" target=3D"_blank">joomla-dev-cms@googleg=
roups.=E2=80=8Bcom</a>.<br>> To unsubscribe from this group, send email =
to<br>> <a href=3D"mailto:joomla-dev-cms%2Bunsubscribe@googlegroups.com"=
target=3D"_blank">joomla-dev-cms+unsubscribe@=E2=80=8Bgooglegroups.com</a>=
.<br>> For more options, visit this group at<br>> <a href=3D"http://g=
roups.google.com/group/joomla-dev-cms?hl=3Den-GB" target=3D"_blank">http://=
groups.google.com/=E2=80=8Bgroup/joomla-dev-cms?hl=3Den-GB</a>.<br></p><p><=
/p><p></p><p></p><p></p></blockquote></div><br>On Saturday, March 24, 2012 =
10:30:58 PM UTC-4, Samuel Moffatt wrote:<blockquote class=3D"gmail_quote" s=
tyle=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-le=
ft: 1ex;">You have to write something in your model to process and save it.=
This<br>is primarily a PHP side effect that it'll be jammed into $_FILES a=
nd<br>you have to use move_uploaded_file (or JFile::upload) to shift it<br>=
somewhere on disk where you want to use it.<p>No idea why the required thin=
g isn't working, possibly a bug but you<br>shouldn't need to subclass, just=
pick it up in the model when you save<br>to shift the file on disk somewhe=
re.</p><p>Cheers,</p><p>Sam Moffatt<br><a href=3D"http://pasamio.id.au" tar=
get=3D"_blank">http://pasamio.id.au</a></p><p></p><p>On Sun, Mar 25, 2012 a=
t 5:02 AM, elin <<a href=3D"mailto:elin.war...@gmail.com" target=3D"_bla=
nk">elin.war...@gmail.com</a>> wrote:<br>> I have to say that field h=
as always been a mystery to me. It has never saved<br>> and I guess you =
have figured out why but it's not clear what it should even<br>> s=
ave. I think of it more like the field in the installer that you use to<br>=
> find the zip file but you don't ever actually store that information.<=
br>> Usually the fields like the media or imagelist or filel=
ist fields would<br>> just save the reference to the name and location o=
f the file.<br>><br>> I have always thought that what you should do i=
s extend it to make a custom<br>> field that is actually useful for the =
file types you want.<br>><br>> Elin<br>><br>><br>>> Thank=
s for the reply Elin. Of course I can share a little bit of code:<br>>&g=
t;<br>>> so here is the form:<br>>><br>>> <?xml versio=
n=3D"1.0" encoding=3D"utf-8"?><br>>> <form><br>>> &nbs=
p; <fieldset><br>>> =
<field name=3D"hotspots_kml_id" type=3D"text" default=3D"0"<br>&g=
t;> label=3D"JGLOBAL_FIELD_ID_LABEL"<br>>> &nbs=
p; readonly=3D"true" class=3D"readonly"<br>&=
gt;> descriptio=
n=3D"JGLOBAL_FIELD_ID_=E2=80=8BDESC"/><br>>><br>>><br>>&g=
t; ....<br>>><br>>><=
br>>> <field name=3D"km=
l_file" type=3D"file" label=3D"COM_HOTSPOTS_KML_FILE"<br>>> &nb=
sp; =
description=3D"COM_HOTSPOTS_KML_=E2=80=8BFILE_DESC" required=3D"true"/>=
<br>>><br>>><br>>> &n=
bsp; ....<br>>><br>>><br>>> </field=
set><br>>><br>>> </form><br>>><br>>> my ed=
it.php layout file:<br>>><br>>> <form enctype=3D"multipart/f=
orm-data" action=3D"<?php echo<br>>> JRoute::_('index.php?option=
=3D=E2=80=8Bcom_hotspots&view=3Dkml&=E2=80=8Bhotspots_kml_id=3D' .<=
br>>> (int)$this->item->hotspots_=E2=80=8Bkml_id); ?>" metho=
d=3D"post" class=3D"form"<br>>> name=3D"adminForm" id=3D"adminForm" &=
gt;<br>>><br>>> <div class=3D"width-60 fl=
tlft"><br>>> <fields=
et class=3D"adminform"><br>>> =
<legend><?php echo empty($this->=
item->hotspots_=E2=80=8Bkml_id) ?<br>>> JText::_('COM_HOTSPOTS_NEW=
_=E2=80=8BKML') : JText::sprintf('COM_HOTSPOTS_=E2=80=8BEDIT_KML',<br>>&=
gt; $this->item->hotspots_kml_id); ?></legend><br>>><b=
r>>> &nbs=
p; <ul class=3D"adminformlist"><br>>> &=
nbsp; ....<br>&=
gt;> &=
nbsp; <li><br>>> &nbs=
p; &=
nbsp; <?php echo $this->form->getLabel('=E2=80=8Bhotspots_km=
l_id');<br>>> ?><br>>> &=
nbsp; &nbs=
p; <?php echo $this->form->getInput('=E2=80=8Bhotspots_kml_id');<b=
r>>> ?><br>>> &nbs=
p; </li><br>>> =
....<br>>><br>>> &n=
bsp;  =
; <li><br>>> &n=
bsp; <?php e=
cho $this->form->getLabel('kml_=E2=80=8Bfile'); ?><br>>> &nb=
sp; =
<?php echo $this->form->getIn=
put('kml_=E2=80=8Bfile'); ?><br>>><br>>> &=
nbsp; <=
;/li><br>>> =
</ul><br>>><br>>> &n=
bsp; .....<br>>><br>>> &nbs=
p; <input type=3D"hidden" name=3D"task" value=3D""/><br>&=
gt;> <?php echo JHTML::_('form.token'); ?><br>&=
gt;> </form><br>>><br>>> So when you click on sa=
ve, we go in the kml controller that extends<br>>> JControllerForm:<b=
r>>> class HotspotsControllerKml extends JControllerForm {<br>>>=
; ....<br>>> }<br>>><br>>> If I don't override the save f=
unction, then we use the parent function<br>>> which looks like this:=
<br>>><br>>> public function save($key =3D null, $urlVar =3D nu=
ll)<br>>> {<br>>><br>>> &n=
bsp; // Check for request forgeries.<br>>> &=
nbsp; JRequest::checkToken() or jexit(JText::_('JI=
NVALID_=E2=80=8BTOKEN'));<br>>><br>>><br>>> &=
nbsp; // Initialise variables.<br>>> &=
nbsp; $app =3D JFactory::getApplication();<br>>> &=
nbsp; $lang =3D JFactory::getLanguage();<br>=
>> $model =3D $this->getMode=
l();<br>>> $table =3D $model-&g=
t;getTable();<br>>> $data =3D J=
Request::getVar('jform', array(), 'post', 'array');<br>>>  =
; $checkin =3D property_exists($table, 'checked_ou=
t');<br>>> $context =3D "$this-=
>option.edit.$this->=E2=80=8Bcontext";<br>>> =
$task =3D $this->getTask();<br>>><br>>> =
.....<br>>><br>>><br>>><br>>> // Validate the poste=
d data.<br>>> // Sometimes the =
form needs some posted data, such as for plugins<br>>> and modules.<b=
r>>> $form =3D $model->getFo=
rm($data, false);<br>>><br>>> &n=
bsp; if (!$form)<br>>> {<br>>=
;> $app->enq=
ueueMessage($model->=E2=80=8BgetError(), 'error');<br>>><br>>&g=
t; return false;<b=
r>>> }<br>>><br>>> =
// Test whether the data is valid.<br=
>>> $validData =3D $model->v=
alidate($form, $data);<br>>><br>>> .....<br>>><br>>>=
; if ($validData =3D=3D=3D false)<br>=
>> {<br>>> ....<br>>&g=
t;<br>>> }<br>>><br>>> As you can see in this function we=
do:<br>>> $data =3D JRequest::getVar('jform', array(), 'post', 'arra=
y');<br>>><br>>> and this is all ok, but the file is not sent i=
n the post array, but it is<br>>> in the $_FILES array.<br>>><b=
r>>> And because of this the validation in the model fails.<br>>&g=
t;<br>>> So as I said, I overwrote the save function and added a $dat=
a['kml_file']<br>>> =3D true; if the user has submitted a file.<br>&g=
t;><br>>> The question is - should I overwrite this function? Or s=
hould I do the<br>>> checks in the validate function?<br>>><br>=
>> Since the validate function expects to receive the $form and the $=
data I<br>>> think that we should not do any interactions with the $_=
POST and $_FILES<br>>> arrays there, right?<br>>><br>>> A=
m I doing something wrong here? Is there a better approach to this???<br>&g=
t;><br>>> Thanks in advance!<br>>> Daniel<br>>><br>>=
;> On Friday, March 23, 2012 10:27:04 PM UTC+1, elin wrote:<br>>>&=
gt;<br>>>> I'm not sure I understand what is happening. Maybe you =
could show us your<br>>>> code or xml?<br>>>><br>>>=
><br>>>> Are you saying that the field has been filled out but =
the validation is<br>>>> saying it is empty?<br>>>><br>&g=
t;>><br>>>> Elin<br>>>><br>>>><br>>>=
> On Friday, March 23, 2012 11:39:22 AM UTC-4, Daniel D. / compojoom wro=
te:<br>>>>><br>>>>> Hey Guys,<br>>>>> F=
or the last hour I've been trying to figure out how to use a<br>>>>=
;> JFormFieldFile field that is set to required.<br>>>>><br>=
>>>> The thing is when I save the form data, the save function =
complains that<br>>>>> the file field is empty and because it i=
s required -> the validation<br>>>>> function returns false.=
..<br>>>>><br>>>>> I went around this by overriding=
the whole save function in the<br>>>>> JControllerForm. (I jus=
t checked if the $_Files array contains the file and<br>>>>&=
gt; added it to the $data array.) But this approach feels really stupid for=
<br>>>>> me??? I basically have the same save function + 3 line=
s that handle the file<br>>>>> field.<br>>>>><br>&g=
t;>>> Is there any other way to let JForm know that we have a fiel=
d that is<br>>>>> not in the post array? Preprocess form in the=
controller or something?<br>>>>><br>>>>> Regards,<=
br>>>>> Daniel<br>><br>> --<br>> You received this mes=
sage because you are subscribed to the Google Groups<br>> "Joomla! CMS D=
evelopment" group.<br>> To view this discussion on the web, visit<br>>=
; <a href=3D"https://groups.google.com/d/msg/joomla-dev-cms/-/Nc8IpWwUHz8J"=
target=3D"_blank">https://groups.google.com/d/=E2=80=8Bmsg/joomla-dev-cms/=
-/=E2=80=8BNc8IpWwUHz8J</a>.<br>><br>> To post to this group, send an=
email to <a href=3D"mailto:joomla-dev-cms@googlegroups.com" target=3D"_bla=
nk">joomla-dev-cms@googlegroups.=E2=80=8Bcom</a>.<br>> To unsubscribe fr=
om this group, send email to<br>> <a href=3D"mailto:joomla-dev-cms%2Buns=
ubscribe@googlegroups.com" target=3D"_blank">joomla-dev-cms+unsubscribe@=E2=
=80=8Bgooglegroups.com</a>.<br>> For more options, visit this group at<b=
r>> <a href=3D"http://groups.google.com/group/joomla-dev-cms?hl=3Den-GB"=
target=3D"_blank">http://groups.google.com/=E2=80=8Bgroup/joomla-dev-cms?h=
l=3Den-GB</a>.<br></p><p></p><p></p><p></p><p></p></blockquote>
------=_Part_82_29733976.1332679285687--
------=_Part_81_6152849.1332679285687--