1

How can I write rule for input field like below:

{!! Form::number("amount[]",null,['min' => 0, 'class' => 'form-control col-xs-2 ']) !!}

I tried following which gave error: htmlentities() expects parameter 1 to be string, array given

        $rules = array(
            'amount[]' => 'required'
        );
        $this->validate($request, $rules);

Update:

I tried this as suggested by a user, it's not redirecting it on page again. Below is controller method:

public function postEstimate(Request $request) {
        $rules = array(
            'amount' => 'required|array'
        );

        $this->validate($request, $rules);
    }
5
  • You get this error because it consider amount[] as array,cant you call the field amount? Commented Sep 30, 2015 at 14:45
  • @Gal if you meant in rules then yes I already set it as amount but it's not catching it at all. Commented Sep 30, 2015 at 14:50
  • 2
    I meant both,form & rules,if you don't want to change that you could call it as amount.0 inside rules (in case this is your first amount[]) but I don't see why you would want to do it like this. Commented Sep 30, 2015 at 14:52
  • @Gal fields are Dynamic, not pre-defined. Commented Sep 30, 2015 at 14:53
  • 1
    I see,this might help you out - How to validate array input? its not exactly the same as you dont need valueKey all you need to do is iterate once just to build the array of rules for your amount array. if you dont have an option to delete the dynamic fields you might be able to use range function from 0 to array length. Commented Sep 30, 2015 at 15:00

4 Answers 4

3

I guess you got issues with what I explained so this is what I meant -

$rules = [];
$count_amounts = count($request->input('amount'));
foreach (range(0, $count_amounts) as $number) {
    $rules['amount.' . $number] = 'required|integer|min:0';
}

This should check that each amount input you have is an integer and is bigger than 0 (like you defined in the html validation)

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

Comments

1

Instead try this:

private  $rules = array(
            'amount' => 'required|array',
        );

public function postEstimate(Request $request) {


        $this->validate($request, $this->rules);
    }

or, try a validation with a 'amount' => 'required

im not sure about this 'amount' => 'required|array

1 Comment

By using amount only it's not detecting field at all. array rule introduced in Laravel 5.1
1

For custom rules implementation of integer type value check of an array

firstly open the following file

/resources/lang/en/validation.php

Then add the custom message

'numericarray'         => 'The :attribute must be numeric array value.',
'requiredarray'        => 'The :attribute must required all element.',

Again open the another file

/app/Providers/AppServiceProvider.php

Now add custom validation code in the boot function.

public function boot()
{

    $this->app['validator']->extend('numericarray', function ($attribute, $value, $parameters)
    {
        foreach ($value as $v) {
            if (!is_int($v)) {
                return false;
            }
        }
        return true;
    });

   $this->app['validator']->extend('requiredarray', function ($attribute, $value, $parameters)
    {
        foreach ($value as $v) {
            if(empty($v)){
                return false;
            }
        }
        return true;
    });

}

Now you can use the requiredarray for all element required of array. And also use the numericarray for integer type value check of array

$this->validate($request, [
            'field_name1' => 'requiredarray',
            'field_name2' => 'numericarray'
        ]);

Comments

0

if you expect amount as an array the rules should be

$rules = array(
    'amount' => 'required|array'
);

check the doc

If your not redirecting or getting a validation error means there is no validation error

just dd($request->input('amount')) in the controller and check its a array or not if it's a array then validations will pass.

2 Comments

It's not validating it at all.
The rule required|array still does not work as of 5.3.

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.