I'm trying to validate form field with array value.
$this->validate($request, [
'includes' => 'required',
'excludes' => 'required'
]);
HTML is as follows:
<input type="text" name="excludes[]" id="package_exclude"
class="form-control">
<input type="text" name="includes[]" id="package_include"
class="form-control">
The outputted value of $request->includes gives ["Include Value One","Include Value Two"] which is working fine..
But the validation doesn't works..
I suppose you want the arrays to have at least one value? In this case, you can use the min validation rule:
$this->validate($request, [
'includes' => 'required|min:1',
'excludes' => 'required|min:1'
]);
Related
In my project, I got many forms, so I've decided to specify each one with an iscription field, for exemple: Kids' form => <input = 'hidden' name = 'inscripted_in' value = 'kids'>. I want to set each one with a default value, but whenever I sign in, I get this error message:
SQLSTATE[HY000]: General error: 1364 Field 'inscripted_at' doesn't have a default value
Although when I go to Laravel Debug, I still get the inserted constant value, what's the problem?
This is one of my forms
<div class="InputBox">
<input type="hidden" name="inscripted_at" value="Adults">
<input type="hidden" name="status" value="pending">
</div>
my controller:
public function store(Request $req)
{
$this->validate($req,[
'name' => 'required|max:120',
'surname' => 'required|max:120',
'job' => 'required|max:120',
'day' => 'required',
'month' => 'required',
'year' => 'required',
'hobby' => 'required|max:120',
'help' => 'required|max:120',
'place' => 'required|max:120',
'residence' => 'required|max:120',
'email' => 'required|email|unique:users',
'photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'scholar_year' => 'required|max:120',
'tel' => 'required|regex:/(05)[0-9]{8}/',
]);
Chababounauser::create($req->all());
return redirect()->route('chababounausers.index')
->with('success','chababouna User inserted successfully.');
}
Check the fillable property of the Chababounauser model.
P.S.: Please, don't put spaces in HTML between attribute name and it's value.
Isn't 'inscripted_at' a default column in the database giving the date of inscription ? If so, you can't default a value to that kind of output.
I'm wondering how to map form field to eloquent model. Thing is that form input field has different name than eloquent model.
This is what i have
Model
class Message extends Model
{
protected $fillable = [
'name', 'email', 'subject_id',
];
}
Form
<form action="{{ action('MessageController#store') }}" method="post">
<input id="name" name="name" type="text">
<input id="email" name="email" type="text">
<select id="subject" name="subject">
#foreach ($subjects as $subject)
<option value="{{ $subject->id }}">{{ $subject->title}}</option>
#endforeach
</select>
</form>
Controller
public function store(Request $request)
{
$message = $this->validate(request(), [
'name' => 'required',
'email' => 'required',
'subject' => 'required',
]);
Message::create($message);
}
Notice that form select field name is subject and Message model field is subject_id.
Is it possible to map these two fields in Message model?
I guess it's possible in controller with something like
Message::create([
'name' => $request->input('name');
'email' => $request->input('email');
'subject_id' => $request->input('subject');
]);
but that's not what i want.
I don't expect some code improvements or suggestions as i'm complete Laravel noob :)
You could add a mutator on the Message model.
public function setSubjectAttribute($value) {
$this->attributes['subject_id'] = $value;
}
That essentially tells eloquent there is a subject attribute on the model but under the hood you're modifying subject_id
I have a checkboxes like this:
<div class="form-group">
<div style="display:none ;" class="weekday_message form-control alert-warning"></div>
<label id="weekday2" for="weekday" class="col-md-4 control-label">Weekday</label>
<div class="required form-field" name="weekday" id="weekday">
<input class="weekday" type="checkbox" name="weekdays[]" value="MO">Monday
<input class="weekday" type="checkbox" name="weekdays[]" value="TU">Tuesday
<input class="weekday" type="checkbox" name="weekdays[]" value="WE">Wednesday
<input class="weekday" type="checkbox" name="weekdays[]" value="TH">Thursday
<input class="weekday" type="checkbox" name="weekdays[]" value="FR">Friday
<input class="weekday" type="checkbox" name="weekdays[]" value="SA">Saturday
<input class="weekday" type="checkbox" name="weekdays[]" value="SU">Sunday
</div>
<span class="help-block">
<strong></strong>
</span>
</div>
My validation:
public function rules()
{
return [
'startdate' => 'required|date',
'endate' => 'nullable|date',
'startime' => ['required', new Time],
'endtime' => ['required', new Time],
'title' => 'required',
'entity_id' => 'required',
'type' => 'required|exists:entities,type',
'description' => 'required',
'frequency' => 'required',
'interval' => 'nullable|numeric',
'monthday' => 'nullable|numeric|min:1|max:3',
'weekdays' => 'array|max:3',
'month' => 'nullable|numeric',
'until' => 'nullable|date',
'tags' => 'nullable',
];
}
and controller:
public function storeEvent(EventRequest $request)
{
$test = ($request->input('weekdays'));
dd($test);
$weekday_string = implode(",", $request->input('weekdays'));
$request->merge(array('weekday', $weekday_string));
dd($request->all());
$event = DirtyEvent::create($request->all());
$geoloc_id = Entity::find($event->entity_id)
->first();
$user_id = Auth::id();
// Save Geoloc + user id into newly created event
$event->_geoloc()->associate($geoloc_id);
$event->users()->associate($user_id);
$event->save();
Now, validation seems to pass because it does data dump, however both dd($test) as well as $request->all() are giving me back empty weekdays, like it would not be defined. What could be the possible cause of this?
If you want to make sure you have always at least one weekday selected you should change:
'weekdays' => 'array|max:3',
into:
'weekdays' => 'array|required|max:3',
Also I suppose you don't send data using standard HTML form because you set for example name for divs so maybe you forget to attach weekdays or have bug in code elsewhere?
Your HTML says weekday (singular) but your rules set says weekdays (plural).
There needs to be at least one checkbox selected to make the input weekdays to be included in the request. You can use a default value in case none was selected by adding a hidden input before the checkboxes.
<input type="hidden" name="weekdays" value="defaultValue">
I have 4 input fields. 1 of them has to be filled.
My fields :
<input name="name" placeholder="Name">
<input name="hair_style" placeholder="Style">
<input name="hair_color" placeholder="Color">
<input name="options" placeholder="Options">
My function
$this->validate($request, [
'name' => 'required_if:hair_style,0,',
]);
So when hair_style is 0. Input field name has to be filled. This works but.. I want it like this below but I don't know how:
$this->validate($request, [
'name' => 'required_if:hair_style,empty AND hair_color,empty AND options,empty,',
]);
It has to work like this. When hair_style, hair_color and options are empty name has to be filled. But is this possible with required_if ?
You can try as:
'name' => 'required_if:hair_style,0|required_if:hair_color,0||required_if:options,0',
Update
You can conditionally add rules as:
$v = Validator::make($data, [
'name' => 'min:1',
]);
$v->sometimes('name', 'required', function($input) {
return ($input->hair_style == 0 && $input->hair_color == 0 && $input->options == 0);
});
You can add more logics in the closure if you required...like empty checks.
So all I had to do was :
$this->validate($request, [
'name' => 'required_without_all:hair_style, hair_color, options',
]);
for more information check https://laravel.com/docs/5.3/validation#rule-required-without-all
How to apply validation rules to every item within an items[] array? For example:
...->validate($request, [
'items[]' => 'required' // <-- what is the correct syntax?
]);
Try something like this
$validator = Validator::make($request->all(), [
'person.*.email' => 'email|unique:users',
'person.*.first_name' => 'required_with:person.*.last_name',
]);
Where person is the name of the input field and email is the key
Laravel 5.2 has an array validation all you need to do is :
In your view assuming that you have an inputs like this :
<input type="text" name="example[]" />
<input type="text" name="example[]" />
The [] are the key for this :)
And in your controller you can just do :
$this->validate($request, [
'example.*' => 'required|email'
]);
$this->validate($request, [
'items' => 'required|array',
'items.*.title' => 'required',
]);