Trying to use exclude_if on Laravel 6 - php

I'm trying to escape a value from a field according to another field, but it never get escaped. Here's a part of my code. However if 'samename' is true or false, 'res_title' never get escaped. It always end up in my database. I've also tried with 'unless_if', but stil got the same result.
return [
'samename' => 'required|boolean',
'res_title' => 'exclude_if:samename,false'
];
So here is how I process the request, I use Postman :
{
"samename": true,
"res_title": "test"
}
Also, here is my controller :
public function store(DmLogRequest $req)
{
$log = new DmLog();
$log->samename = $req->samename;
$log->res_title = $req->res_title;
$log->save();
return response()->json($log);
}

Related

Laravel 9 API return error codes instead of error messages on validation

I am trying to make Laravel return error CODES instead of messages along with some extra data depending on the validation error.
Quick examples:
If I have a "unique" validation rule I would expect the result for that field to be:
<field>: {
"code": "unique_rule_error",
"message": "The username has already been taken."
}
If I have, let's say a min rule:
<field>: {
"code": "min_rule_error",
"value": "100",
"message": "The username must be at least 100 characters."
}
In both examples, the "message" field is unnecessary, I am just keeping it there for the time being.
What I have currently working and almost returning the desired results:
App\Validators\RestValidator (custom validator, extending the base Validator class)
class RestValidator extends Validator
{
public function addFailure($attribute, $rule, $parameters = [])
{
if (!$this->messages) {
$this->passes();
}
$attribute = str_replace(
[$this->dotPlaceholder, '__asterisk__'],
['.', '*'],
$attribute
);
if (in_array($rule, $this->excludeRules)) {
return $this->exclude`enter code here`Attribute($attribute);
}
$message = $this->getMessage($attribute, $rule);
$message = $this->makeReplacements($message, $attribute, $rule, $parameters);
$customMessage = new MessageBag();
$customMessage->merge(['code' => strtolower($rule . '_rule_error')]);
if ($rule !== 'Unique') {
$parts = explode(':', $this->currentRule);
if (count($parts) >= 2) {
$boundaries = explode(',', $parts[1]);
$boundaries_count = count($boundaries);
if ($boundaries_count == 1) {
$customMessage->merge(['value' => $boundaries[0]]);
}
if ($boundaries_count > 1) {
$customMessage->merge(['lower' => $boundaries[0]]);
}
if ($boundaries_count >= 2) {
$customMessage->merge(['upper' => $boundaries[1]]);
}
}
}
$customMessage->merge(['message' => $message]);
$this->messages->add($attribute, $customMessage);
}
}
AppServiceProvider
public function boot()
{
//
Validator::resolver(function ($trnslator, $data, $rules, $messages) {
return new RestValidator($trnslator, $data, $rules, $messages);
});
}
This gives me the results I want with all rules I have currently tried, except when I use the Password validation:
Password::min(8)->mixedCase()->numbers()->symbols()
This causes a rather strange behavior that I can't explain, the result is the following:
"password": [
{
"code": "confirmed_rule_error",
"message": "The password confirmation does not match."
},
"{\"code\":\"min_rule_error\",\"value\":\"8\",\"message\":\"The password must be at least 8 characters.\"}",
"The password must contain at least one uppercase and one lowercase letter.",
"The password must contain at least one symbol."
]
The complete password validation code is:
'password' => ['required', 'confirmed', Password::min(8)->mixedCase()->numbers()->symbols()]
I want to return error codes like "unique_rule_error" because I will be translating the messages in the front-end with Vue and I don't want to keep track of a locale on the API side.
The problem seems to be that Laravel handles responses for strings and classed defined rules in different sections of code.
Laravel 9.34.0 has this chunk of code in Illuminate\Validation\Validator#603-607.
In your code, the "confirmed" rule is defined as string, hence in #603, the if statement is false, and eventually in #612 the addFailure method is called in RestValidator.
The next rule would be Password::... which is indeed an instance of RuleContract thus an early return is executed preventing the #612 addFailure method in RestValidator to be executed.
Checking the validateUsingCustomRule method in this same Illuminate\Validation\Validator class we find that in #829 an add method is called to populate error messages, you may want to add your logic there as well.

How to remove parameter from a URL in laravel 5.2

How can I remove the parameters from a URL after processing in my controller? Like this one:
mydomain/mypage?filter%5Bstatus_id%5D
to
mydomain/mypage
I want to remove the parameters after the ? then I want to use the new URL in my view file. Is this possible in laravel 5.2? I have been trying to use other approaches but unfortunately they are not working well as expected. I also want to include my data in my view file. The existing functionality is like this:
public function processData(IndexRequest $request){
//process data and other checkings
return view('admin.index')
->with([
'data' => $data,
'person' => $persons,
]);
}
I want it to be like:
public function processData(IndexRequest $request){
//process data and other checkings
// when checking the full url is
// mydomain/mypage?filter%5Bstatus_id%5D
// then I want to remove the parameters after the question mark which can be done by doing
// request()->url()
// And now I want to change the currently used url using the request()->url() data
return view('admin.index')
->with([
'data' => $data,
'person' => $persons,
]);
}
I'm stuck here for days already. Any inputs are appreciated.
You can use request()->url(), it will return the URL without the parameters
public function processData(IndexRequest $request){
$url_with_parameters = $request()->url();
$url= explode("?", $url_with_parameters );
//avoid redirect loop
if (isset($url[1])){
return URL::to($url[0]);
}
else{
return view('admin.index')
->with(['data' => $data,
'person' =>$persons,]);
}
}
add new url to your routes and assuming it will point to SomeController#SomeMethod, the SomeMethod should be something like :
public function SomeMethod(){
// get $data and $persons
return view('admin.index')
->with(['data' => $data,
'person' =>$persons,]);
}
I hope this helps

apiResourece does not return values on custom method

I am using laravel apiResources for an api call, the problem is that i created a custom method named closePeriod, that when i try to fetch a period it return always
No query results for model [App\Entities\Accounting\Period]
this is the method, i try to get the Period with Period::findOrFail($id), i have checked and $id has a value, and it exists in the database
Route::get('periods/close/{id}', 'PeriodController#closePeriod');
public function closePeriod($id) {
if($period = Period::findOrFail($id)) {
//do something
return response()->json([
'error' => false,
'data' => $period
]);
}
return response()->json([
'error' => true,
'message' => 'The period was not found, please reaload and try again'
]);
}
I have noticed that the api methods does work fine, i get the periods in index, show and can create and edit a period so i think the problem is in the custom method? why?

Laravel 5.2 Query String Parameter Validation

I am currently doing a small project to learn some laravel validation and ran into a problem.
The API endpoint is api/test/schoolbook?start= and my validation is
'start' => ['date_format:Y-m-d H:i:s']
While this works like a charm and sorts the schoolbooks by a start year, i think my validation has some error. It validates if start is equal to the defined date format, all good. but if i now parse just ?start=without any thing, it still goes through, but doesn't throw an error message (it just returns everything without sorting)
Is there a way i can validate this better and prevent the query string parameter to be empty?
If start is not passed, it should return all the records, so i cant make it required really.
So the scenarios are:
?start=date is passed in the right format and returns all the schoolbooks by the passed date,
?start=date is not passed and returns all the records in the database
?start= should also return 'has to be in date format validation'
Thank you!
The Controller:
public function findSchoolbook(
SchoolBookRequest $request,
) : JsonResponse {
$schoolbook = $this->schoolkbool->sort($paramBag);
$response = $this->transformer()->paginator($schoolbook);
return $this->response($response);
}
The ParamBg method i use
private function getParamBag(SchoolBookRequest $request) : ParamBag
{
return ParamBag::create()
->setPage($request->get('page'))
->setPerPage($request->get('per_page'))
->setStartDate($request->get('start_at'))
}
The Request
class SchoolBookRequest extends Request
{
public function rules() : array
{
return [
'start_at' => ['date_format:Y-m-d H:i:s']
];
}
}
This should only filter in start date when present in the request:
private function getParamBag(SchoolBookRequest $request) : ParamBag
{
$parambag = ParamBag::create()
->setPage($request->get('page'))
->setPerPage($request->get('per_page'))
if ($request->has('start_at')) {
$parambag = $parambag->setStartDate($request->get('start_at'));
}
return $parambag;
}

ROUTE PARAMETER DISAPPEAR AFTER SAVE DATA

Hi everyone I need help with this problem. I am programming an application using php with laravel framework. My current laravel framework version is 4.2.11
I have this route to handle POST & GET actions
Route::any("/myaccount2/ausersave/{code?}", array("as" => "ausersave",
"uses" => "PrivateController#ausersave"))->before('auth_admin');
When I use Get action with mydomain/myaccount2/ausersave/90
I get list all data ok. But when I post all data to save the url change to mydomain/myaccount2/ausersave (parameter 90 is missing)
I guest this change is before the data is saved because the {code?} or 90 parameter is missing.
So I was looking for a function that allowed my application to post data and keeps the old url (mydomain/myaccount2/ausersave/90)
I find this function Redirect::back() but some post don't recommend to use it
I will apreciate your help. Thanks
My controller function is:
public function ausersave($code = 'null') {
$messg = null;
if(Input::get("userid") != null && Input::get("personid") != null) {
return View::make('PrivateController.ausersave', array('message' =>'Ok',
'user' => null, 'children' => null));
}
else if(isset($code)) {
return View::make('PrivateController.ausersave',
$this->ausersaveGet($code) );
}
return View::make('PrivateController.ausersave',
array('message' => '', 'user' => null,
'children' => null));
}
$this->ausersaveGet($code) -> this function bring me data form database and return me an array with thosse values array('message' => '', 'user' => $user, 'children' => $children); where user has info about user and children is an array of data. All this data return ok.
I would try taking the the ? out of the route parameter. i.e. change this:
Route::any("/myaccount2/ausersave/{code?}", array(......
to this:
Route::any("/myaccount2/ausersave/{code}", array(......

Categories