I think you misunderstood my question. What I'm talking about is if
the validation fails.
In controller I don't do any redirect if validation fails. It simply
sets errors variable to be passed to add view and the view is rendered
with previously entered values along with error messages. This works
ok except for dependent select list (id= appilcation) which is
displayed without any option. It is because the options in dependent
select list is created with jquery when there is a change in another
select list  (id=document) (I set onChange event to document select
list).
I can get the value of previously entered value for dependent select
list option in controller.
So my question is, how can I display options in dependent select list
with correctly selected value  if validation fails ?

What I'm thinking now is that I set a variable  in controller when
validation fails. This variable will be passed to jquery.(this creates
another problem that I don't quite understand, which is passing
variable from controller to javascript/jquery) In jquery, we will
check if this variable is set. If it is, then create options for
dependent select list and select an option with previously selected
value. Otherwise create options with no selection.
Or maybe anyone has another ideas ??
Thanks..

On Sep 5, 4:38 pm, Jeremy Burns | Class Outfit
<[email protected]> wrote:
> You're doing a redirect, which effectively clears down the values in the 
> $this->data array, which the form will look for to get previously entered 
> data. Set $this->data to $this->Session->read('applicantdata') before you go 
> back to the form. Should work.
>
> Jeremy Burns
> Class Outfit
>
> [email protected]http://www.classoutfit.com
>
> On 3 Sep 2010, at 05:14, aveev wrote:
>
>
>
> > 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'),tru
> >>  e);
> > 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 sitehttp://cakeqs.organd 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 [email protected]
> > To unsubscribe from this group, send email to
> > [email protected] For more options, visit this group 
> > athttp://groups.google.com/group/cake-php?hl=en

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 [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to