Yes that will work, but....
I have $form->input('username', array('id' => 'username')). But I need the
model name to pass as a variable. Because on other pages
name="data[User][username] will be name="data[Post][title] or
name="data[Profile][name] and so on.
Js:
$.post('/users/ajax_validate', {
field: fieldName,
value: fieldValue
},
I want to grab the Model needing to be validated and change the js to
$.post('/ajax_validate'+myVarModel, {
field: fieldName,
value: fieldValue
},
So I can validate my fields from anywhere in the site with my function
ajax_validate in the app controller.
function ajax_validate($model) {
Configure::write('debug', 0);
if ($this->RequestHandler->isAjax()) {
$this->data['$model'][$this->params['form']['field']] =
$this->params['form']['value'];
$this->$model->set($this->data);
if ($this->$model->validates()) {
$this->autoRender = false;
} else {
$errorArray = $this->validateErrors($this->$model);
$this->set('error',
$errorArray[$this->params['form']['field']]);
$this->render('/elements/ajax_validate');
}
}
}
-----Original Message-----
From: rchavik [mailto:[email protected]]
Sent: September-14-09 11:26 AM
To: CakePHP
Subject: Re: Help with variable
Try this: $('input[name="data[User][username]"]) ?
But we're better off by passing an id to the $form->input('username',
array('id' => 'username')).
This way, we'll be able to use $('#username') to access the element.
On Sep 14, 7:57 pm, "Dave Maharaj :: WidePixels.com"
<[email protected]> wrote:
> I know its more of a jQuery question...i asked on the jQuery group but
> no luck and its Cake related so I will try here.
>
> All the forms have this structure name="data[Model][field]"
>
> How can I get User as a variable from name="data[User][username]"
>
> var myVar = $(this).attr('name').substr(4);
> alert(myVar);
>
> gets me [User][username] but how can i strip out the rest of the text
> so i end up with just User
>
> Thanks,
>
> Dave
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---