So I'm getting a callback with two outcomes:
positive: url.com?scope=read_write&code={AUTHORIZATION_CODE}&state={csrf}
negative: url.com?error=access_denied&error_description=The%20user%20denied%20your%20request
I would like to validate all possibilities. My current approach is:
$data = request()->validate([
'scope' => 'required_without:error, error_description',
'code' => 'required_without:error, error_description',
'state' => 'required_without:error, error_description',
'error' => 'required_without:scope,code,state',
'error_description' => 'required_without:scope,code,state'
]);
Actually, it works super fine for every possibility but somehow not for:
$this->get(route('connect.index', [
'error' => $this->error,
'error_description' => $this->error_description,
]));
dd(session('errors'));
The weird part is that I get the error:
#messages: array:3 [
"scope" => array:1 [
0 => "The scope field is required when error / error description is not present."
]
"code" => array:1 [
0 => "The code field is required when error / error description is not present."
]
"state" => array:1 [
0 => "The state field is required when error / error description is not present."
]
]
But that is not true since I see that the values error, error_description are present!
array(2) {
["error"]=>
string(2) "ok"
["error_description"]=>
string(5) "state"
}
Update
#DigitalDrifter helped me, but now, I have got two new test-cases that fail!
$this->get(route('connect.index', [
'scope' => $this->scope,
'code' => $this->code,
'error' => $this->error,
'error_description' => $this->error_description,
]))
->assertStatus(302);
$this->get(route('connect.index', [
'scope' => $this->scope,
'code' => $this->code,
'state' => $this->error,
'error_description' => $this->error_description,
]))
->assertStatus(302);
As you might see I'm expecting to get a Response of 302 but I get 200. It should not be the case. I'm using #DigitalDrifter answer:
$data = request()->validate([
'scope' => 'bail|sometimes|required_without:error,error_description',
'code' => 'bail|sometimes|required_without:error,error_description',
'state' => 'bail|sometimes|required_without:error,error_description',
'error' => 'bail|required_without:scope,code,state',
'error_description' => 'bail|required_without:scope,code,state',
]);
Might have luck with sometimes:
Validating When Present
In some situations, you may wish to run validation checks against a field only if that field is present in the input array. To quickly accomplish this, add the sometimes rule to your rule list:
$data = request()->validate([
'scope' => 'bail|sometimes|required_without:error,error_description',
'code' => 'bail|sometimes|required_without:error,error_description',
'state' => 'bail|sometimes|required_without:error,error_description',
'error' => 'bail|required_without:scope,code,state',
'error_description' => 'bail|required_without:scope,code,state'
]);
Related
i'm integrating this login api on laravel:
endpoint: http://127.0.0.1:8888/api/v1/users/login/
parameter:
{"data":{
"email":"admin#mail.com",
"password":"admin#123",
"user_type":"1",
"encrypted":false
},"encoded_data":"yes"}
controller:
i'm posting data from form:
$request->validate([
'username' => 'required',
'password' => 'required',
'user_type' => 'required'
]);
$ds = $request->all();
$url = $this->base_api_url . 'api/v1/users/login/';
$apiRequest = [
'data' => [
'email' => $ds['username'],
'password' => $ds['password'],
'user_type' => $ds['admin'],
'encrypted' => false
],
'encoded_data' => 'yes',
];
$apiResponse = Http::acceptJson()->post($url, $apiRequest);
dd($apiResponse);
its return:
{"error":1,"success":false,"message":"Undefined index: data"}
this is the parameter i'm sending in $apiRequest:
array:2 [▼
"data" => array:4 [▼
"email" => "admin#mail.com"
"password" => "admin#123"
"user_type" => "1"
"encrypted" => false
]
"encoded_data" => "yes"
]
api working fine on postman:
I think you are requesting json format data so you need to get with by using this$request->json()->all() by using this you can access the key value.
Laravel Http::post sends a normal post request (form data). You should also send the Content-type: application/json header so the Laravel API understands it.
Try using the following:
$apiResponse = Http::accept('application/json')->post($url, $apiRequest);
// or
$apiResponse = Http::acceptJson()->post($url, $apiRequest);
I have this code here. Basically i want to make a post request to my weebhook but it is not making a successful request and it is returning 400 as a status code , what i am missing
Route::get('/testime',function (){
$response = Http::post(env('SLACK_WEBHOOK'),[
'content' => "Testing testing",
'embeds' => [
[
'title' => "Test ",
'description' => "URRA",
'color' => '7506394',
]
],
]);
return $response->status();
});
You are receiving a 400 error, which is Slack telling you, your data is missing or malformed. To debug you can do the following. Which will most likely state what is wrong.
dd($response->body);
Your structure seems like nothing Slack supports, which most likely will be the error message. See examples here, not certain this will even work.
$response = Http::post(env('SLACK_WEBHOOK'), [
'channel' => 'Your channel id',
'text' => 'Testing testing',
'blocks' => [
[
'type' => 'header',
'text' => [
'type' => 'plain_text',
'text' => 'URRA',
'color' => '#36a64f',
],
],
],
]);
there is a form with a select field "state", the value of which must be "draft" or "published". how to do it? does not work now.
$this->validate($request, [
'name' => 'required|unique:article_categories' . $category->id,
'description' => 'required|min:200',
'state' => 'draft' || 'published'
]);
you have to use Rule::in([])
$this->validate($request, [
'name' => 'required|unique:article_categories' . $category->id,
'description' => 'required|min:200',
'state' => [
'required',
Rule::in(['draft', 'published']),
]
]);
Is see you have answered your own question, but just to add to it: 'draft' || 'published' evaluated in PHP as true.
Meaning you are actually doing this:
$this->validate($request, [
....
'state' => true
]);
which makes no sense in terms of what is expected in the validation rules.
The correct way to do this is as you said:
$this->validate($request, [
...
'state' => [
'required',
Rule::in(['draft', 'published']),
]
]);
I am trying to format date with Carbon before importing my data due to the following reason
when importing dates they are changed to a value resembling this 101010(example) even though the excel value looks like 01/01/1001
Here is a discussion on it however I have had no luck with there suggested ideas and am wondering if there are any other recommenadations or if I am missing something?
here is my import ClientsImport.php
public function model(array $row)
{
return new Client([
'category' => $row[0],
'referral_type' => $row[1],
'first_name' => $row[2],
'middle_initial' => $row[3],
'last_name' => $row[4],
'occupation' => $row[5],
'dob' => \Carbon\Carbon::createFromFormat('m/d/Y', $row['6']),
'email' => $row[7],
'cell_phone' => $row[8],
'work_phone' => $row[9],
'has_spouse' => $row[10],
'spouse_first_name' => $row[11],
'spouse_middle_initial' => $row[12],
'spouse_last_name' => $row[13],
'spouse_occupation' => $row[14],
'spouse_dob' => \Carbon\Carbon::createFromFormat('m/d/Y', $row['15']),
'spouse_email' => $row[16],
'spouse_cell_phone' => $row[17],
'spouse_work_phone' => $row[18],
'street_address' => $row[19],
'city' => $row[20],
'state' => $row[21],
'postal_code' => $row[22],
]);
}
I have tried \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['6'])
and I get this error A non well formed numeric value encountered
And also tried \Carbon\Carbon::createFromFormat('m/d/Y', $row['6'])
and I get this error The separation symbol could not be found ↵The separation symbol could not be found
I am having a problem with validation - the validation doesn't pass and it also doesn't show what type of error is making it
$rules = array(
'first_name' => 'required|min:3',
'last_name' => 'required|min:3',
'email' => 'required|email|unique:users',
'company_name' => 'required|min:3',
'zip_code' => 'required|integer|between:3,10',
'address_1' => 'required|min:3',
'address_2' => 'required|min:3',
'city' => 'required|min:3',
'country' => 'required|min:3',
'state' => 'required|min:3',
'phone_num' => 'required|integer|between:4,10',
'security_answer' => 'required|min:10',
'password' => 'required|min:3|confirmed',
'password_confirmation' => 'required'
);
This is how I am retrieving errors
{{ $errors->first('first_name') }}
{{ $errors->first('last_name') }}
and so on
if I remove |between:4,10 from the phone_num and zip_codeeverything works fine otherwise it doesn't show me any error but also doesn't pass the validation
Is there something wrong I am doing here? I tried finding the answer online but couldn't understand the problem here
You should change validation rules
'zip_code' => 'required|integer|between:3,10'
'phone_num' => 'required|integer|between:4,10'
to
'zip_code' => 'required|digits|between:3,10'
'phone_num' => 'required|digits|between:4,10'
Your validation rule now for zip code expects an integer between 3 to 10. I suppose you need numeric value with length from 3 to 10.