On Jun 10, 2009, at 1:38 PM, [email protected] wrote:

Hi David and others,

I was trying to implement the below and had a problem.

I have a form which is to accept image uploads and need some help
with the following:

1. My form allows a maximum of 3 images to be uploaded. I'm
wondering if it is necessary for me to check that not more than 3
are uploaded?

If yes, how do I do this? My input fields are called data[images]
[1], data[images][2]... etc - I tried checking these fields using
the array length validator as below, but it always fails (even when
the validation conditions are met).

     <validator class="arraylength">
       <arguments base="data[Images][]">
         <argument/>
       </arguments>
       <errors>
         <error>Error</error>
       </errors>
       <ae:parameters>
         <ae:parameter name="required">false</ae:parameter>
         <ae:parameter name="min">0</ae:parameter>
         <ae:parameter name="max">3</ae:parameter>
       </ae:parameters>
     </validator>

Not like that. If you use an arguments base, then that simply means
that every element in the base will be run against the validator. Not
what you want in this case.

What you want to do is simply check for the length of the images array:
<argument>data[images]</argument>


I tried this and created the following validator:

    <validator class="arraylength">
      <arguments>
        <argument>data[Images]</argument>
      </arguments>
      <errors>
        <error>ERROR: An invalid number of images was uploaded</error>
      </errors>
      <ae:parameters>
        <ae:parameter name="required">true</ae:parameter>
        <ae:parameter name="min">1</ae:parameter>
        <ae:parameter name="max">3</ae:parameter>
      </ae:parameters>
    </validator>

However, the error message does not appear on the form template (code
below). I'm guessing because I don't have an input explicitly called
data[Images] and so the FPF isn't able to place the error?


So the validator fails and you get the error view, you just do no get the message displayed? That's a tad weird, because the following config block from global_filters.xml should take care of placing that right inside the form:

<!-- everything that did not match any of the rules above, or errors that do not belong to a field -->
                                <ae:parameter name="error_messages">
                                        <!-- insert before the element -->
<!-- that can be an input, or a form, if the error does not belong to a field or didn't match anywhere else -->
                                        <ae:parameter name="self::*">
                                                <ae:parameter 
name="location">before</ae:parameter>
                                                <!-- no container here! we just 
insert paragraph elements -->
<ae:parameter name="markup"><![CDATA[<p class="error">$ {errorMessage}</p>]]></ae:parameter>
                                        </ae:parameter>
                                </ae:parameter>

Could you check the html code on the error page to see if the html code is maybe injected but suppressed by css?


<fieldset>        
  <legend>Vehicle Images</legend>
        <label for="data[Images][1]">Image #1:</label>
        <input id="data[Images][1]" type="file" name="data[Images][1]" />
        <p/>
        <label for="data[Images][2]">Image #2:</label>
        <input id="data[Images][2]" type="file" name="data[Images][2]" />
        <p/>
        <label for="data[Images][3]">Image #3:</label>
        <input id="data[Images][3]" type="file" name="data[Images][3]" />
        <p/>
  <p class="note">
<em>Images should be 200x125 px, <br/> JPEG or GIF format only.</ em>
  </p>
        </fieldset>

3. I need to validate two fields together ie. sale price min and max
4. If the code in my Action throws and catches an Exception, how do
I pass the Exception message forward to the view? For example, in
the below code, I'd like to read $e->message and have that displayed
in the Error view

public function executeWrite(AgaviRequestDataHolder $rd)
{
     try {
       // initialize object
       $listing = new Listing();
       // populate with validated input
       $listing->fromArray($rd->getParameter('data'));
       $listing->DisplayStatus = 0;
       $listing->save();
         return 'Success';
     } catch (Exception $e) {
         // $msg = $e->getMessage();
         return 'Error';
       }
 }

Why don't you simply set the exception or the message as an attribute!?

I was under the impression (maybe mistaken) that calls to
setAttribute() should only be made in the View, not the Action. Not
sure if that's right or not, do advise what is the "best practice" in
this case?

That's a misconception. It's fine to place calls to setAttribute() in the action, in fact it's the standard way for the action to pass data to the view (and to the template).


Thanks,

Vikram

_______________________________________________
users mailing list
[email protected]
http://lists.agavi.org/mailman/listinfo/users


cheers

felix

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________
users mailing list
[email protected]
http://lists.agavi.org/mailman/listinfo/users

Reply via email to