2009年11月25日星期三

Re: [fw-mvc] Couple of Zend_Form questions

Hi

i have used your way, and it works. i get back json with this structure:

{
username: {
isEmpty: 'blah blah',
stringLengthTooShort: 'blah blah'
},
password: {
isEmpty: 'blah blah',
stringLengthTooShort: 'blah blah'
}
}

but i cannot append them to their corresponding input field. (i guess its
because i suck with javascript and jquery). how can i take the results for
the username field and append it to it, and the same for the password field?

best regards

weierophinney wrote:
>
> -- Cristian Bichis <contact@zftutorials.com> wrote
> (on Thursday, 17 January 2008, 04:22 PM +0200):
>>
>> 2. Based over same Andries sample how is possible to do form
>> validation based
>> on Ajax ?
>>
>>
>>
>> AJAX is simply an HTTP request, so you can treat it basically the
>> same;
>> the difference is in the response format you use.
>>
>> Zend_Form::processAjax() will process a partial form (or the entire
>> thing, if it's present), and return a JSON response. If all
>> validations
>> pass, a boolean true response is sent; otherwise, a JSON object of
>> element name / message arrays is returned. You can then have your
>> client-side code use that response to update the form and display
>> error
>> messages.
>>
>> If you wish to return other formats, use isValid() or
>> isValidPartial()
>> to validate, and then you can loop over the return values of
>> getMessages() and/or getErrors() to build your response.
>>
>>
>>
>> Ok, so basically would be needed 2 pages:
>> 1. for normal form
>> 2. for ajax response using Zend_Form::processAjax()
>> Correct ?
>
> Not necessarily. You can test if a request is an XHR request with the
> request object (which tests for the 'X-Requested-With: XmlHttpRequest'
> header, which most major JS libraries send):
>
> public function editAction()
> {
> $form = ...;
> $request = $this->getRequest();
> if ($request->isPost()) {
> // submitted form
> if ($request->isXmlHttpRequest()) {
> // XmlHttpRequest detected. Generate AJAX response
> $this->_helper->viewRenderer->setNoRender();
> $response = $form->processAjax($request->getPost());
> $this->getResponse()->setHeader('Content-Type',
> 'application/json')
> ->setBody($response);
> return;
> }
>
> // Normal validation:
> if ($form->isValid($request->getPost()) {
> // success...
> // Do something with results and redirect
> $values = $form->getValues();
> $this->getModel()->save($values);
> $this->_helper->redirector('profile', 'user');
> }
> }
> $this->view->form = $form;
> }
>
> --
> Matthew Weier O'Phinney
> PHP Developer | matthew@zend.com
> Zend - The PHP Company | http://www.zend.com/
>
>

--
View this message in context: http://n4.nabble.com/Couple-of-Zend-Form-questions-tp665941p787364.html
Sent from the Zend MVC mailing list archive at Nabble.com.

没有评论: