Yii Framework Forum: Calc And Validate Many Relative Tabular Input Fields - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Calc And Validate Many Relative Tabular Input Fields Rate Topic: -----

#1 User is offline   yujin1st 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 13-November 12

Posted 02 March 2013 - 08:20 AM

Hello!
There is a form, that contains many (>100) relative fields (models Document, Field, Value).
Some of them has to be summed up, multiplied. Results have to be compared to each other...
Question is how and where (in controller or model) would be right to implement the math and it's validation?
Below i've described model and both possible solution, but there are major disadvantage in each one.
0

#2 User is offline   yujin1st 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 13-November 12

Posted 02 March 2013 - 08:36 AM

1st: Controller
If solution will be implemented in controller, all math should and data's save logic should be written in it... But, in this case, the new questions appear, if we want to process our data somewhere else
Getting of the data and it's independent validation is made as "Collecting Tabular Input" article (http://www.yiiframew...1/en/form.table)
class DocumentController extends Controller{
  public function actionUpdate() {

    /** Getting already saved data */
    $values = $document->values;

   /** Going over every field and create models for a values*/
    foreach ($allFields as $fieldId) {
      if (!$values[$fieldId]) {
        $value = new Value();
        $value->documentId = $document->id;
        $value->fieldId = $fieldId;
        $values[$fieldId] = $value;
      }

    /** Is Form submitted? */
    if (isset($_POST['Document'])) {
      /** Checking values */
      $valuesValid = true;
      foreach ($values as $i => $value) {
        if (isset($_POST['Document'][$i]))
          $value->attributes = $_POST['Document'][$i];
        $valuesValid = $value->validate() && $valuesValid;
      }

      if ($valuesValid) {
        /** Do and validate the math  */
       $allOk = true || false;
      }

      /** Saving if all's ok */
      if ($allOk) {
        foreach ($values as $value) {
          $value->save(false); 
        }
       $this->redirect('somewhere');
      }


    /** Render */
    $this->render('view' array(
      'document' => $document,
      'values' => $values,
      'fields' => $fields,
    ));

  }
}
 

0

#3 User is offline   yujin1st 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 13-November 12

Posted 02 March 2013 - 08:38 AM

2nd: Model
If math is implemented in model, there is a problem: we can't work with $document->fields: it doesn't exist before save, and this field is read-only. That's why we should make a fuss wigh extra field:
class Document extends CActiveRecord{
  public $tempFields;

/**
  * Math and validation by this extra field $tempFields
  * @return boolean  */
  public function calc(){ }

/** saving values */
  public function Document::saveData(),
}


In controller we should:
1. get all data and validate each field
2. assign data to $document->tempFields
3. call $document->calc(), which will do the math and raise errors
4. if all's fine, call $document->saveFields(),

Finally, controller's code won't get less, and what should we do if we need to change the data.
It will be already in $document->field, but math is tied on $document->tempFields.
Should we assign all values to temp ones? Pass as parameters to math function?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users