Z_Form :: Adding a custom error message to zend_form inside Action Controller - zend-form

I'm new to ZF and I'm discovering how to use Zend_Form and utilize it's capability like validating and filtering input values. I already know the basic of Zend_form like building a form and add element into it. My problem is that I want to add a custom error message to form element and I want to define that message inside the action controller that instantiated the form. I want to defined the error message inside the controller because I need to perform a validation against the database. For example checking if the username/email already exist in the database. I tried googling and that's leads me to setErrorMessage method of zend_form but when I try to use it, the error message is not showing at all... I also tried zend_form->setError and still no error displaying in the view script. Is my idea of setting custom error in the action controller correct or this should be done the other way?

Are you using Zend_Validate_Db_RecordExists?
Something like this should work:
$form->getElement('username')->getValidator("RecordExists")->setMessage('This username exists',
Zend_Validate_Db_RecordExists::ERROR_RECORD_FOUND
);

This works for me:
$form->getElement('username')->addError('This username exists.');

Related

Aurelia Validation rule (bound to model) does not fire on subsequent activations of a view model

I am trying to expand on the Aurelia Contact Manager Tutorial. Specifically: adding email validation to the contact-details.html view. I have followed the examples in the Validation: Basics documentation and on first pass it worked as expected: Launch application, select a contact from the contact-list module, then update the email to something invalid by removing the '#', then tab away. The validation rule fires and the error message is displayed.
However, if after launching the application I select a first contact followed by a second, hence triggering a second activation of the contact-details module, then the validation rule does not fire.
I have tried a validationController.reset() on activate of the contact-detail and while this will remove any 'old' error messages, the on blur validation will still not fire.
I have tried the two different methods of creating the validation controller (using NewInstance.of(ValidationController) vs ValidationControllerFactory) but both yield the same result.
If, after navigating to a second contact and 'breaking' the validation, I then refresh the browser and reload the page then the validation works again. Until I choose another contact from the list which will then break it again.
I am new to Aurelia and JavaScript frameworks in general and I'm not sure if this is a bug or there is something extra required to handle re-routing to the same page.
That's a good question. There are a couple of things that may be catching you out. I've created a Gist which includes the necessary file modifications to get this working:
https://gist.github.com/freshcutdevelopment/170c2386f243e7095e276811dab52299
Gotchas
Because the view-model you're using for validation is not the backing view-model for the contact-detail.html view file you'll need a separate class which you'll apply the validation rules to. Although it sounds like you've already nailed this part, I'll include it for completeness. You can create this class like so:
export class Contact {
email= '';
}
You can then apply the validation rules to this class as follows:
ValidationRules
.ensure(a => a.email).required().email()
.on(Contact);
The last possible missing puzzle piece here is that you'll need to hook into the screen activation life-cycle hook deactivate() and reset the validation context. This will force the BootstrapValidationRenderer to remove the validation styles from your view.
deactivate(){
this.controller.reset();
}
Validation Workflow
The steps are as follows:
Inject the controller
Add the validation renderer to the controller
Create the validation model (only needed if the model you want to validate is not the view-model that backs your view)
Apply the validation rules to the model
Determine when to re-set and execute the validation (in this case on the deactivate life-cycle hook.
Apply the validation binding behavior to the view

MVC unobtrusive general message

Im working in a project where the validations messages with unobtrusive system works fine.
When any attribute is uncompleted or wrong the proper error messages appear, at client side ofc.
The problem is the following. I need to set an general error message at the top of the site (besides to each field validation message error). I know i can use #Html.ValidationSummary but i dont want the list of fields, what i need is one general message telling the user that there is something wrong with the form.
Regards.
Edit: On jquery before submit i'm doing $("#frmSubmit").valid();
Did you try using the ValidationSummary(bool) overload? Supplying true excludes property errors.
Why not just have check ModelState on your view like this:
#if (!ViewContext.ViewData.ModelState.IsValid)
{
<div>Your error message here...</div>
}
Then style your div to be errorish...

Display error on actionProductUpdate hook execution

Is there any way to display an error message after actionProductUpdate hook is executed in a module?
Looking at the Prestashop code, the hook is triggered in AdminProductController, but the return value is not being processed. Also adding any message in passed Product object seems to be out of my requirement.
The purpose is to provide user with an error message in case of wrong input, database update error etc after clicking on Save button in back-office product edit page.
in belvg blog user Prestarocket post the the very helpful comment:
"Why not using hookActionProductSave ?
With this hook, you can add errors to the controller ($this->context->controller->errors[])"
Regards

DNN get params in view are not available in edit

I am creating a dnn module. The content depends on the param in the url.
I want to be able to edit this content in the 'edit content' mode. However when i go to edit content the param in the url is no longer accessible because it is the parent document. How do i go about passing this value from the view.ascx to the edit.ascx?
Try storing the param in cookies or localstorage. Then you should be able to access it. Of course the user will be able to modify it but you can do a check that the user has not modified it by storing a server side encryption or somthing like that.
A workaround is to have a field where the user enters this parameter. But i know this isn't a very good solution. I am guessing that you will have to override the dotnetnuke core to do this (yes i know it sucks).
I hope I am understanding the question properly.
To pass parameters from your View to Edit controls, you should first make sure they are registered properly in the module definition. You default View should have an empty controlkey and your Edit should be registered with a control key, for example "addedit".
When creating a link between your view control and edit control, use the EditUrl() method of PortalModuleBase. When passing a parameter, for example an id of the item you want to load into your edit control, you can pass them as arguments in the EditUr method.
Example (in my view.ascx.cs):
lnkEdit.NavigateUrl = EditUrl("id", "16", "addedit");
This will assign a module view link to the edit.ascx (assuming the controlkey in the definition is addedit) passing in a url parameter "id" with value 16.
See my DNN Module Views tutorial for a complete lesson on how to do DNN module views and navigation.
http://www.dnnhero.com/Premium/Tutorial/tabid/259/ArticleID/204/Introduction-and-Module-Definition-basics-in-DNN-Part-1-6.aspx

Yii generate errors from parent controller

I just started using Yii, coming from Codeigniter (CI). I'm trying to set up a situation where the application will check a users credentials before they can even access the site. In CI, I would create a parent controller, put my checks in there, and then inherit that parent controller. I've been trying to do the same thing with Yii, but with no luck.
I first created an init() method (for some reason I can't put the code in a __construct() method). I then perform my check, which works fine. I can throw a new CHttpException, but that looks ugly. How do I use Yii's built in error handling to accomplish what I want to do?
For simple login rules you should just implement the 'accessControl' filters from Yii see Yii documentation on authorizations.
Another way would be to throw the Exception like you already did, and write custom Http error views, place it in the yerfolder/protected/views/system/ (see the yii/framework/views directory for examples)
I solved this by sending the error to the site/error view and then calling Yii::app()->end() to keep the child controllers from loading.