Persist CMultiFileUpload selection through input validation - yii

I'm using a CMultiFileUpload control in one of my forms like this:
$this->widget('CMultiFileUpload', array(
'name' => 'neueAnhaenge',
));
When input validation for some other form element fails and the input form is rendered again, a previous selection in this control is gone (as expected).
How do I repopulate this control, what do I have to do in my controller, is there a way to prepopulate this?
Thanks in advance.

For file fields it is rather impossible to reset the values assigned to it after it has been sent to the server.
One way to solve this would be to get the uploaded files, store them temporarily on the server and modify the form so it sends a reference to the file on the server.
A much better way would be to use Ajax or Client side validation of form fields to ensure no validation error occures when form has been sent. You can enable these options for CActiveForm: $enableClientValidation and $enableAjaxValidation.

Related

How to update fields before the view is displayed in Odoo

I could not figure how I'm supposed to do the following scenario;
I've two fields in my inherited res.partner view "ExternalID" and "ExternalCode" all the fields are going to be editable until those two are filled. And after these two are filled everything is going to be read-only that's the easy part we can handle this by attrs. The problem is when these two fields are filled (they're being filled from an external app by odoo web API.) I need call an external web API before the form is loaded i^ve to update the read-only fields. I've tried placing a computed field and make it invisible so that it gets fired up when the view is being loaded and I thought I should update the other fields but it seems when I write a compute function it can only update the field that it is called from.
For Example;
external_api_call_field = fields.Char(compute='_get_values_from_api')
#api.multi
def _get_values_from_api(self):
for record in self:
#call api and do stuff
#set the values that we got from the external application
record.company_detailed_address="Sample address line"
the method gets triggered and seems to run all the way but when the form is loaded the address field is not filled.
I tried to call
self.write({'company_detailed_address' : 'Sample address line'})
which seems to work but the new value is not set directly you've to refresh the view
Any help or guidance is appreciated.
Regards.

Disable validation in Yii

I am trying to implement a "Save as Draft" functioning in my project using Yii. I have a form with 2 buttons :- Submit and Save . On clicking the Submit button , after validating all the fields including required fields the form data is saved into the database. It works perfect. On clicking Save button I need to save the form data without default validations into the database. How can I implement this disabling of validation in a controller action ??
All advices are acceptable..
Thanks
Turning off validation rules all together is easy:
$model->save(false);
This will not do any validation and will just try and save your model (may still fail on the database side).
But if you want to run some validation ,you might want to look into Scenarios here. They allow you to specify a different set of rules depending on which scenario you initialize the model with. That way you can only turn on/off whole sets of validation rules.
$model = new Thingy();
$model->save(); // All default validation rules
$model = new Thingy('draft');
$model->save(); //Applies all default & "draft" validation rules
Your question says in Controller but as far as I know, in controller we have filters which do for example check the permissions. That can be overridden as explained in this section of the guide. If you meant the validation that is done in model, then you can use scenarios (bypass validation by binding rules to scenario and no validation in other scenarios).
Check this thread that discuss a like problem
If I have misunderstood your question please comment here so that I update the answer accordingly!

Yii: ClientSide Validation on FileField and TextField

I have a yii form with a file field and a normal text field for supplying an external image url. I managed to get a normal server validation working which checks that only one of the two form fields is filled out (because you can either provide a local file for uploading OR an external picture URL). Important to notice is, that the attribute value of the "file field" seems only to be available after the $_POST var is set, meaning that a normal custom validation rule in the Model Class won't work since the $_POST value of the file field is only available after submission (in the $_FILES var).
But if you look at my provided picture below, I want also a ClientSide Validaton, so that the user gets immediate feedback that it's not allowed to fill out both fields. But how do I accomplish that? I'm sitting on this problem for about 2 days, half of the time on searching for solutions...I am new to Yii.
http://www.prism-informatics.com/images/demo.png
Best wishes,
S
I am not completely sure what your problems is as you didnt provide any code but i assume you forgot :
public function rules()
{
return array(
array('image', 'file', 'types'=>'jpg, gif, png'),
);
}

JQGrid: Binding JSON data to MultiSelect Checkbox

I have JQGrid loading data from WCF OperationContract with paging and sorting working fine. I am using "multiselect: true" so that I get the checkbox column and ability to select multiple rows. I've implemented gridComplete:, onSelectAll: and onSelectRow: to capture when checkboxes are checked/unchecked and to maintain checked state upon pagination. I am able to save the checkbox state to the DB via another WCF method call.
What I cannot figure out how to do is load the saved checkbox state for each row along with the other fields specified in colModel:.
Any ideas? I realize I can make a separate WCF service call to get the values, loop through them and set state manually, but that seems like a huge waste and overly clunky.
Thanks in advance.
The simplest way, which I imagine me immediately after reading of your question, is to have additional hidden column (hidden: true property in the colModel for the column) and the checkboxes inside. You can load the selection state from the database an fill the hidden checkboxes. Inside of loadComplete or gridComplete you can use the information to select the rows.
If you would use loadComplete instead of gridComplete you can even eliminate the need to hold hidden row. The callback method loadComplete has data parameter which are initialized with the data originated from the ajax call. So if your server response contain more information as jqGrid need the data will be ignored by jqGrid, but you can see the data in the loadComplete and use the information to set row selection.

MonoRail - Server-side vs. Client-side Form Validation

I'm using MonoRail and was wondering how it decides when to use client-side vs. server-side validation? In my model class I have [ValidateNonEmpty] on two properties, one is a textbox, the other is a dropdown. The textbox triggers client-side validation on form submission, if I leave the dropdown empty though it posts back to the server and returns back the validation error from server-side. Is there a way to get the dropdown to trigger client-side validation? Also it's odd because after the postback, it clears what I had entered in the dropdown but maintains the state of the textbox (viewstate anyone??)
Thanks,
Justin
It viewed source and I saw that it was using jQuery for the client-side validation. It had:
"business.businesstype.id":{ required: "This is a required field" },
for the dropdown, which wasn't working. I noticed that it was using 0 as the default dropdown value so I manually put in firstoptionvalue and that got it working:
$FormHelper.Select("business.businesstype.parent.id", $businessTypes, "%{value='id', text='name', firstoption='Select a Business Type', firstoptionvalue=''}")