Custom Form Layout Validation - forms

I want to have my own validation flow, with custom layout and message.
By default, the validation from the form builder put all the error message beside the input field. And it will validate all fields at once after submit.
I want to validate field by field after submitting, and error message is displayed in the same place for all the input fields (beside the submit button/on top of the form).
Currently I'm trying custom form layout with "ASCX" type. Is it possible to do all the validation in the back-end code ".cs"?
Or I must inject java script at the custom form layout design in source mode?
Or there is any better way to do it?

In HTML layout type you can place validation macros anywhere you need -> $$validation:FirstName$$
You can also specify a validation that executes without submitting the form - example -> http://devnet.kentico.com/articles/tweaking-kentico-(2)-unique-fields
Anyway, with the validation macro above you can move the error message wherever you want.

In your online form, go to Layout and enter your layout markup manually using HTML and the macros for form field values, labels and validation. There you can specify where all your form elements will go on the form, even the button.
If you want to have custom CS for your validation of that form, you're better off creating a custom event handler for the form before insert. See documentation below:
Custom event handler
Form Event handler
using CMS;
using CMS.DataEngine;
using CMS.OnlineForms;
using CMS.Helpers;
// Registers the custom module into the system
[assembly: RegisterModule(typeof(CustomFormModule))]
public class CustomFormModule : Module
{
// Module class constructor, the system registers the module under the name "CustomForms"
public CustomFormModule()
: base("CustomForms")
{
}
// Contains initialization code that is executed when the application starts
protected override void OnInit()
{
base.OnInit();
// Assigns a handler to the Insert.After event
// This event occurs after the creation of every new form submission
BizFormItemEvents.Insert.After += Insert_After;
}
private void Insert_After(object sender, CMS.OnlineForms.BizFormItemEventArgs e)
{
if (e.Item.TypeInfo.ObjectType.ToLower().Contains("bizform.codename"))
{
//do some work or form validation
}
}
}

Related

Wicket: Validating nested forms inside DataTable component

I'm currently testing the creation of a custom DataTable where I need to have a a panel with a form inside some of the table cells, with the following structure:
Outer form > DataTable (rows > cells > cell > panel > inner form)
At the moment, I am able to successfully submit these nested forms (inner forms inside each cell) with the submit button of an outer form, but the inner forms do not show the validation feedback messages (although I checked and they are being validated - the outer form goes to onError() on validation error).
I believe this problem is somehow related to the similar issue with ListView where I have to use setReuseItems(true) in order to be able to get the feedback messages. (Ref: wicket validate textfield inside listview can't see error message)
I have tried to configure the DataTable reuse item strategy, and even the DataTable inner datagrid (as from Wicket 6) reuse strategy, but still, I could not get the feedback messages. (Ref:
A GridView inside a Wizard in wicket fails to render error feedback messages)
I'm starting to think that I might not be able to this at the DataTable level, since the onPopulate() of the cells is really only called on the AbstractDataGridView level. Does anyone know then if validating these inner forms inside of a DataTable is possible and, if so, how can I achieve this?
Thanks for your time and attention.
I had the same problem and the way I solved it was like this:
Form<List<MyDataType>> form = new Form<List<MyDataType>>("form", getModel()) {
#Override
protected void onValidate() {
super.onValidate();
visitChildren(FormComponent.class, new IVisitor<FormComponent, Void>() {
#Override
public void component(FormComponent component, IVisit<Void> visit) {
component.processInput();
if (component.hasErrorMessage()) {
for (FeedbackMessage message :
component.getFeedbackMessages()) {
if (message.isError()) {
get(TABLE_ID).getFeedbackMessages().add(message);
}
}
}
}
});
}
};
So basically when ever I validate my Form, I manually check for inner FormComponents and check those. Then I forward the error messages to the table which has its own FeedbackPanel.
Probably not the most elegant solution, but it works.
table.setItemReuseStrategy(new ReuseIfModelsEqualStrategy()) set the trick for me
source:
1 wicket validate textfield inside listview can't see error message
2 A GridView inside a Wizard in wicket fails to render error feedback messages

Make a standard POST form validation in Knockout

To allow to make validation of form before submit,
I want to make a simple HTTP POST submit from the Submit method i defined.
Code of the view :
<form data-bind="submit: BaseSubmit" id="FormId">
<!-- Some rows ... -->
<button type="submit">Create</button>
</form>
And of the JS behavior
this.viewModel.BaseSubmit =
function(formElements) {
//Here the code to make the submit redirection
};
Is this possible ?
Thanks by advance.
Upon successful validation, make sure and return true from your binding handler:
this.viewModel.BaseSubmit =
function(formElements) {
if (/* validation is successful */) {
return true;
} else {
/* handle validation errors */
}
};
Example: http://jsfiddle.net/CCNtR/22/
From the documentation:
By default, Knockout will prevent the event from taking any default action. For example if you use the event binding to capture the keypress event of an input tag, the browser will only call your handler function and will not add the value of the key to the input element’s value. A more common example is using the click binding, which internally uses this binding, where your handler function will be called, but the browser will not navigate to the link’s href. This is a useful default because when you use the click binding, it’s normally because you’re using the link as part of a UI that manipulates your view model, not as a regular hyperlink to another web page.
However, if you do want to let the default action proceed, just return true from your event handler function

Adding a form field that is not in the schema to Doctrine form Symfony 1.4

I have an image upload form and at the bottom, I'd like to have a checkbox that the user must check before submitting the form, certifying that they have the right to distribute the photo. I've tried adding it as a Widget in the Form class, but it is not displaying. What is the best way to accomplish this?
For validation, you can add this to your form class to allow fields outside the model:
$this->validatorSchema->setOption('allow_extra_fields', true);
$this->validatorSchema->setOption('filter_extra_fields', false); // true or false
Other than that, just adding the widget in the standard way should work fine.
Adding a new widget to your form should be the right way.
class ImageForm extends BaseImageForm
{
public function configure()
{
$this->widgetSchema['copyright'] = new sfWidgetFormInputCheckbox();
}
}
For conditional validation, check this cookbook page should still be valid.

How can I use the sfValidatorEmail validator in Symfony to validate a single email field

I have a form with 2 elements that will be submitted and then update part of a user profile.
I don't want to use the entire generated form and have to remove all the fields except for the two I need. I just want to be able to create a quite simple form to do my update.
Is there a way to utilize Symfony's sfValidatorEmail inside the action on the returned value of an email field?
Since the regex is already written in the validator, I would like to reuse it, but I don't know how to use it in the action after the non-symfony form has been submitted.
Two approaches here - you could construct a simple form anyway extending from sfForm/sfFormSymfony (doesn't have to be ORM-based) that just contains the 2 fields you want. That way you can use the existing validation framework, and then use $myForm->getValues() after everything has been validated to get your values for your profile update.
Alternatively, as you've mentioned, you can use the sfValidatorEmail class in your action like so:
$dirtyValue = "broken.email.address"
$v = new sfValidatorEmail();
try
{
$v->clean($dirtyValue);
}
catch (sfValidatorError $e)
{
// Validation failed
}
The latter approach quickly leads to messy code if you have many values that need cleaning, and it's worth putting the logic back into a form to handle this in the usual manner.
If you're submitting a form with 2 elements, it should be a form on the edit and update end, period. Symfony forms are lightweight, there's no performance reason to not use them. Instead, make a custom form for this purpose:
class ProfileUpdateForm extends ProfileForm
{
public function configure()
{
$this->useFields(array('email', 'other_field'));
}
}

C Builder TForm not allocated or created properly all its controls

I'd like to know how I can check that all the controls on the form are created and initialized.
I have a form I am showing when a user presses the update button. It has only a TProgressBar control.
The handle is not NULL for this control and at random stages it can or can't set the Position/Max values.
When I set TProgressBar->Max value to some integer it remains 0 after.
So the question is:
How to really create the form and all controls on it (i am currently using just Form->Show() method, which as I can check calls the constructor)
Also I have following form creation code in main cpp file:
Application->CreateForm(__classid(TupdateProgramForm), &updateProgramForm);
How to check that all controls on the form are created and PAINTED (showed and visible)
In C++ Builder the form and the controls created at design-time are translated into binary objects through automatic scripts that produce Delphi code.
To view the originated Delphi code just right-click anywhere on form at design-time and select 'View as text'. This will show the Delphi source code of the form and it's controls.
After a form and all child controls created, the OnCreate event of that form invoked and you can place your initialization and checking code in this event, for example:
void __fastcall TfrmMain::updateProgramFormCreate(TObject *Sender)
{
ProgressBar->Max = 100;
ProgressBar->Value = 20;
}