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

phplee
phplee

Hi

I have an order form which has a product dropdown. I want to populate the price field based on the product selected. i have noticed there is a dependOn option for dropdown fields but was sure how to set a field value based on a dropdown.

any help would be great!

scottstone72
scottstone72

I can try to help you.

So first thing in your in your form_config.yaml do the following:

country:
    label: Country
    type: dropdown

state:
    label: State
    type: dropdown
    dependsOn: country

The state field depends on the country selection.

Populate the dropdown options by adding the functions below to your model

public function getCountryOptions()
{
    return ['au' => 'Australia', 'ca' => 'Canada'];
}

public function getStateOptions()
{
    if ($this->country == 'au') {
        return ['act' => 'Capital Territory', 'qld' => 'Queensland', ...];
    }
    elseif ($this->country == 'ca') {
        return ['bc' => 'British Columbia', 'on' => 'Ontario', ...];
    }
}

Once the first selection is made, query the database based on that selection and format the array to return for the state options dropdown.

Hope that helps.

Last updated

phplee
phplee

Hi,

Ive tried this exact method. which works for dropdown. I want to populate a text input based on the dropdown. not return a array list.

Thanks for you reply.

dragontree
dragontree

This should get you going:

form_config.yaml example:

code:
    label: Code
    required: 1
    type: text
name:
    label: Name
    required: 1
    type: text
    dependsOn: code

And in the model, use the filterFields function

public function filterFields($fields, $context = null)
{
    if (empty($this->code))
        return;

    // do something to get the name value based on the code
    $newName = '';

    $fields->name->value = $newName;

}
phplee
phplee

Thanks for the reply. that worked!

Last updated

1-5 of 5

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