How to bind a array with a single data to a form u

2019-06-14 10:06发布

问题:

I need to add a variable to be displayed as a default in the form in the widget area, I have set a filter to select a single value according to a different name in the dropdown. i have set the selected data to a session. and from that session i have taken that data to the action file. I want to display the selected data in the form dropdown with out creating a submit event.

the form is displayed below

 public function configure() {

      //the dropdown
    $cost_range[''] = '-- Please select --';
    for ($i = 0; ($i <= 100); $i++) {
        $cost_range[$i] = $i;
    }

    $this->setWidgets(array(
        'user_id' => new sfWidgetFormDoctrineChoice(array('model' => 'Person', 'add_empty' => '-- Please select --'), array('onchange' => 'filerUnitCostByName()', 'id' => 'user_id')),
         //widget I need to dispaly the seleted value
        'unit_cost' => new sfWidgetFormSelect(array('choices' => $cost_range), array('id'=>'unit_cost')),
    ));
    $this->widgetSchema['user_id']->addOption('order_by',array('name','asc'));
    $this->setValidators(array(
        'user_id' => new sfValidatorString(array(), array('required' => 'User is required')),
    ));

    $post_validator = new sfValidatorAnd();
    $post_validator->addValidator(new sfValidatorCallback(array('callback' => array($this, 'checkPostValidations'))));
    $this->validatorSchema->setPostValidator($post_validator);
    $this->validatorSchema->setOption('allow_extra_fields', true);
    $this->validatorSchema->setOption('filter_extra_fields', false);
    $this->widgetSchema->setNameFormat('cost[%s]');
}

The Action file

public function execute($request) {
    $contributionService = $this->getContributionService();
    $this->form = new UnitCostForm();
    $this->valueSet = $contributionService->getCostCenterList();
    $this->userList = $contributionService->getUserLists();
    //get value from session
    $this->unitCost = $this->getUser()->getAttribute('unit_cost');
    //assign variable to session value
     $value_lists = ($this->unitCost);
    //select value from foreach
    foreach($value_lists as $values)
        {
        echo $values['unit_cost'];
        }

This is where I have got the data from a array and then taken to a foreach to select the single value. I want to display the single value in the form with out a submit. How can I do it?