This question already has answers here:
How do I style the HTML form validation error messages with CSS?
(4 answers)
Closed 3 years ago.
I modified the code like this: ArticleRequest.php
public function rules()
{
return [
'title' => 'required|min:2|max:255',
'slug' => 'unique:articles,slug,'.\Request::get('id'),
'content' => 'required|min:2',
'date' => 'required|date',
'status' => 'required',
'category_id' => 'required',
'description'=> 'required',
];
}
ArticleCrudController.php
$this->crud->addField([
'name' => 'description',
'label' => 'Description',
'type' => 'text',
'placeholder' => 'Description meta tag',
'attributes' => [
'required' => true,
],
]);
Why is the style different? something is missing?
thanks, you are very kind
Marco
public function rules()
{
return [
'title' => 'required|min:2|max:255',
// you should add validation rule for description attr
'description' => 'required',
'slug' => 'unique:articles,slug,'.\Request::get('id'),
'content' => 'required|min:2',
'date' => 'required|date',
'status' => 'required',
'category_id' => 'required',
'description'=> 'required',
];
}
Related
I would like to check if a customer already exists so that I can update his details or if he does not exist create it by starting a new subscription.
I created this, but I believe there is something wrong.
if(Auth::user()->subscribed('default')) {
$stripe->customers->update(Auth::user()->stripe_id, array([
'email' => Auth::user()->email,
'name' => 'Example Example',
'phone' => '322234455556',
'address' => [
'city' => 'Alcatraz',
'line1' => 'Via Dalle Palle',
'postal_code' => '96011',
'state' => 'Italia'
]
]));
} else {
$stripe->customers->create([
'description' => 'Example Example',
'email' => Auth::user()->email,
'name' => 'Example Example',
'phone' => '322234455556',
'address' => [
'city' => 'Alcatraz',
'line1' => 'Via Dalle Palle',
'postal_code' => '96011',
'state' => 'Italia'
]
]);
}
I think there is a mistake, because when I go to create a new subscription with a newly registered user, therefore not present on Stripe as a customer, it does nothing but create me two entries as in the image below.
I solved it this way
$stripeCustomer = Auth::user()->createOrGetStripeCustomer([
'description' => 'Example Example',
'email' => Auth::user()->email,
'name' => 'Example Example',
'phone' => '322234455556',
'address' => [
'city' => 'Alcatraz',
'line1' => 'Via Dalle Palle',
'postal_code' => '96011',
'state' => 'Italia'
]
]);
i' new to laravel here I have created a registration API and private function for validation but not show validation.
that is my validation function
`private function validateInfo($request, $toValidate) {
$validator = Validator::make($request->all(), $toValidate);
if ($validator->fails()) {
$errors = $validator->errors()->toArray();
$values = array_values($errors);
$arr = [];
foreach ($values as $errorlst)
array_push($arr, $errorlst[0]);
$error = implode(',', $arr);
return response()->json(['error' => true, 'message' => $error], 400);
exit;
}
}`
call function under switch case :-
switch ($request->user_type) :
case "teacher":
$this->validateInfo($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
break;
case "student" :
$this->validateInfo($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
break;
endswitch;
it is not showing validation error. Any Mistake?
Thank You
instead of this you can create a form request to validate your request.
class UserRequest extends FormRequest
{
protected function failedValidation(Validator $validator) {
throw new HttpResponseException(
response()->json([
"message" => implode(',',$validator->errors()->all())
],422));
}
public function rules(){
return [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
}
}
please check docs for more info https://laravel.com/docs/8.x/validation#form-request-validation
You Need to return For Display Like
switch ($request->user_type) :
case "teacher":
return $this->validateInfo($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
break;
case "student" :
return $this->validateInfo($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
break;
endswitch;
I am using yjra datatable and html builder plugin. i want to show two datatable in one view. i am getting error like can not reinitialize
Controller :
public function edit($id ,Builder $builder){
$this->htmlBuilder->ajax(route('backend.ajax.my_peoples', ['project_id'=> $project->project_id,'request' => 'ajax_listing']));
$people_html = $this->htmlBuilder->columns([
['data' => 'project_person_role', 'name' => 'project_person_role', 'title' => 'Person Role'],
['data' => 'project_person_sort', 'name' => 'project_person_sort', 'title' => 'Sort'],
['data' => 'project_person_status', 'name' => 'project_person_status', 'title' => 'Status'],
['data' => 'action', 'name' => 'action', 'title' => 'Action', 'orderable' => false, 'searchable' => false, 'width' => '10%'],
]);
$this->htmlBuilder->ajax(route('backend.ajax.my_consultants', ['project_id'=> $project->project_id,'request' => 'ajax_listing']));
$dt_html = $this->htmlBuilder->columns([
['data' => 'project_consultant_role', 'name' => 'project_consultant_role', 'title' => 'Consultant Role'],
['data' => 'project_consultant_sort', 'name' => 'project_consultant_sort', 'title' => 'Sort'],
['data' => 'project_consultant_status', 'name' => 'project_consultant_status', 'title' => 'Status'],
['data' => 'action', 'name' => 'action', 'title' => 'Action', 'width' => '5%', 'orderable' => false, 'searchable' => false]
]);
return view('backend.projects.projects')->with([
'view_type' => 'edit',
'view_title' => 'Update',
'cslt_html' => $dt_html,
'people_html' => $people_html
]);
}
The code does not have any syntax error, but does not work. It seems the code has no interaction with database.
public function execute(Request $request) {
if($request->isMethod('post')) {
$messages = [
'required' => "Campo :attribute rellenado",
'email' => "Este :attribute campo debe rellenarse con su email"
];
$this->validate($request,[
'name' => 'required|max:255',
'email' => 'required|email',
'text' => 'required'
], $messages);
dump($request);
edit your rules
$messages = [
'name.required' => 'This Field Name is required ',
'name.max' => 'The name should be :max in length',
'email.required' => 'the email field is required',
'email.email' => 'the email should be valid email',
'text.required' => 'the the text is required',
];
$this->validate($request,[
'name' => 'required|max:255',
'email' => 'required|email',
'text' => 'required'
], $messages);
the dump($requset) will not work in this case because you are using request trait where $this->validate function automatically redirect back if validation fails
Try to change dump($request) to dd($request)
if($request->isMethod('post')) {
$messages = [
'required' => "Campo :attribute rellenado",
'email' => "Este :attribute campo debe rellenarse con su email"
];
$this->validate($request,[
'name' => 'required|max:255',
'email' => 'required|email',
'text' => 'required'
], $messages);
dd($request);
in laravel validation (registering) i want to compare one of the fields with a php variable (it should be equal with that)
how can i do this?
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
}
You can do it for example for name field like this:
$variable = "something"
return Validator::make($data, [
'name' => [
'required',
Rule::in([$variable]),
],
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
Remember to import Rule Class (use Illuminate\Validation\Rule;)
You can get more info in: https://laravel.com/docs/5.4/validation#rule-in
EDIT
As suggested by #patricus, you can also concatenate the variable
$variable = "something"
return Validator::make($data, [
'name' => 'required|in:'.$variable,
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
EDIT2
If you have a variable that is an array:
$variable = ['one','two'];
return Validator::make($data, [
'name' => 'required|in:'.implode(",", $variable),
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
Or
$variable = ['one','two']
return Validator::make($data, [
'name' => [
'required',
Rule::in($variable),
],
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);