ATK单选按钮与模型字段

ATK单选按钮与模型字段

问题描述:

How can I define radio buttons for a model field.

class Model_Campaign extends Model_Table {
  public $entity_code = 'campaign';
  function init() {
    parent::init();

    $this->addField('nombres')->mandatory(TRUE);
    $this->addField('email')->mandatory(TRUE);
    $this->addField('celular')->mandatory(TRUE);
    $this->addField('ciudad');
    $this->addField('operador')->mandatory(TRUE);
  }
}

I want to 'operador' field shows like radiobuttons, and I want to 'celular' field only accepts numeric values.

如何为模型字段定义单选按钮。 p>

  class Model_Campaign扩展了Model_Table {
 public $ entity_code ='campaign'; 
函数init(){
 parent :: init(); 
 
 $ this-> addField('nombres') - > 强制(TRUE); 
 $ this-> addField('email') - >强制(TRUE); 
 $ this-> addField('celular') - >强制(TRUE); 
 $  this-> addField('ciudad'); 
 $ this-> addField('operador') - > mandatory(TRUE); 
} 
} 
  code>  pre> 
  
 

我想'operador'字段显示像radiobuttons,我想'celular'字段只接受数字值。 p> div>

1# about radiobuttons

So, if you want to have radio buttons, this implies that either you have static list of values or you are referencing other model, thus do following:

a: $this->hasOne("Operador", "operador"), given you have Model_Operador
b: $this->addField("operador")->datatype("list")->listData(array());

this will make drop down to appear. If you want to have radio buttons: add ->display("radio");

2# validation - read documentation: http://agiletoolkit.org/doc/form/validation

->validateField('filter_var($this->get(), FILTER_VALIDATE_INT)');

e.g.

$this->addField("fieldx")->validateField('filter_var($this->get(), FILTER_VALIDATE_INT)');

n.b. make sure you use atk4.2 (master branch in github)

more filters: http://php.net/manual/en/filter.filters.validate.php

$this->addRadioAttribute("gender",array("male","female"));

here: gender is a field name, male and female are values.