i have a form field with two options:
{!! Form::select('pet',array('1'=>'Dog', '2'=>'Cat') ,null,['class'=>'form-text']) !!}
is there any way to validate this values via $this->validate($request, [...]) or do i have to do this via if statement?
thanks for any sugestions.
You cant prevent people edit the data
You have to validate the data once you have the request in you server
Could try like this:
$this->validate(request(), [
'pet' => 'required|in:1,2'
]);
so only if pet value is 1 or 2 will pass.
not tested but should work
Related
Is there any way to get original data in request?
I mean, in my case I want to validate updating user's in the form. I want the email to be unique on emails in database except on the email that user wrote to the form.
Laravel already has a validation rule for this, you can add an id of the user to ignore to your unique validator.
'email' => [
'required',
Rule::unique('users')->ignore($currentUser->id),
]
I used something like this:
'email' => ['sometimes', 'email', 'unique:users,email,'.$user->getKey().','.$user->getKeyName().',deleted_at,NULL', 'string'],
Not sure if it is the best solution but works. I kind of do not like it because the $user is based on hidden input from the request.
You can do it liks this:
'email' => 'unique:users,email,'.$user->id
The user id will allow you to keep updating the record but maintain the unique constraint on email. You don't need a hidden input field for $user->id btw.
I want to use email validation in admin form of my custom module. I've seen core module but couldn't get exact idea.
You can also validate the 'email' text field by adding a class ['validate-email'] in your form
$fieldset->addField(
'email',
'text',
[
'name' => 'email',
'label' => __('Email'),
'title' => __('Email'),
'required' => true,
'class' => 'validate-email'
]
);
Validation is Model's issue. Only model knows how your data should look like. You describe your data fields in model, so you should describe validation rules for this fields in the same place.
It seems to be obvious for me, but I'd gladly listen to opponents.
put database validation in the Model (assuming it's a db model) and http data validation in the controller. Xss filtering, for example, does not pertain to the Model. it pertains to the Controller in input and to the View in output
I found an answer by myself, I used PHP email validation in the save controller.
I've a post value members and members is an array of values. How can I validate that one specific value is not in the list?
Example
$this->validate($request, [
'member_ids' => 'required|min:1|not_in:4',
]);
Thanks!
Laravel 5.2 has array validation.
So try this:
$this->validate($request, [
'member_ids.*' => 'required|not_in:4',
]);
I am not sure what it is you would like to achieve, but I think you can find a solution from here: http://laravel.io/forum/11-12-2014-how-to-validate-array-input
I'm using {!! Form::model( $data) !!} to get my edit working dynamically, also I have normal inputs like {!! Form::text('name', null) !!} but, within my controller, before I send my $data to my view, I use with like the following $data = User::with('Address')->find($id_user);
When I submit, I send my form to my controller update public function update(MyRequest $request) that merge both rules (address and user) to create the validation.
The problem:
The Address input to work with Form::model, I had to make it as an array like this {!! Form::text('Address[zipcode]', null) !!} and now, my validation doesn't work.
My request looks like this:
Array ( [_method] => PATCH [_token] => OlF0IuFxwTIucFavzUB9Sk1IGE5NJlaaarbBxSE [name] => Michel [Address] => Array ( [zipcode] => 80000400))
I was thinking if there is a way to indicate via rule that it should be looked inside the sub-array, something like Address[zipcode] but it didn't work
Got that working right after I posted the question....
$rules = ['Address.zipcode' => 'required']
Instead of quotes like before, using DOT will make you access the sub-array
How can i pass validation errors directly to a View without a redirect in Laravel?
I don't want to do that:
return Redirect::back()->withErrors($validator)->withInput();
but rather:
Input::flash(); // for repopulating fields
Validator::flash(); // this doesn't exist
return View::make('fragments/login_ajax');
Or another more concise direct generation of a view without a redirect. Optimal would be something like that, but that doesn't work:
return View::make('fragments/login_ajax')->withErrors($validator)->withInput();
The reason is, that this is the answer to an ajax based login. Redirecting to another method only used to display the same view deems unneccessary.
Cheers
Though agreeing it is a bad idea to do this, you can use View::share() to share the errors to all the views.
\View::share('errors', $errors);
Use below solution.
Controller:
$validator = Validator::make($request->all(),
[
'class_id' => 'required|numeric|not_in:0',
'batch_id' => 'required|numeric|not_in:0',
'student_id' => 'required|numeric|not_in:0'
]);
$this->viewData['errors'] = $validator->errors();
View File:
{!! Form::label('class_id','Student Class') !!}
{!! Form::select('class_id', array(), 0, ['class' => 'form-control', 'id' =>
'class_id']) !!}{!! $errors->first('class_id', '<p class="help-block">:message</p>')!!}
Real answer:
this is not an issue that is worth spending time on. It is an architecture problem.
Possible Solution to your situation (not recommended):
Make auth a new route and have it return json of errors/messages. Then have JS deal with the errors / messages / logic.