Welcome Guest, Not a member yet? Register   Sign In
Conditional validation?
#1

I am working on a form that has a set of checkboxes. Ones one checkbox is check with value "phone" I would like to then require a set of radio buttons. 
Right now I have this: 

PHP Code:
$validation = \Config\Services::validation();
 
$request = \Config\Services::request();
 
$rules = [
  'hardware' => [
    'label'  => 'Hardware',
    'rules'  => 'required',
    'errors' => [
      'required' => 'Choose hardware',
    ],
  ], 
 ];

 
// type of hardware if phone, require type of phone
 
if ($request->getPost('hardware[0]')) {
  $rules = [
    'phone_type' => [
      'label'  => 'Phone type',
      'rules'  => 'required',
      'errors' => [
        'required' => 'Choose type of phone',
      ],
    ],
 
  ];
 }

if (! 
$this->validate$rules )) {
  return view('form', [
    'validation' => $this->validator
  
]);
 } 
This works but it overwrites my previous $rules. How can I append a rule?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB