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
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.
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?
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.
http://docs.getfuelcms.com/libraries/my_model#func_add_error
http://docs.getfuelcms.com/libraries/validator
http://docs.getfuelcms.com/libraries/my_model