form template field problems

edited August 2014 in Modules
the template field is handy, however for my code

$f['awards']=array(
'repeatable'=>TRUE,
'add_extra' => FALSE,
'type' => 'template',
'display_label'=>FALSE,
'fields'=>array(
'year'=>array('type'=>'text','ignore_representative' => TRUE, 'required'=>TRUE),
'publication'=>array('type'=>'text','ignore_representative' => TRUE, 'required'=>TRUE),
'title'=>array('type'=>'text','ignore_representative' => TRUE, 'required'=>TRUE),
)
);

the purpose is to allow users to be able to click "add another" to populate more awards field for users to enter. However this holds a problem:

I click the "add another" link, theree fields of "year, publication, title" are shown, but i do not enter any values ( leave blank) and submit the form. the data is submit with [awards] => Array ( [0] => Array ( [year] => [publication] => [title] => ) ) and saved to db with empty values. So i want to achieve the following

1. user click "add another" and create the template fields, if they dont the data, the form will not be submitted. i put"required"=>true here but not working.

2. users are allowed to remove all the fields by clicking "Remove", so there will not have any awards fields in the form but allow to submit the form ( to cater the case of no award for an item), but the validation of the field will thus not applicable. (no more required)

how can i do these?

Comments

  • edited 10:08PM
    1. The required attribute when used in the form_fields method only shows the asterisk next to it in the label but the actual validation should be done in the on_before_validate hook. The only time the validation for a required field will run will be if you add it to the models required property. In this case, since you are using a repeatable field where it saves the information in JSON array format, we'll need to do something like the following:
    function on_before_validate($values) { if (!empty($values['awards'][0])) { foreach($values['awards'] as $award) { $this->add_validation('year', 'required', 'The year value is required.' , $award['year']); $this->add_validation('publication', 'required', 'The year value is required.' , $award['year']); $this->add_validation('title', 'required', 'The year value is required.' , $award['year']); } } return $values; }
    2. In the example above, we are checking for the existence of the zero index so the validation will be applied only if that array is not empty.
  • edited 10:08PM
    i tried, but this is not working. the problem may be due to http://forum.getfuelcms.com/discussion/1865/saving-to-diff-table#Item_9

    there are two table one is product and another is awards table.

    as the form contains both product info and awards info. so i used on_after_update($values) in product_model.php to update awards table.

    and because awards are manually created template fields. so the $values passed in does not contain those awards input.

    i am modifying the on_before_validate to

    //validation
    function on_before_validate($v)
    {

    $values=$this->normalized_save_data;
    if (!empty($values['awards'])){
    foreach($values['awards'] as $award){
    $this->add_validation('year', 'required', 'The year is required.' , $award['year']);
    $this->add_validation('publication', 'required', 'The publication is required.' , $award['publication']);
    $this->add_validation('title', 'required', 'The title is required.' , $award['title']);
    }
    }
    return $v;
    }

    but this is not validating. Any suggestion?
  • edited 10:08PM
    If the validation is being run on the products model instead of the awards model, then that would make sense. The model should take care of it's own validation. Did you set those fields as required on the awards_model? If so, then it's most likely returning an error if those values are empty and the save method may be returning false. If it is returning false than you you can use the model's "get_errors" method to get an array of error messages returned:
    http://docs.getfuelcms.com/libraries/my_model#func_get_errors

    Alternatively, you can add those validation rules to the awards model dynamically by loading the awards_model and running the add_validation method on it instead of the products model.
  • edited 10:08PM
    ok, so what u mean is that i must create awards_model in this case, and actually i did not utilize the the build in model functions to save the data, i used raw sql, in this case, it is necessary to use model->save()?
  • edited 10:08PM
    ok, i think i can validate the award fields using award_model then merge the error returned back to form error array, how can i do this?
  • edited August 2014
    You can use the method "add_error" on your products_model:
    http://docs.getfuelcms.com/libraries/my_model#func_add_error
  • edited 10:08PM
    how can i also highlight the field in this case?
  • edited 10:08PM
    That's a little trickier. The field name is used as the key value of the error message (second parameter). In this case, because it is a repeatable field, the key may be something like 'awards[0][year]'. What is the array that get's returned from the get_errors method?
  • edited 10:08PM
    for all manually created field, even those not template generated, how to enforce validation on these fields?
  • edited 10:08PM
    I would use a method similar to what was mentioned above using the "$this->add_validation". Or you can use the "get_validation" method which will return the validator object used for the model by reference and you can add rules directly to that:
    http://docs.getfuelcms.com/libraries/validator
  • edited 10:08PM
    maybe i missed, however was unable to find add_validation and get_validation documentation in the above link.
  • edited 10:08PM
    Sorry for the confusion. The documentation link I sent was for the validator object which is what is returned when calling get_validation() on the model. The add_validation and get_validation methods are on MY_Model:
    http://docs.getfuelcms.com/libraries/my_model
Sign In or Register to comment.