Hi guys,
I’m having some trouble with a form that handles two modals’ inputs in one. The second modal (ExerciseRoutine) has repeatable fields and I’m having trouble working out how to validate those fields.
If you take a look at the screenshot of the form attached, you will get a better idea on what I’m trying to achieve.
The actionCreate method concerned is:
public function actionCreate()
{
$routine = new Routine;
if(isset($_POST['Routine'], $_POST['RoutineExercise']))
{
// Populate input data to routine modal
$routine->attributes=$_POST['Routine'];
// Validate routine inputs
$valid = $routine->validate();
foreach($_POST['RoutineExercise'] as $i=>$item)
{
$routineExercise[$i] = new RoutineExercise;
$routineExercise[$i]->attributes=$_POST['RoutineExercise'][$i];
$valid = $routineExercise[$i]->validate() && $valid;
}
// Check to see if both modal's inputs are valid
if($valid)
{
// Save routine modal
if($routine->save(false))
{
foreach($_POST['RoutineExercise'] as $i=>$item)
{
$routineExercise = new RoutineExercise;
$routineExercise->routine_id = $routine->id;
// Set modal attributes
$routineExercise->exercise_id = $_POST['RoutineExercise'][$i]['exercise_id'];
$routineExercise->sets = $_POST['RoutineExercise'][$i]['sets'];
$routineExercise->save(false); // Save routineExercise modal
}
}
Yii::app()->user->setFlash('success', '<strong>Routine created!</strong> You have successfully created this routine.');
$this->redirect(array('view','id'=>$routine->id));
}
}
$routineExercise = new RoutineExercise;
$this->render('create', array(
'routine'=>$routine,
'routineExercise'=>$routineExercise,
));
}
The validation works fine for the Routine modal, but not for the RoutineExercise repeatable fields, if their input is invalid the modal is not saved (as it does in fact fail validation), but no errors are shown on the form.
The _form view file contains:
<?php $form = $this->beginWidget('bootstrap.widgets.BootActiveForm', array(
'id'=>'horizontalForm',
'type'=>'horizontal',
'htmlOptions'=>array('class'=>'well'),
)); ?>
<?php echo $form->textFieldRow($routine, 'name', array('hint'=>'This is what your routine will be called.')); ?>
<hr />
<div id="input1" class="routine_exercise">
<?php echo $form->dropDownListRow($routineExercise, '[1]exercise_id', CHtml::listData(Exercise::model()->findAll(array(
'condition'=>'(user_id IS NULL) OR user_id='.Yii::app()->user->id,
'order'=>'target_muscle')),
'id', 'name', 'target_muscle'),
array('prompt'=>'Select exercise…')); ?>
<?php echo $form->textFieldRow($routineExercise, '[1]sets', array('hint'=>'The number of sets for this exercise. You will define each set\'s target reps on the next page.')); ?>
</div>
<input type="button" id="btnAdd" class="btn btn-success btn-mini" value='Add Exercise' />
<input type="button" id="btnDel" class="btn btn-danger btn-mini" value='Remove Exercise' />
<div class="form-actions">
<?php echo CHtml::htmlButton($routine->isNewRecord ? '<i class="icon-ok"></i> Create' : '<i class="icon-ok"></i> Save', array('class'=>'btn btn-primary', 'type'=>'submit')); ?>
</div>
<?php $this->endWidget(); ?>
Any help with this would be very much appreciated!
Thanks,
Aaron