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?
<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?
Thanks,
Vikram
_______________________________________________
users mailing list
[email protected]
http://lists.agavi.org/mailman/listinfo/users