1

I have a table, where the user can fill in information. However, the user can add rows and/or delete rows. I used a cloning function for that.

My html looks like:

<input type="text" class="form-control" name="TB1_a[]">
<input type="text" class="form-control" name="TB1_b[]">

As you can see, it is an array input. So if the user adds a row, I'll have two values stored in TB1_a[] and TB1_b. Now, I would like to make my rules so that if the user enters information inside TB1_a[0], I would like to make TB1_b[0] required, but valid if both TB1_a[0] and TB1_b[0] are empty.

My validation rules:

'TB1_a.*' => 'required_with:TB1_b.*',
'TB1_b.*' => 'required_with:TB1_a.*'

However, these rules are unable to detect that I am referring to an array, meaning Laravel does not detect the '.*'. I would also like to point out that this logic was working perfectly with Laravel 5.4, but I have had to downgrade my Laravel and now it stops working. Any help please?

1 Answer 1

2

As I know, this[.*] notation was introduced in Laravel 5.2+. So, to achieve the array validation, you would need to add custom rules. Reference Link

public function rules()
{
  $rules = [
    'name' => 'required|max:255',//add more static rules as you need
  ];

  foreach($this->request->get('TB1_a') as $key => $val)
  {
    $rules['TB1_a.'.$key] = 'required_with:TB1_b.'.$key;
  }

  foreach($this->request->get('TB1_b') as $key => $val)
  {
    $rules['TB1_b.'.$key] = 'required_with:TB1_a.'.$key;
  }

  /*
  To combine the both validation rules in 1 loop. considering number of both fields are always equal
      foreach($this->request->get('TB1_a') as $key => $val)
      {
        $rules['TB1_a.'.$key] = 'required_with:TB1_b.'.$key;
        $rules['TB1_b.'.$key] = 'required_with:TB1_a.'.$key;
      }  
  */
  return $rules;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Ohh, yea this makes much more sense. I felt Laravel 5.0 might have some other type of notation, but I guess not. Thanks for all your help!
So I tried to do what you suggested, but I'm running into another problem. If I leave both empty, and progress it works as expected (i.e. Valid). However, if I fill in both of them, it says that the data is invalid. I am super confused as to what is happening.
I'm not sure what's wrong :(. Could you please try to print the values of $rules and post the op. just to check if rules are getting as we want them to.
Also, if you could add error message as given in reference link, that might help to see if which variable is failing (a or b)
Yea sure, I outputted $rules as well as all of the inputs I retrieve, just to make sure everything is working correct. My rules are: $rules => [ 'TB1_a.0' => 'required_with: TB1_b.0', 'TB1_b.0' => 'required_with:TB1_a.0', 'TB1_a.1' => 'required_with: TB1_b.1', 'TB1_b.1' => 'required_with: TB1_a.1' ] and this is if I have two rows.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.