2

i have a table with checkbox as array and textbox as also array. what i want to achieve is when the user checked a checkbox it should validate that input textbox are not be empty.

public function roombooking(Request $request)
    {


      $messsages = array(
        'check.required'=>'No room was selected.Please select room to proceed for booking !',
        'txtnos.required|numeric'=>'Please enter no of persons',
    );
      $rules = array(
        'check'=>'required',
        'txtnos'=>'required_with:data', //txtnos is a array input filed  and data is a array checkbox 
        );
      $validator = Validator::make($request->all(),  $rules,$messsages
            );
      if ($validator->fails()) {

            return Redirect::back()
                ->withErrors($validator)
                ->withinput();

        }
}

Html code

<table class="table table-hover"  data-toggle="table"  id="table"
data-click-to-select="true">
    <thead>
        <tr>
            <th style="width:10%;" data-field="ActivityId">Select</th> 
            <th style="width:30%;" data-field="ActivityName">Activity Name</th>
            <th style="width:30%;" data-field="Rate">Rate/Person</th>
            <th style="width:30%;">Nos. of person</th>
        </tr>
    </thead>
    <tbody>                
           @foreach($loadactivity as $key=>$activity)
           <tr>
               <td>
                   <input type="checkbox" name="data[]"  value="0;{!! $activity->ActivityId !!};{!! $activity->Rate !!};0;0;{!! $activity->ActivityName !!}" />
               </td>
               <td>{!! $activity->ActivityName !!}</td>
               <td>{!! $activity->Rate !!}</td>
               <td >{!! Form::text('txtnos[]','',['class' => 'form-control small-textbox  ','txtnoid'=>$activity->ActivityId]) !!}</td>
           </tr>
           @endforeach
    
     </tbody>
</table>

please help me

1
  • Please provide the HTML for the form Commented Aug 5, 2016 at 10:42

1 Answer 1

6

Change your form:

...
<td><input type="checkbox" name="row[{{$key}}][data]" value="0;{!! $activity->ActivityId !!};{!! $activity->Rate !!};0;0;{!! $activity->ActivityName !!}" />
</td>
<td>{!! $activity->ActivityName !!}</td>
<td>{!! $activity->Rate !!}</td>
<td>{!! Form::text('row[{{$key}}][txtnos]','',['class' => 'form-control small-textbox  ','txtnoid'=>$activity->ActivityId]) !!}</td>
...

So the only thing that's changed is the name of data and txtnos, it will give you the following:

$exampleResult = [
    'row' => [
        // old $key as new key
        0 => [
            'txtnos' => 'entered value',
            'data' => '1', // But only if checked
        ],
        1 => [
            'txtnos' => 'entered value',
            'data' => '1', // But only if checked
        ],
    ]
];

Validation rules

$rules = [
    'row.*.txtnos' => 'required_with:row.*.data'
];

In the example, txtnos on each row is required if data on the same row isset.

Validation message

$messages = [
    'row.*.txtnos.required_with' => 'Enter a value or uncheck the checkbox..'
];

Important:
The validation for .*. was added in Laravel 5.2, you didn't specify your exact version so I'm not sure if it will work for you. Anyway, there is another way to do this.

For versions < 5.2, loop the input rows and replace the * in my example with the current key.

Sign up to request clarification or add additional context in comments.

2 Comments

i have update my question with HTML code. Please check and tell me how to set the rules
Good to hear. Please "accept the answer" to say Thank you.

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.