How to set default value for one field only if conditional is met

308 views Asked by At

I have a prize form with the followings fields:

artist -> Choice (Related model artist)
prize_type
year

When creating a new Prize object I don't have any problems. Now I want to have a button on my Artist view called Create Prize and when it is clicked it needs to go to a form but I want the artist field to be selected by default.

How can I set a default value for this field depending from which URL is coming from?

I tried to retrieve my Artist ID in the URL so I got something like this:

"prize/new?id_artist=23"

But I can't figure out how to insert that ID on the $form['id_artist'] field.

I have tried to use setDefault option but it is only for placeholders:

$this->form->setDefault('id_artist', $request->getParameter("id_artist"));

Also I hide the related field in my template since I don't want it when coming from that URL

<?php if(!$id_artist): ?>
            <div class="control-group <?php echo $form['id_artist']->hasError()?'error':''; ?>">
                <?php echo $form['id_artist']->renderLabel('Artist ',array('class' => 'control-label')) ?>
                <div class="controls">
                    <?php echo $form['id_artist']?>
                    <small class="help-inline">Artist</small>
                    <?php if($form['id_artist']->hasError()): ?>
                        <small class="help-inline">(<?php echo $form['id_artist']->getError() ?>)</small>   
                    <?php endif; ?> 
                </div>
            </div>
        <?php endif; ?>

I tried to automatically set id_artist directly to that parameter but instead, I got an error since artist in my form is required.

0

There are 0 answers