Block: Set default value inside the form

Permalink 1 user found helpful
In text area i want to set the "deafult" to 500 (when the block first added)
<div class="form-group">
<strong>W </strong> <i class="fa fa-arrows-h" aria-hidden="true"> | </i> <label class='control-label'><?= t('Thumbnail Max Width') ?></label>
 <input type="text" name="thumbnailMaxWidth" value="<?= $thumbnailMaxWidth ?>" class="form-control">
</div>


Also how to set deafult "radio" option in radio form.

I use this code :
<div class="form-group">
            <strong>Space around thumbnail</strong>
            <label class="control-label"><?= t('Select the space between the image and box') ?></label>
            <?php
               $filterTeaserOptions = array(
               'uk-panel-regular' => t('Regular space: Use box space'),
               'uk-panel-teaser' => t('No space: Image without any spacing(<a c
               'uk-panel-space' => t('Extra space: Image with large space around(@panel-space-padding)'),
               ); 
               foreach ($filterTeaserOptions as $gridTeaserOptionHandle => $spaceTeaserOptionLabel) {
                  $isChecked = ($filterTeaserOption == $gridTeaserOptionHandle) ? 'checked' : '';
               ?>
               <div class="radio">
                  <label>
                     <input type="radio" class='filterTeaserOption' name="filterTeaserOption" value="<?=$gridTeaserOptionHandle?>" <?php $isChecked?> />

siton
 
JohntheFish replied on at Permalink Reply
JohntheFish
You can have a $this->set(etc) in the controller add method.
MrKDilkington replied on at Permalink Best Answer Reply
MrKDilkington
Hi siton,

Default to 500:
In addition to JohntheFish's suggestion, you can set a default using a ternary operator.
$thumbnailMaxWidth ? $thumbnailMaxWidth : 500
- when first adding the block, $thumbnailMaxWidth will be false, and the value of 500 will be used as the default
- after a value has been entered and saved, $thumbnailMaxWidth is true, and its value will be used
<div class="form-group">
    <strong>W </strong>
    <i class="fa fa-arrows-h" aria-hidden="true"> | </i>
    <label class='control-label'><?php echo t('Thumbnail Max Width') ?></label>
     <input type="text" name="thumbnailMaxWidth" value="<?php echo $thumbnailMaxWidth ? $thumbnailMaxWidth : 500; ?>" class="form-control">
</div>

Regarding your second question, I am not sure what you are trying to do.

Here is an alternative using the concrete5 form widget and a ternary operator that sets the default checked radio button:
$filterTeaserOption ? $filterTeaserOption : 'uk-panel-regular'
- when first adding the block, $filterTeaserOption will be false, and the value of "uk-panel-regular" will be used as the default
- after the block form has been saved, $filterTeaserOption will be true, and its value will be used to determine which radio button receives the checked attribute
<div class="form-group">
    <?php echo $form->radio('filterTeaserOption', 'uk-panel-regular', $filterTeaserOption ? $filterTeaserOption : 'uk-panel-regular', array('id' => 'filterTeaserOption1')); ?>
    <?php echo $form->label('filterTeaserOption1', t('Regular space: Use box space')); ?>
    <br>
    <?php echo $form->radio('filterTeaserOption', 'uk-panel-teaser', $filterTeaserOption ? $filterTeaserOption : 'uk-panel-regular', array('id' => 'filterTeaserOption2')); ?>
    <?php echo $form->label('filterTeaserOption2', t('No space: Image without any spacing')); ?>
    <br>
    <?php echo $form->radio('filterTeaserOption', 'uk-panel-space', $filterTeaserOption ? $filterTeaserOption : 'uk-panel-regular', array('id' => 'filterTeaserOption3')); ?>
    <?php echo $form->label('filterTeaserOption3', t('Extra space: Image with large space around(@panel-space-padding)')); ?>
</div>

This is the HTML that this will produce:
<div class="form-group">
    <input type="radio" id="filterTeaserOption1" name="filterTeaserOption" value="uk-panel-regular" class="ccm-input-radio" checked="checked">
    <label for="filterTeaserOption1" class="control-label">Regular space: Use box space</label>
    <br>
    <input type="radio" id="filterTeaserOption2" name="filterTeaserOption" value="uk-panel-teaser" class="ccm-input-radio">
    <label for="filterTeaserOption2" class="control-label">No space: Image without any spacing</label>
    <br>
    <input type="radio" id="filterTeaserOption3" name="filterTeaserOption" value="uk-panel-space" class="ccm-input-radio">
    <label for="filterTeaserOption3" class="control-label">Extra space: Image with large space around(@panel-space-padding)</label>
</div>
siton replied on at Permalink Reply
siton
THANKS!! :) i hope in the future will be more DOCS about this issue (Form helper, and "edit/add.php"). Most of the docs is about "creating a block" talk about the view and the controller.

This help me a lot!! :)

One small Q: what the idea of "<default value="...> in the db.xml ?
<!-- Panel Modifier -->   
      <field name="filterPanelModifierOption" type="string" size="85" 
      comment="('no-modifier','uk-panel-box uk-panel-box-primary','uk-panel-box uk-panel-box-secondary')">
         <unsigned></unsigned>
         <default value="no-modifier"></default>
         <notnull></notnull>
</field>
MrKDilkington replied on at Permalink Reply
MrKDilkington
@siton

You can find more information on the form widget here:
"How to use the form widget in concrete5 5.7"
http://documentation.concrete5.org/tutorials/how-to-use-the-form-wi...

Regarding db.xml, I would recommend looking into Doctrine XML - "An XML representation for Doctrine database schemas.".

http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/...
"default - The default value to set for the column if no value is supplied."