Ad

Get Input Values From Backend Form [octobercms]

Everything is working fine in this code except $result = Input::get('term'); Someone please help me to get the input value of field term. When i do this $result = 8; everything works well.

Form in plugin backend

In my plugin "Lesson" model

<?php

 namespace Cng\Tennis\Models;

 use Model;
 use Flash;
 use Db;
 use Input;
 use request;
 use Cng\Tennis\Models\Term as termModel;

 class Lesson extends Model
 {  
public function getSdateOptions () {

  $result = Input::get('term');
  $term = termModel::select('id','start_date')->where('id',  $result )->first();

   if ($this->term_id == $result ) { 
        return [$term->id => $term->start_date];   
    }
    else{
      return ['Select a date' => 'Select a date' ];
      }
    }

public function getFdateOptions () {
        return ['Select a date' => 'Select a date' ];
    }

In yaml

    term:
        label: Term
        nameFrom: name
        descriptionFrom: description
        span: auto
        containerAttributes: {  }
        type: dropdown
        emptyOption: Select
        tab: 'Event Details'
        dependsOn:
            - location

    sdate:
        label: 'Start Date'
        mode: date
        span: left1
        cssClass: ''
        required: 1
        dependsOn:
            - term
        type: dropdown
        tab: 'Event Details'
        disabled: 1
Ad

Answer

First off, your problem is caused by the fact that the term field is not named that in the HTML. If you look at the generated form inputs in the browser, or in the AJAX request that's sent when the dependsOn trigger is fired, you'll notice that the actual field name for the term field is prefixed with the Form widget's alias (probably Form, so in this case the actual field name is Form[term]).

However, you don't even have to use Input at all, you can just reference the model property the term field refers to directly in your function:

$options = [0 => 'Select a start date'];
$term = TermModel::find($this->term);

if ($term) {
    $options = [$term->id => $term->start_date];
}

return $options;

See https://octobercms.com/docs/backend/forms#field-dependencies for more information on how to properly utilize the dependsOn property. Note that you could probably combine it in your case with the trigger property to also hide the start_date field until you have a valid option selected in the term field.

Ad
source: stackoverflow.com
Ad