protected function attachRules(&$grid, array $rules = null)
    {
        $fieldsets = $grid->fieldsets();

        if (!empty(Input::get('ajax')) && Request::ajax() && !is_null($key = Input::get('key'))) {
            $name      = Crypt::decrypt($key);
            $fieldsets = $this->fieldPermissionAdapter->resolveFields($fieldsets, $name);
            if (empty($fieldsets)) {
                return false;
            }
        }
        $controls = [];
        foreach ($fieldsets as $fieldset) {
            foreach ($fieldset->controls as $control) {
                array_push($controls, method_exists($control, 'getName') ? $control->getType() : $control->name);
            }
        }

        $rulesDispatcher = new RulesDispatcher($rules);
        $grid->rules($rulesDispatcher->getSupported($controls));

        foreach ($fieldsets as $fieldset) {
            foreach ($fieldset->controls as $control) {
                $validation = $this->resolveRules($control, $rules);

                if (method_exists($control, 'setAttributes')) {
                    $control->setAttributes(array_merge($control->getAttributes(), $validation));
                } else {
                    $control->attributes = array_merge($control->attributes, $validation);
                }
            }
        }
    }