Symfony 1.4 form, conditional cast of sfValidatorError in inline form

I have a page object:

Page:
  actAs:
    Timestampable: ~
    I18n:
      fields: [name,content,attachment,course_name, course_description ]
      actAs:
        Sluggable: { fields: [name], uniqueBy: [lang, name], canUpdate: true }
  columns:
  ...
    is_course:            { type: boolean }
    course_name:          { type: string(255) }
    course_description:   { type: string(500) }

And PageForm with built-in i18n forms:

//PageForm.class.php 
public function configure()
{
...  
    $this->embedI18n(array('pl','en'));
    $this->widgetSchema->setLabel('en', 'Angielski');
    $this->widgetSchema->setLabel('pl', 'Polski');
}

The fields field_name and course_description are not required, and is_course is false. But if is_course is enabled, validation should cause an error, the required course_name and description_path.

I read the "Symfony Advanced Forms" manual and some other posts, but I don’t know whether to use sfValidatorCallback in PageForm or PostValidator in PageTranslationForm? I tried using sfValidatorCallback this way:

//PageForm.class.php
public function configure()
{
...
       $this->validatorSchema->setPostValidator(
          new sfValidatorCallback(array('callback' => array($this,'checkCourses')))
          );
}

public function checkCourses($validator, $values)
{
    if($values['is_course'])
    {
      if($values['pl']['course_name'] && !$values['pl']['course_description'])
      {
          $error = new sfValidatorError($validator,'Required filed');
          throw new sfValidatorErrorSchema($validator, array( _what_name_ => $error));
      }
    }
    return $values;
}

But I don’t know how to make a mistake in $ values ​​['pl'] ['course_description'], because the Symfony API says that _what_name_ should be an array of errors.

I am really confused what is during the process of validating forms in symfony.

//

PageTranslationForm, ...    //PageTranslationform.class.php

 public function configure()
 {
 //......

  $this->validatorSchema->setPostValidator(
          new sfValidatorCallback(array('callback' => array($this,'checkCourses')))
   );

 //.....
 }

public function checkCourses($validator, $values)
{
    if($values['course_name'] && !$values['course_description'])
    {
        $error = new sfValidatorError($validator,'Required');
        throw new sfValidatorErrorSchema($validator, array( 'course_description' => $error));
    }
    elseif(!$values['course_name'] && $values['course_description'])
    {
        $error = new sfValidatorError($validator,'Required');
        throw new sfValidatorErrorSchema($validator, array( 'course_name' => $error));

    }
    return $values;
}

, ... , PageForm is_course "true". "is_course" PageForm checkCourses PageTranslationForm?

//

, , , :

//PageForm.class.php
public function configure()
{
    $this->embedI18n(array('pl','en'));
    $this->widgetSchema->setLabel('en', 'Angielski');
    $this->widgetSchema->setLabel('pl', 'Polski');

//.....
  if($this->getObject()->getIsCourse())
  {
      foreach($this->getEmbeddedForms()  as $key => $form)
      {
          $this->validatorSchema[$key]->setPostValidator(
             new sfValidatorCallback(array('callback' => array($form,'checkCourses')))
          );
      }
  }
}

//PageTranslationForm.class.php
//configure etc

  public function checkCourses($validator, $values)
  {
       $errorSchema =  new sfValidatorErrorSchema($validator);


        if($values['course_name'] && !$values['course_description'])
        {
          $errorSchema->addError(new sfValidatorError($validator,'required'), 'course_description');
        }
        elseif(!$values['course_name'] && $values['course_description'])
        {
          $errorSchema->addError(new sfValidatorError($validator,'required'), 'course_name');
        }
        elseif(!$values['course_name'] && !$values['course_description'])
        {
          $errorSchema->addError(new sfValidatorError($validator,'required'), 'course_name');
          $errorSchema->addError(new sfValidatorError($validator,'required'), 'course_description');
        }


        if (count($errorSchema))
        {
          throw new sfValidatorErrorSchema($validator, $errorSchema);
        }

      return $values;
  }

, , , :)

+3
1

, .

:

, sfForm::renderGlobalErrors. , :

public function checkCourses($validator, $values)
{
  if ($invalid)
  {
    throw new sfValidatorError($validator, 'Required.'); //global messages should be more specific than this
  }
}

, , . , . , , .

public function checkCourses($validator, $values)
{
  if ($invalid)
  {
    $error = new sfValidatorError($validator,'Required filed');
    throw new sfValidatorErrorSchema($validator, array('course_description' => $error));
  }
}
+2

Source: https://habr.com/ru/post/1780856/


All Articles