4

I would need to Validate my inputted data, and it says

htmlentities() expects parameter 1 to be string, array given (View: /var/www/mw/app/views/delivery-reports/edit.blade.php)

Here is my Method:

 public static $rules_imei = array(
        'imei'             => 'required|alpha_dash|min:2',

         );

My Rules:

  public function __construct(
        DeliveryReport $delivery_report)
    {
        $this->deliveryReport = $delivery_report;
    }

this my controller validation:

 $data = Input::get('imei');

if($errors = $this->deliveryReport->isInvalidImei($data))
{
    return Redirect::back()->withInput()->withErrors($errors);
}

Here is my view:

@for ($qty = 0; $qty < $item_quantity; $qty++)
<br>
{{Form::text('imei[]',$qty)}}
<br>
@endfor
1
  • Been trying to implode this but still error occurs Commented Jul 18, 2014 at 7:48

2 Answers 2

7

Updated Answer (For old answer check the history)

There is undocumented core support for this (Thanks to Andreas Lutro for the hint).

$validator = Validator::make(Input::all(), []);
$validator->each('imei', ['required', 'alpha_dash', 'min:2']);

No extension needed.

Further Reading

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

3 Comments

How to customize the validator's error message so it doesn't say something like "The imei.0 field is ..."?
Nevermind, you just add a 3rd parameter as usual to the Validor::make declaration, with the value of the attribute shown. Ex: array('imei.0.required' => 'The IMEI field is required.')
Furthermore, to continue my conversation with myself, if you only desire the first instance of the input array to have validation (i.e. an array of files where only the 1st is required), you can additionally declare the 0-th index with your rules. Example: $validator = Validator::make(Input::all(), array('imei.0' => 'required'), array('imei.0.required' => 'Please provide at least one IMEI.') );
2

Make indexed array on view

@for ($qty = 0; $qty < $item_quantity; $qty++)
<br>
{{Form::text('imei['.$qty.']',$qty)}}
<br>
@endfor

Go through the loop to validate each

    $rules_imei = [];
    //make custom rule for each element of array
    foreach(Input::get('imei') as $k => $val){
       $rules_imei[$k] = 'required';
    }
    //to have custom message like '1st imei is required' 
    $niceNames = array(
        '0' => '1st imei',
        '1' => '2nd imei',
        '2' => '3rd imei',
        '3' => '4th imei'

    );      
    $v = Validator::make(Input::get('imei'), $rules_imei);
    $v->setAttributeNames($niceNames); 


    if ($v->fails())
    {
        return Redirect::back()->withErrors($v)->withInput();

    }

With this validation each option is compulsary, you can change as per your need

1 Comment

Hi thomas answer is much more easier, but still thank you timus.

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.