Ad

Octobercm Backend List Maximum Limit Of Selections

I am developing a library system, Octobercms, and I need to limit the maximum number of books for each loan, for example: 2. So that when you reach the number of selections, do not allow me to select more books for that loan.

see the image: Backend List

Please help me!

Ad

Answer

To make it work we need to take different approach, instead adding error when we are selecting we can throw error when saving, (because adding there java-script is little hard as well its client side so insecure).

just add this code to your controller and change fields according to need.

public function onRelationManageAdd() {
    $checked = \Input::get('checked');
    $field = \Input::get('_relation_field');
    $maximumAllowed = 2;
    // i have used comment $field you need to replace it with your field
    // this will be your hasMany relational field name
    // also added condition to check selected id is more then 2
    if($field == 'comments' && is_array($checked) && count($checked) > $maximumAllowed) {

        // if selected id is more then 2 we add flash error and return blank array
        \Flash::error('You Can Select Only 2 !');
        return [];
    }

    // ADDITIONAL CHECK if you need more check you can add it here and return error
    if($field == 'comments' && isset($this->params[0])) {
        // currently editing record id.
        $currentEditRecordId = $this->params[0];
        $record = \October\Test\Models\Post::find($currentEditRecordId);
        if($record && $record->comments->count() >= $maximumAllowed) {
            \Flash::error('You Can Select Only 2 !');
            return [];
        }
    }

    // if everything is good then handle request by relation manger 
    //and return response
    return $this->asExtension('RelationController')->onRelationManageAdd();
}

I added ADDITIONAL CHECK because this will allow user to select 2 records per one time, but user can select 2 records multiple time but we don't allow them ;)

and you can add custom validations you like.

update

problem : select 1 record the first time and then select another record - solution

public function onRelationManageAdd() {
    $checked = \Input::get('checked');
    $field = \Input::get('_relation_field');

    $maximumAllowed = 2;
    $count = 0;

    // i have used comment $field you need to replace it with your field
    // this will be your hasMany relational field name
    // also added condition to check selected id is more then 2
    if($field == 'comments' && is_array($checked)) {
        //$count += count($checked);            
        $count = $count + count($checked);         
    }

    // ADDITIONAL CHECK if you need more check you can add it here and return error
    if($field == 'comments' && isset($this->params[0])) {
        // currently editing record id.
        $currentEditRecordId = $this->params[0];
        $record = \October\Test\Models\Post::find($currentEditRecordId);
        if($record) {
            $count = $count + $record->comments->count();
        }
    }

    if($count > $maximumAllowed) {

        // if selected id is more then 2 we add flash error and return blank array
        \Flash::error('You Can Select Only 2 !');
        return [];
    }

    // if everything is good then handle request by relation manger 
    //and return response
    return $this->asExtension('RelationController')->onRelationManageAdd();
}

Information: you can add this relational field in update context as when user is adding new record that time we don't have information for current record as current record is not saved yet so relation count will not be available in second check and our validation will fail.

to avoid this issue you can add update context for that field and that field will only available in update.

banner:
    label: Banner
    oc.commentPosition: ''
    mode: file
    span: auto
    type: mediafinder
    context: update <------ add this option

Now this field will only showed when user save record. and we are now good with our validation.

if you get any error please comment.

Ad
source: stackoverflow.com
Ad