PhoeniX5
4 months ago

Laravel error "array_map(): Argument #2 should be an array"

Posted 4 months ago by PhoeniX5

I am developing an application but i keep getting this error:

C:\Users\...\vendor\laravel\framework\src\Illuminate\Validation\ValidationRuleParser.php
        }
 
        return $rules;
    }
 
    /**
     * Explode the explicit rule into an array if necessary.
     *
     * @param  mixed  $rule
     * @return array
     */
    protected function explodeExplicitRule($rule)
    {
        if (is_string($rule)) {
            return explode('|', $rule);
        } elseif (is_object($rule)) {
            return [$this->prepareRule($rule)];
        }
 
        return array_map([$this, 'prepareRule'], $rule);//highlight
    }
 
    /**
     * Prepare the given rule for the Validator.
     *
     * @param  mixed  $rule
     * @return mixed
     */
    protected function prepareRule($rule)
    {
        if ($rule instanceof Closure) {
            $rule = new ClosureValidationRule($rule);
        }
 
        if (! is_object($rule) ||


"array_map(): Argument #2 should be an array"

Here is my code: Controller:

function magasin(Request $request) {
        $validation = Validator::make($request->all(), [
            'article'     =>  $request->get('article'),
            'qte_s'       =>  $request->get('qte_s'),
            //'qte_e'       =>  $request->get('qte_e'),
        ]);

        $error_array = array();
        $success_output = '';
        if ($validation->fails())
        {
            foreach($validation->messages()->getMessages() as $field_name => $messages)
            {
                $error_array[] = $messages;
            }
        }
        else
        {
            $mag = new Magasin([
                'article'     =>  $request->get('article'),
                'qte_s'       =>  $request->get('qte_s'),
                //'qte_e'       =>  $request->get('qte_e')
            ]);
            $mag->save();
            $success_output = '<div class="alert alert-success">Produit ajouté</div>';
        }
        $output = array(
            'error'     =>  $error_array,
            'success'   =>  $success_output
        );
        echo json_encode($output);
    }

View:

<script type="text/javascript">
        $(document).ready(function() {

    $(document).on('click', '#pub', function(){
        $('#magModal').modal('show');
    });

    $('#mag_form').on('submit', function(event){
    event.preventDefault();
    var form_data = $(this).serialize();
    $.ajax({
        url:"{{ route('validate.magasin') }}",
        method:"get",
        data:form_data,
        dataType:"json",
        success:function(data)
        {
            if(data.error.length > 0)
            {
                var error_html = '';
                for(var count = 0; count < data.error.length; count++)
                {
                    error_html += '<div class="alert alert-danger">'+data.error[count]+'</div>';
                }
                $('#form_output').html(error_html);
            }
            else
            {
                $('#form_output').html(data.success);
                $('#mag_form')[0].reset();
            }
        }
    })
    });
});
    </script>

Please sign in or create an account to participate in this conversation.