0

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!

1 Answers1

1

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.

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
  • Thank you! It worked now only allows to select 2 records, but now if the user select 1 record the first time and then select another record it is allowing to select more than 2 – Ronaldo Ribeiro de Sousa Jan 27 '18 at 16:00
  • updated answer please try `updated answer` and let me know – Hardik Satasiya Jan 27 '18 at 16:45
  • When I create a new record I get an error: `"Undefined offset: 0" on line 46 of C:\wamp64\www\iepm.dev\plugins\ribsousa\library\controllers\Lendings.php` `line 46 $currentEditRecordId = $this->params[0];` – Ronaldo Ribeiro de Sousa Jan 28 '18 at 17:43
  • Just put an additional check there , updated answer please check from there – Hardik Satasiya Jan 29 '18 at 04:44
  • if($field == 'comments' && isset($this->params[0])) { ... add this condition as well – Hardik Satasiya Jan 29 '18 at 17:03
  • In the update works fine, but when I create a new record and select 1 the first time and then select 2 or more, the validation does not work and continues allowing to add more than 2 records – Ronaldo Ribeiro de Sousa Jan 30 '18 at 16:39
  • then i would suggest use this : https://stackoverflow.com/questions/48502709/octobercms-validation-relation-user question's answer for validation. it should work for both – Hardik Satasiya Jan 30 '18 at 18:01
  • 1
    Excellent answer HardikSatasiya , are you in octobercms slack ? i may need your help if you once join in slack please https://octobercms.slack.com/ and search for me with name as @mit and message me once in private . that would be great . thanks – Mittul At TechnoBrave Feb 01 '18 at 05:07