Thursday, September 2, 2010

Preserving value in dependent select list after validation

Hi, I'm new to cakephp
I'm trying to create an applicant add form with validation..
If validation fails, it should return to the add page with previously
entered values.
Here's the code snippet
//applicant controller
function add() {

$docTypes = $this->DocumentType->find('list');
$states = $this->State->find('list', array ('fields' => array
('name',
'name')));
$this->set('states',$states);
$this->set(compact('docTypes','states'));

if (!empty($this->data)) {
$this->Applicant->set($this->data);
if($this->Applicant->validates()) {

$this->Session->write('applicantdata',$this->data);
$this->redirect(array('controller'=>'uploads','action' =>
'add'));
} else {
//grab options for select list with application id
$this->set('errors', $this->Applicant->validationErrors);
}
}
}

//applicant model
class Applicant extends AppModel {

var $name = 'Applicant';
var $validate = array('name'=>array('rule'=>'alphaNumeric',
'allowEmpty'=>false,'message'=>'This field is required'));
}

//applicant view
$javascript->link('jquery/jquery-1.4.1', false);
$javascript->link('jquery/applicants_ajax', false);

echo $form->create('Applicant');
echo $form-
>select('document_type_id',array($docTypes),null,array('id'=>'document'),true);
echo $form->select('application_type_id', array(),null,
array('id'=>'application'),true );
echo $form->input('name');
echo $form->input('pob');


echo $form->end('Save');


//jquery
//if validation fails create options for select list with id =
application, and select value based
//on previously entered one

$('#document').change( function () {
$.post('http://localhost/doc_apps/documents/
ajax_search_app_type', {app_type_select: $(this).val()},function(data)
{
$('#application').find('option').remove().end().append('<option></
option>');
var obj = jQuery.parseJSON(data);
$.each(obj, function(key, value)
{
$('#application').
append($("<option></option>").
attr("value",key).
text(value));
});

$.each(obj, function(key, value)
{
var strToAdd = "<input type='hidden' value='helo'/>" ;
$('form').append(strToAdd);
});

});
});

});


In the form, there are 2 select lists where 1 select list depends on
another select list (dependent select list).
The problem is I cannot preserve the options and selected value of
dependent select list if validation fails. The options in dependent
select list is created using java script (jquery)
What comes to my mind is that in jquery, I should check if validation
fails or not. If it does, I'll make an ajax call to grab values for
dependent select list options and select an option based on previously
selected value.
Or maybe anyone have another idea to solve this problem ?
Any help would be greatly appreciated

Thanks...
CakePHP 1.2

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: