CakePHP 3 and form validation errors - forms

I use cakePHP 3 and I have a sign in form with Form->input().
If on purpose I make an error, this error doesn't whow up under the Form field. It doesn't appear anywhere.
My code is like this:
$newUser = $this->Users->newEntity($this->request->data());
if (!$this->Users->save($newUser)) {
debug($newUser->errors());
$this->Flash->error('Error');
return;
}
Debug show the errors, but shouldn't they appear under each form element automatically?

ok I found the error.
I wasn't passing in Form->create the entiry but null. I did it like
$this->Form->create($entity...
and works nicely.

Related

Remove submitted state in submission handler

I am trying to reset a form so that it appears to Drupal 8 that it hasn't been submitted. So far I have been unable to do this, as I cannot find any available methods (setSubmitted() hardcodes it to TRUE without a FALSE option). The reason is this isn't a full submit, but a submit of one field after which I would like the user to be redirected to another page that has another form, and I would like this secondary form to use the value obtained in the first step.
In the submit handler for the first part I use this to redirect:
$form_state->setRedirect('my.route', [], []);
And this works, but when the form reaches the second form (it seems) that the second form thinks it is a submission. As a result any submit buttons I add to the second form seem to make it auto-submit, and this breaks my user journey.
In the submit for the first part I have tried:
$form_state->setRebuild(TRUE);
$form_state = new FormState();
unset($form_state);
Tried the above in various configurations to no avail. They all prevent/ignore the setRedirect call that I make afterwards. The reason I want/need to do it this way is I want to preserve the POST method used.
Do you want to obtain something similar to what core search module does? It has simple SearchBlockForm that sends data to more complex SearchPageForm.
SearchBlockForm uses GET method (though you may use POST):
$form['#method'] = 'get';
and has no id and token fields:
function search_form_search_block_form_alter(&$form, FormStateInterface $form_state) {
$form['form_build_id']['#access'] = FALSE;
$form['form_token']['#access'] = FALSE;
$form['form_id']['#access'] = FALSE;
}
BTW, the last change allows you to avoid running submit callbacks.
Hope this helps.

cakephp 3.0 'App\Controller\ContactForm' not found modelless form

I am trying to create a modelless form using cakePHP 3.0, I have been following the guide through the cookbook here http://book.cakephp.org/3.0/en/core-libraries/form.html but it seems I am getting confused as to where to put ContactForm.php. It says to put it in src/Form/ContactForm.php but it did not work. Can someone direct me to this? Thanks
Pls, provide a bit more info about your problem
1) is ContactForm recognized in controller?
if not - check namespace
2) handle your data only in
protected function _execute(array $data)
{
// Send an email.
return true;
}
not in controller's
if ($this->request->is('post')) {
if ($contact->execute($this->request->data)) {
//NOT HERE!!!
} else {
// error
}
}
Also bear in mind that your server configuration may forbid php short open tags.
Make sure your class starts with <?php and not only <? to be on the safer side. If not, CakePHP will simply respond by the Your\Namespace\Class Not Found message.
Just verify where you have put Form Folder, it must be inside src Folder.
Also be sure that the class has Form as suffix.
Because your error is App\Controller\ContactForm not found, that means that you have NOT put Form Folder inside src Folder.

Symfony2, validation on getters, show error on form

I have a problem with errors using Symfony2 FormComponent.
I've validation for getters in bundle:
getters:
someGetter:
- MyOwnValidator: { message: "Fail"}
After that, I want to validate form and show errors:
{{form_errors(form)}}
returns to me bunch of errors (including error for someGetter), but
{{form_errors(form.someGetter)}}
doesn't return anything.
Any ideas?
UPDATE
I've found the solution. Field, for which I implemented validation (someGetter) is a collection type without 'error_bubbling' => 'false' option.
When I added error_bubbling, everything works fine.
Other options to have the getters error is with http://symfony.com/doc/current/reference/forms/types/entity.html#error-mapping and http://symfony.com/doc/current/reference/forms/types/entity.html#error-bubbling
I've found the solution. Field, for which I implemented validation (someGetter) is a collection type without 'error_bubbling' => 'false' option.
When I added error_bubbling, everything works fine.
Answer extracted from updated question.

Validation / Error Messages in ASP.Net MVC 2 View Unrelated to a Property

What pattern can I use to display errors on an MVC 2 view that are not related to a single property?
For example, when I call a web service to process form data, the web service may return an error or throw an exception. I would like to display a user-friendly version of that error, but have no logical means to relate the error to any given property of the model.
UPDATE:
Trying to use this code as suggested, but no summary message is displayed:
MyPage.spark:
Html.ValidationSummary(false, "Oopps it didn't work.");
Controller:
ViewData.ModelState.AddModelError("_FORM", "My custom error message.");
// Also tried this: ViewData.ModelState.AddModelError(string.Empty, "My custom error message.");
return View();
UPDATE 2
What does this mean?
next to each field.
Instead of always displaying all
validation errors, the
Html.ValidationSummary helper method
has a new option to display only
model-level errors. This enables
model-level errors to be displayed in
the validation summary and
field-specific errors to be displayed
next to each field.
Source: http://www.asp.net/learn/whitepapers/what-is-new-in-aspnet-mvc#_TOC3_14
Specifically, how does one add a model level error (as opposed to a field-specific error) to the model?
UPDATE 3:
I noticed this morning that Html.ValidationSummary is not displaying any errors at all, not even property errors. Trying to sort out why.
Simply adding a custom error to the ModelState object in conjunction with the ValidationSummary() extension method should do the trick. I use something like "_FORM" for the key... just so it won't conflict with any fields.
As far as patterns go, I have it setup so that my Business Logic Layer (called via services from the controller) will throw a custom exception if anything expected goes wrong that I want to display on the view. This custom exception contains a Dictionary<string, string> property that has any errors that I should add to ModelState.
HTHs,
Charles

Zend Lucene displays blank screen when no results found

When I submit a query to Zend_Lucene with a string that exists in the index, the results are displayed as expected, however when string is not found, I get a blank page with no error messages. Code used as below:
require_once 'Zend/Feed.php';
require_once 'Zend/Search/Lucene.php';
$index = Zend_Search_Lucene::open('data/my-index');
$queryStr ='fjkhsdkdfh';
$hits = $index->find($queryStr);
if ($hits) {
foreach ($hits as $hit) {
echo $hit->page_title;
}
} else {
echo 'No results found.';
}
I would expect 'No results found' to appear, but instead I get a blank page with no error messages.
What confuses me more is that I have this tested and working locally, but when on a live server it stops working.
Locally I have Zend Server 4 installed, remotely PHP 5.2.11 and ZF 1.10.2
Any help much appreciated!
Paul
I actually found a work around to this that involved processing the entire routine through a single page. As I was calling in external functions to generate the querys for some reason a blank page was always returned. By placing all script on one page I was able to have results displayed.