CakePHP when using uploadedit validation, save fails - validation

Okay, so I am uploading documents when a user registers on my site, I collect some personal information and save it. Now I have had a bit of a problem, I split up the main registration form into a couple of view, where the user once logged in can now edit certain information, like for example one field called personal_introduction. For some reason this wont save, even though there are no validation errors on the field. Messed around in the model, to try pick up the problem and fount THIS causes the problem:
'document_file' => array(
'checkuploadedit' =>array(
'rule' => array('checkUpload', false),
'message' => 'Please select your document.',
),
'checkuploadedit' => array(
'rule' => array('extension', array('doc', 'docx', 'txt', 'rtf')),
'message' => 'File extentions allowed: .doc .docx .txt .rtf - we do NOT accept pdf format.'
)
),
Once I remove this from my model, it saves the data. Now its weird for me that the main registration form gives no problems, and that this view that does not even contain a form input called "document_file" fails the save.
Any ideas...Please!!! Obviously I need to validate the upload.
I tried this: validation on a input file in cakephp , thinking that maybe the above code is not correct, still no luck.
Thanks in advance for your answers.
Cheers Louis.

I went on to further investigate the problem, and found the following, which I still cant explain. When submitting views that do not contain the upload form on the same model, it still tries to validate the upload form, I could actually see the value for one as if submitted, even though it was not. so the only solution I got was to go use unset($this->data['user']['document']) for the actions which do not have the document upload form field. Hope this makes sense.

Related

Skip first index of array validation rule?

Good Evening Devs,
I'm trying to skip the first index of the array while applying validation rule and this is what I tried so far
$validatedData = Validator::make($request->all(),([
'inventories.0' => 'bail',
'inventories' => 'required|array|filled',
'quantities.0' => 'bail',
'quantities.*' => 'required|array|filled',
'required.0' => 'bail',
'required.*' => 'required|array|filled',
]));
But it's not working, any ideas?
I'm trying to add multiple dynamic fields, but want to skip the first index of it.
Please review the picture given below to get the clear picture of the problem.
try this:
$validatedData = Validator::make($request->except(['inventories[0],quantities[0],required[0]']),([
'inventories.*' => 'required|array|filled',
'quantities.*' => 'required|array|filled',
'required.*' => 'required|array|filled',
]));
Bail is not used for skipping an entry. But it may be used for skipping validation logic.
for example,
'phone' => 'bail|numeric|unique:users'
In this case, if somehow the entered phone number is not numeric, it will not check the third validation (i.e. whether the phone number is unique in 'users' table or not).
For your case, you should not use "$request->all()". You should use "request()->except(['inventories[0], quantities[0], required[0]'])" instead
This is perhaps, not the best practice. You're trying to allow the presentation layer to have a direct influence over the data / logic layer of your application. It would probably be better to only send over the data you want to validate rather than sending over everything and they tying to get your validation (and other logic) to ignore the first array element.
Is it an api call or a standard web form you are submitting? If it is an api call, can you not build up your data of only the rows you want to send over, before you make the call?
This will keep your logic layer much cleaner, and allow you to change the ui much easier without affecting the logic, and it being tightly coupled.
Just a suggestion.

How to add / remove elements from array that is in Request

My request looks like this
Array
(
[name] => Eugene A
[address] => Array
(
[billing] => Array
(
[address] => aaa
)
[shipping] => Array
(
[address] => bbb
)
)
)
I need to delete the shipping address. But how?
I can only delete both addresses,
$request->request->remove('address');
but I don't want it.
I want to delete only shipping address, like so
$request->request->remove('address.shipping');
But it is not working for me
Laravel 5.6
Update
Why do I need it?
Easy. I have abstracted out my Form Request validation into a class that is a child to Illuminate\Foundation\Http\FormRequest.
I actually have few classes for validation. I call them one by one in a controller like so:
app()->make(CustomerPostRequest::class); // validate Customer information
app()->make(AddressSaveRequest::class); // validate Addresses
Why?
Now I can Mock this requests in unit-tests, and I can have my validation abstracted out. And I can use Address validation in many places.
But Now I need more flexibility. Why?
Because AddressSaveRequest rule looks like this
public function rules(): array
{
return [
'address.*.address' => [
'bail',
'required',
'string',
],
...
It validates all addresses.
But sometimes I don't want to validate shipping address, if the the chech_box - ship_to_the_same_address is ticked.
But I have my Address validator abstracted in separate file and it is used in many places. There are places where ship_to_the_same_address tick box is not presented.
Thus I cannot use 'required_unless:ship_to_same_address,yes',
And I cannot use
app()->makeWith(AddressSaveRequest::class, ['ship_to_the_same_address ' => 'yes']);
Because Taylor said ...when calling makeWith. In my opinion it should make a new instance each time this method is called because the given parameter array is dynamic.. And it does, and it does not work correctly with app()->instance(AddressSaveRequest::class, $addressSaveRequest); and cannot be mocked in unit tests.
Why Taylor decided it - I seriously don't know.
PS
And yes, I know that mocking requests is not recommended.
If you were trying to add or remove inputs from the Request itself:
You can add data to the request pretty easily by merging it in and letting Laravel handle which data source is being used:
$request->merge(['input' => 'value']);
That will merge in the input named input into the input source for the Request.
For removing inputs you could try to replace all the inputs without that particular input in the replacement:
$request->replace($request->except('address.shipping'));
Just one idea to try.
Try this:
$request->except(['address.shipping']);
Details: Laravel Request
Laravel has a helper method called array_forget, which does exactly what it sounds like:
$requestArray = $request->all();
$newArray = array_forget($requestArray, 'address.shipping')
Documentation
After the edit to the main question with why some inputs of the request are to be deleted, my main answer isn't correct anymore. User Lagbox has the correct answer for the question that was asked.
However, I would like to note that another solution would be to have seperate Request classes with validation. One for placing an order (assuming it is a system where someone can order stuff) where ship_to_same_address is present and another one for things like updating your account, like PlaceOrderRequest and UpdateAccountRequest classes.

Facebook adgroup creation - Invalid parameter

I am having an issue promoting an unpublished page post via the ads-api.
This was previously working ok for me, but began causing problems yesterday.
I first create an ad_campaign, and then, using the returned campaign_id, I attempt to create an adgroup.
The response from the server is
array(1) {
'error' =>
array(3) {
'message' =>
string(53) "(#100) Invalid parameter: adgroup_spec["campaign_id"]"
'type' =>
string(14) "OAuthException"
'code' => int(100)
}
}
I have verified that I am sending over the correct, newly created campaign_id.
Another point of interest; when I use the UI to delete the ad_campaign afterwards, I am told that I do not have permission to do so. User XXXX does not have permission to access campaign YYYY.
I'm thinking this must be an access_token/permission issue but I'm stumped. The ad objects are created using the user's token, and the unpublished page post is created using the page's access token.
NB: I can provide snippets if needs be, but I'm fairly sure this is a problem with the object creation flow as opposed to a code issue.
Has anyone seen anything similar?
Cheers, Gary
Update with POST data
array(7) {
'campaign_id' =>
int(6013621027457)
'bid_type' =>
int(6)
'bid_info' =>
string(37) "{"clicks":10,"reach":10,"actions":80}"
'conversion_specs' =>
string(66) "{"action.type":"offsite_conversion","offsite_pixel":6013619180457}"
'creative' =>
string(86) "{"type":27,"object_id":407012979370770,"auto_update":false,"story_id":565852233486843}"
'name' =>
string(23) "PropelAd (via PropelAd)"
'targeting' =>
string(95) "{"countries":["IE","AE","GB"],"friends_of_connections":[407012979370770],"page_types":["feed"]}"
}
Yes, it happens to me as well from time to time when uploading ads.
It's very statistical, and it seems to be an off-sync between Facebook servers (the campaign was already successfully uploaded, but the ad-request-handler does not recognize that id).
Wait a few seconds, and try again - after a few shots, it will always work (usually there's no problem, it's pretty rare but happens - and never lasted more than a minute of not-recognizing).
Perhaps it happened more today, due to the general Facebook failures.
I can only assume/hope that Facebook keep track of these errors, and are working on minimizing them by syncing their servers better.
Yep - we are currently putting a lot of time in checking why do ad group fail on unpublished posts - specifically on posts scheduled in the future.
The behavior is very inconsistent. The errors we get are slightly different: "Could not save ad", "Invalid ad creative". The errors are not reported on all ad groups but mostly on one of the ad groups of a whole batch.
We do make sure that the campaign start time is equal or AFTER the time the post is scheduled to.
When we publish the post now and test with the same campaign structure it succeeds (although this also succeeds sometimes on the second try - like it was mentioned here, that the campaign takes some time maybe to become fully valid)
A related bug I opened is here:
https://developers.facebook.com/bugs/593878450648811?browse=external_tasks_search_results_52662e53c59bd2e63625449

Magento Custom Report remove column

How do i remove the Period column from my custom report?
i tried with
unset($this->_columns['period']);
but its not working.
I need to start my columns like order number, order date,subtotal etc etc.
This is loaded by a custom grid of Backend of Magento, you need identify this. To find the custom block you can active backend hints and see what grid are loaded in this section.
You can active backend hints with the next Sql:
UPDATE core_config_data SET scope_id = 0 WHERE path like 'dev/debug/template_hints%';
Execute always in development enviroment. Never in production site
You can see how to active backend hints in this link, http://www.damianculotta.com.ar/2009/07/11/mostrar-phtmls-y-bloques-usados-en-el-skin-de-backend-de-magento/ sorry, this is in spanish, I don´t find this information in english :(
With this you also to know the type of block load in this grid, this is the important date. Next you need create a little module that rewrite this block.
Whit this method you can see that the next dates
Template
adminhtml\default\default\template\widget/grid.phtml
Block
Mage_Adminhtml_Block_Report_Sales_Sales_Grid
in the block you can see the next columm in the construct
$this->addColumn('period', array(
'header' => Mage::helper('sales')->__('Period'),
'index' => 'period',
'width' => 100,
'sortable' => false,
'period_type' => $this->getPeriodType(),
'renderer' => 'adminhtml/report_sales_grid_column_renderer_date',
'totals_label' => Mage::helper('sales')->__('Total'),
'html_decorators' => array('nobr'),
));
You need remove this columm in you custom block, always in separate extension.
If you don´t know how you can rewrite a block, only need said me :P
Hope help you

Yii : form values retention for CHtml::checkBoxList on form validaton

I am using CHtml::checkBoxList for my form. For some reason I cannot use CHtml:activeCheckBoxList or CActiveForm::checkBoxList. Everything works fine only problem is that I loose checkbox values on form validation error. What could be the easiest way to fix this ?
If you're making a form, you probably want to use CActiveForm's checkboxlist, which is a form-specific wrapper of CHtml::activeCheckBoxList) instead. Something like
echo $form->checkBoxList(
$model,
'condiments',
array(
'ketchup'=>'Ketchup',
'mustard'=>'Mustard',
'relish'=>'Relish',
'onions'=>'Onions'
)
);
should give you a persistent check box list of hot dog condiments, for example.

Resources