formers / former

A powerful form builder, for Laravel and other frameworks (stand-alone too)
https://formers.github.io/former/
1.34k stars 205 forks source link

public static $key on Model #527

Closed XavRsl closed 8 years ago

XavRsl commented 8 years ago

Hi, I'm trying to use former select in a situation where I have a related hasOne model that holds the 'name' value of the select, and a specific field on my main model that I want to use as 'value'. In the docs, it says that we can add elements on the model to achieve this :

public function __toString()
{
    return $this->relatedModel->name;
}

works fine. But, setting a static public $key that holds the name of the field containing the select 'value' doesn't work. Am I doing something wrong ?

Thanks.

Xavier

claar commented 8 years ago

Hey Xavier,

If I understand the question, the easiest way to handle that is to specify your options manually:

{!!
    Former
        ::select('my_field')
        ->options(RelatedModel::lists('name', 'key'))
        ->value($mainModel->my_field)
!!}
XavRsl commented 8 years ago

Hi ! Here's what I ended doing :

{!!
    Former::select('recruiters')
        ->fromQuery($recruiters, null, 'uid_ref')
        ->placeholder('Sélectionnez votre recruteur pour ce centre')
        ->addClass('chosen-select')
!!}

And in the model :

function __toString()
{
    $name = $this->prenom . ' ' . $this->nom_usuel;
    $name .= ($this->nom_usuel == $this->nom_patronymique) ? '' : ' (' . $this->nom_patronymique . ')';

    return $name;
}

According to the docs, I could have used a public static property called $key containing 'uid_ref' in order to avoid the relatively ugly fromQuery($recruiters, null, 'uid_ref') I've tried with options, value and the lot, but can't make it work properly...

Thanks for your help Claar !!

Xavier

claar commented 8 years ago

No problem, glad you got it worked out.