更改嵌入的表单域的验证器

时间:2011-03-26 23:03:41

标签: symfony1

我有这个架构:

propel:
  autor:
    id: ~
    nombre: { type: varchar, size: 255, required: true }

  libro:
    id: ~
    autor_id:   { type: integer, size: 11, foreignTable: autor,
foreignReference: id}
    titulo:     { type: varchar, size: 255 }
    paginas:    { type: varchar, size: 255, required: true }

和这个表单类:

class AutorForm extends BaseAutorForm
{
  public function configure()
  {
    $this->embedRelation('Libro');
  }

public function foo()
{
    $this->validatorSchema['Libro']['newLibro1']['paginas'] = new
sfValidatorPass();
    return $this;

}
}

我在bind()之后调用foo()(在processForm()内部。)

提交Auto-Libro表单后,如果我没有插入任何内容 它显示了第一个嵌入形式(Libro)的字段'paginas' “需要”。

但是......为什么如果paginas有验证者传递?

编辑:在matt的回答之后,这是我的代码:

var_dump($this->embeddedForms['Libro']->validatorSchema['newLibro1']['paginas']);
$this->embeddedForms['Libro']->validatorSchema['newLibro1']['paginas'] = new sfValidatorPass(array('required' => false));
var_dump($this->embeddedForms['Libro']->validatorSchema['newLibro1']['paginas']);

打印出来:

object(sfValidatorString)[152]
  protected 'requiredOptions' => 
    array
      empty
  protected 'defaultMessages' => 
    array
      'required' => string 'Required.' (length=9)
      'invalid' => string 'Invalid.' (length=8)
      'max_length' => string '"%value%" is too long (%max_length% characters max).' (length=52)
      'min_length' => string '"%value%" is too short (%min_length% characters min).' (length=53)
  protected 'defaultOptions' => 
    array
      'required' => boolean true
      'trim' => boolean false
      'empty_value' => string '' (length=0)
      'max_length' => null
      'min_length' => null
  protected 'messages' => 
    array
      'required' => string 'Required.' (length=9)
      'invalid' => string 'Invalid.' (length=8)
      'max_length' => string '"%value%" is too long (%max_length% characters max).' (length=52)
      'min_length' => string '"%value%" is too short (%min_length% characters min).' (length=53)
  protected 'options' => 
    array
      'required' => boolean true
      'trim' => boolean false
      'empty_value' => string '' (length=0)
      'max_length' => int 255
      'min_length' => null

object(sfValidatorPass)[196]
  protected 'requiredOptions' => 
    array
      empty
  protected 'defaultMessages' => 
    array
      'required' => string 'Required.' (length=9)
      'invalid' => string 'Invalid.' (length=8)
  protected 'defaultOptions' => 
    array
      'required' => boolean true
      'trim' => boolean false
      'empty_value' => null
  protected 'messages' => 
    array
      'required' => string 'Required.' (length=9)
      'invalid' => string 'Invalid.' (length=8)
  protected 'options' => 
    array
      'required' => boolean false  <<<<<<<<<<<< FALSE <<<<<<<<<<<<<<<<<<
      'trim' => boolean false
      'empty_value' => null

问题仍然存在:当我尝试提交表单时,仍然需要字段'paginas'。为什么呢?

sf 1.4 / propel 1.6

Javi

1 个答案:

答案 0 :(得分:0)

几乎所有验证器,包括sfValidatorPass,默认为required = true。

我认为sfValidatorPass并不意味着“它总是通过验证”,它意味着“通过提交的值而不清理它或检查任何东西。”

  

sfValidatorPass是一个身份验证器。它只返回未修改的值。

所以我认为你可以做到

$this->validatorSchema['Libro']['newLibro1']['paginas'] = new 
    sfValidatorPass(array('required' => false));