codefog / contao-haste

Haste is a collection of tools and classes to ease working with Contao
http://codefog.pl/extension/haste.html
MIT License
43 stars 24 forks source link

Default value is not working if there is a bound model #125

Closed cboelter closed 6 years ago

cboelter commented 6 years ago

I'm using the Form class with a bound Contao Model. If I want to set the default value of a field in the dca configuration like this:

'salutation' => [
  'label' => &$GLOBALS['TL_LANG']['xxx']['salutation'],
  'inputType' => 'radio',
  'options_callback' => ['xxx', 'getSalutationOptions'], // returns ['mr', 'mrs']
  'default' => 'mrs',
  'reference' => &$GLOBALS['TL_LANG']['xxx'],
  'eval' => [
    'mandatory' => true,
    'step' => 1,
  ],
  'sql' => ['type' => 'string', 'length' => 3],
],

The default value will be overwritten every time with the model value, even it is currently empty.

To fix this, I have changed this line: https://github.com/codefog/contao-haste/blob/master/library/Haste/Form/Form.php#L414 to the following:

if (!$arrDca['ignoreModelValue'] && $this->objModel !== null && $this->objModel->$strName) 

So the model value is only used, if there is a value for the field. Is this a bug or is there another way to set the default value?

Toflar commented 6 years ago

If you bind the model, the model is the master. Default values should be represented by the model too, otherwise what happens if I do something like this:

$model = new FoobarModel();

echo $model->salutation;

? It will not show mrs but it should ;-)