A
A
Alexander Ivanov2018-09-17 17:32:36
Laravel
Alexander Ivanov, 2018-09-17 17:32:36

How to use Rule class in laravel?

public function store(Request $request){
...
 if ($data['boolean_field']) {
            $validator = Validator::make($request->all(), [
                'field1' => 'required|integer',
            ]);
        } else {
            $validator = Validator::make($request->all(), [
                'field2' => 'required|integer',
            ]);
        }
...
}

How it is possible to prewrite more competently I use Rule?
https://laravel.com/docs/5.7/validation#rule-boolean
protected function validator($data)
    {
        return Validator::make($data, [
...
             'field1'  => ['required|integer', Rule::boolean('boolean_field',true)], // моя попытка 
...
        ]);
    }

Or is it not through Rule, but somehow it can be done differently ...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2018-09-17
@cimonlebedev

$rules = [
    'field1' => ['required_if:boolean_field,true', 'integer'],
    'field2' => ['required_unless:boolean_field,true', 'integer'],
];

A
Artem Kiryanov, 2015-04-10
@hacker342

Have you tried searching, for example, jQuery slider. There are already a lot of these solutions. For example, I use Nivo Slider

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question