material-input with validation and error message in AngularDart - angular-dart

I would like to use validation and error message with material-input in AngularDart.
We have a default message error (Enter a number) in this component, as we can see in the figure below:
I need to validate if a percentual field input is between 0 and 100%, for example. If not, a message error should be displayed.
What is the way to work with input validation and error messages in AngularDart with Material Components?

The components have a couple of validators already written which can help you here.
<material-input type=[percent] [lower]="0" [upper]="100"></material-input>
These validators are coming from here. The error messages are set to percent friendly here. If you wanted to override your own messages you can do that too here is the code. Sorry in advance for any errors whiteboard coding:
import 'package:angular_components/forms/error_renderer.dart';
#Component(
selector: 'my-form'
template: '<material-input type="percent" percentErrorRenderer="myErrors">')
class MyForm {
ErrorFn myErrors = replaceErrors(
{'lower-bound-number': 'Bigger number please',
'upper-bound-number': 'Smaller number please'});
}
The error renderer pattern allows you to use the common validators but change the messages to whatever you want. You can also use errorRenderer for regular inputs, but percent needs it's own attribute since it is directly using input.
If your validation needs to be more complicated than the defaults that are included I suggest you use the validators linked above as an example of creating one.

Related

Get object from form with errors in play framework 2.0

I am getting an error when I try to extract a user from a form with validation errors.
I have the following route configured in my routes file:
GET /users/:user controllers.UsersController.viewUser(user: models.User)
GET /users/:user/edit controllers.UsersController.editUser(user: models.User)
This is fine at this point, and I can render a link to the user view from my scala templates:
routes.UsersController.viewUser(myUserObject)
My problem is that in my user edition form I need to get myUserObject from a Form[User] object. What I am currently doing is:
routes.UsersController.viewUser(userForm.get)
However, when the userForm has any errors, the get method raises an exception, as shown in the documentation.
The approach I have taken is passing an additional User parameter to the scala view, together with the Form[User] parameter I was passing up to now, I mean,
userEdit.render(user, userForm)
instead of just
userEdit.render(userForm)
However, I would like to know if there is a more suitable solution that does not involve including an additional parameter.
The documentation states that you can prefill a form with existing data:
val filledForm = userForm.fill(User("Bob", 18))
Given the preexisting User, it should be trivial to adapt to your example.

easy way of showing errors in a wicket form

I am a wicket beginner and find it extremely hard to implement more advanced actions in it.
So I've got a form with input fields in it and a submit button. I've written my own validator with its own logic. now when the validator fails the page simply reloads and the wrong value is lost. what I want is:
inputs with wrong input(where the validator failed) should be graphically different, that is get its own style - can i do it in the validator itself? or do I have to implement a visitor? how do i register it for this very form? No error message is necessary.
the 'wrong input' should not be lost and should be visible to the user.
I'd be eternally thankful for a simple solution (with some code snippets please)
What you need is a Validator + Behavior.
The behavior is needed to modify the FormComponent's markup when it is invalid.
Fortunately they mix very well:
public class MyValidator extends Behavior implements IValidator<SomeType> {...}
Implement IValidator#validate(IValidatable) to validate (you already have this) and override Behavior#onComponentTag(Component, ComponentTag) to set a CSS class when invalid, e.g.:
#Override public void onComponentTag(Component c, ComponentTag tag) {
FormComponent fc = (FormComponent) c;
if (!fc.isValid()) {
tag.append("class", "error", " ");
}
}
Make sure you have a .css file loaded that defines .error with the desired styles. You can override Behavior#renderHead(Component, IHeaderResponse response) {response.render(CssHeaderItem.forReference(new CssResourceReference(SomeScope.class, "res/css/styles.css")))} for that.
I hope things a cleaner now.
Have fun!

Event xforms-model-construct-done behaviour

In my form I would like to call web service after to form is loaded after publishing. I've created custom XBL control for it, where I have :
<xf:group id="component-group">
<xf:action ev:event="xforms-enabled" ev:target="component-group">
<xf:send ev:event="xforms-enabled" submission="my-submission"/>
</xf:action>
</xf:group>
But it doesn't work as expected : my submission is sent everytime when I add new element in FormBuilder or change a name of some other controls. Generally speaking submission is sent when my form is changing in some way.
Now I want submission to be sent ONLY when I publish my form and someone would open it to fill (and of course when I press "Test" in FormBuilder, but I guess it's the same as filling form after publishing).
I was trying something like this :
<xf:group id="component-group">
<xf:action ev:event="xforms-model-construct-done" ev:target="component-group">
<xf:send ev:event="xforms-model-construct-done" submission="my-submission"/>
</xf:action>
</xf:group>
Unfortunately it's not working, this way submission is not sent at all. Any thoughts ?
This is due to the fact that XBL components are live at design time too. So you need a way to test whether the component is running within Form Builder or not.
There should be a function for this, really, but there isn't (I added this to the list of functions we should add to the API here). You can do:
<xf:group id="component-group">
<xf:var name="fr-params" value="xxf:instance('fr-parameters-instance')"/>
<xf:action
event="xforms-enabled"
target="component-group"
if="not($fr-params/app = 'orbeon' and $fr-params/form = 'builder')">
<xf:send submission="my-submission"/>
</xf:action>
</xf:group>
A few minor comments:
you don't need to (in fact shouldn't) place event attributes on nested actions
you don't even need the ev prefix

symfony how to translate form error message

Does anyone know how to translate form error messages in Symfony ?
At the moment I write this line in the configure() of the form:
sfContext::getInstance()->getConfiguration()->loadHelpers(array('I18N'));
And then I overwrite all error messages with the __() method.
Is there any way for Symfony to translate error messages without calling a helper and overwriting error messages ?
How I do it:
Add to the configure function of the Form to following line:
$this->widgetSchema->getFormFormatter()->setTranslationCatalogue('forms');
Then create a translation file like:
\i18n\forms.nl.xml
(in your application root or the plugin root)
It's also possible to add translations for the default validators (like 'Required.', etc.)

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