This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Tschallacka
Tschallacka

I have a Model wih the following fields:

Id(primary key), geo_poi_id(foreign key), searchterm

I want to pass a value to the

 /backend/exitcontrol/countrymanager/poisearchwordcontroller/create/

that says that geo_poi_id has to be selected on value 2(or whatever the current poi is we are editing, this number can be anything that'll be passed on by url) when creating a new entry.

And I have no idea how to format the url or access those values from within the controller or to pass it on to the display methods so the correct default values will be displayed.

Please help, the documentation is drawing a blank for any search term I can come up with... this should be really straight forward

Edit 1

I figured out that I can read the value of the url via $this->params

So in the controller I can read the value 2 with the url

/backend/exitcontrol/countrymanager/poisearchwordcontroller/create/2

by using the $this->params[0] which will give me the passed value 2 in the controller.

Now, how do I pass this on to my create view so it will default select value 2?

My controller:

<?php namespace ExitControl\CountryManager\Controllers;

use Flash;
use BackendMenu;
use Backend\Classes\Controller;
use ExitControl\CountryManager\Models\POIModel;
/**
 * Sliders Back-end Controller
 */
class POISearchWordController extends Controller
{
    public $implement = [
        'Backend.Behaviors.FormController',
        'Backend.Behaviors.ListController'
    ];
    public $formConfig = 'config_form.yaml';
    public $listConfig = 'config_list.yaml';

    public function __construct()
    {
        parent::__construct();

        BackendMenu::setContext('ExitControl.CountryManager', 'countrymanager', 'poi');
    }

    public function index_onDelete()
    {
        if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) {

            foreach ($checkedIds as $itemId) {
                if (!$slider = POIMOdel::find($itemId))
                    continue;

                $slider->delete();
            }

            Flash::success('Successfully deleted those selected.');
        }

        return $this->listRefresh();
    }
}

my fields.yaml

# ===================================
#  Form Field Definitions
# ===================================

fields:
    searchterm:
        span: left
        label: exitcontrol.countrymanager::lang.poimodel.searchterm

    poimodel:
        span: left
        nameFrom: poiname
        type: relation
        label: Point of Interest    

    #package:
    #    span: left
    #    type: relation

and my model

<?php namespace ExitControl\CountryManager\Models;

use Model;

/**
 * Slider Model
 */
class POIModel extends Model
{
    use \October\Rain\Database\Traits\Validation;
    /**
     * @var string The database table used by the model.
     */
    public $table = 'geo_poi';
    public $connection = 'exitproducts';
    public $timestamps = false;
    public $rules = [
        'poiname' => 'required|between:3,64',
    ];

    /**
     * @var array Guarded fields
     */
    protected $guarded = ['*'];

    /**
     * @var array Fillable fields
     */
    protected $fillable = [];

    /**
     * @var array Relations
     */
    public $hasOne = [];
    public $hasMany = ['poisearch' => ['ExitControl\CountryManager\Models\POISearchWordModel',  'key' => 'geo_poi_id']];
    public $belongsTo = [];
   /* public $belongsToMany = ['package' => [
            'ExitControl\CarManager\Models\PManager', 'table' => 'exitcontrol_cars_to_packages' 
        ]   
    ];*/
    public $belongsToMany = [];
    public $morphTo = [];
    public $morphOne = [];
    public $morphMany = [];
    public $attachOne = [];
    /*public $attachMany = [
        'images' => ['System\Models\File', 'order' => 'sort_order'],
    ];*/
    //public $jsonable = ['package'];
    public function getpackageOptions($keyValue = null) {
        return $this->lists('poiname');
    }
}

and my create.htm

<?php Block::put('breadcrumb') ?>
    <ul>
        <li><a href="<?= Backend::url('exitcontrol/countrymanager/poisearchwordcontroller') ?>"><?= e(trans('exitcontrol.countrymanager::lang.create.sliders')); ?></a></li>
        <li><?= e($this->pageTitle) ?></li>
    </ul>
<?php Block::endPut() ?>

<?php if (!$this->fatalError): ?>

    <?= Form::open(['class'=>'layout-item stretch layout-column']) ?>

        <?= $this->formRender() ?>

        <div class="form-buttons layout-item fix">
            <div class="loading-indicator-container">
                <button
                    type="submit"
                    data-request="onSave"
                    data-hotkey="ctrl+s"
                    data-hotkey-mac="cmd+s"
                    data-load-indicator="<?= e(trans('exitcontrol.countrymanager::lang.create.creating')); ?>"
                    class="btn btn-primary">
                    <?= e(trans('exitcontrol.countrymanager::lang.create.create')); ?>
                </button>
                <button
                    type="button"
                    data-request="onSave"
                    data-request-data="close:1"
                    data-hotkey="ctrl+enter"
                    data-hotkey-mac="cmd+enter"
                    data-load-indicator="<?= e(trans('exitcontrol.countrymanager::lang.create.creating')); ?>"
                    class="btn btn-default">
                    <?= e(trans('exitcontrol.countrymanager::lang.create.createclose')); ?>
                </button>
                <span class="btn-text">
                    <?= e(trans('exitcontrol.countrymanager::lang.create.or')); ?> <a href="<?= Backend::url('exitcontrol/countrymanager/poisearchwordcontroller') ?>"><?= e(trans('exitcontrol.countrymanager::lang.create.cancel')); ?></a>
                </span>
            </div>
        </div>

    <?= Form::close() ?>

<?php else: ?>

    <p class="flash-message static error"><?= e($this->fatalError) ?></p>
    <p><a href="<?= Backend::url('exitcontrol/countrymanager/poisearchwordcontroller') ?>" class="btn btn-default"><?= e(trans('exitcontrol.carmanager::lang.create.return')); ?></a></p>

<?php endif ?>

Last updated

corentin8023
corentin8023

did you find a solution because I am in the same situation as you

Tschallacka
Tschallacka

Good news everyone, yes I did :-) By extending the fields via the controller.

public function formExtendFields($form)
    {
        $para = null;
        if(isset($this->params[0]) && substr($this->params[0],0,2) === 'sw' ) {
            $para = $this->params[0];
        }
        else if(Session::get('poimodel')) {
            $para = Session::get('poimodel');
        }

        //throw new ValidationException(['poimodel'=>Cookie::get('poisearchwordpoiid','NOT SET')]);
        if($para !== null ) {   
            Session::put('poimodel',$para);
            $this->vars['para'] = $para;
            //$number = $this->asExtension('FormController')->formGetContext();
            $form->addFields([
                    'poimodel' => [
                            'span'=> 'left', 
                            'nameFrom'=>'poiname',
                                'type'=>'relation',
                                'label'=>'Point of Interest',
                                'default'=>substr($para,2)
                    ],
            ]);
        }
    }
corentin8023
corentin8023

Yeah ! I will test but how you call this function ?

fitodac15814
fitodac15814

Thank you! I spend a lot of hours looking for something like this.

wilirius15434
wilirius15434

Tschallacka - I'm a bit confused as to where you changed the parameter to include 'sw' at the beginning - all of the forms I create just have the ID parameter. If instead you usually set the Session variable, other than in this function, where did you do that at?

comger
comger

How do you get url var value in create.htm?

1-7 of 7

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.