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

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.

Related

Custom Form Layout Validation

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
}
}
}

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

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'));
}
}

Symfony forms or normal HTML forms

I am using Symfony 1.4 to create project, and i need to create dynamic forms depending upon the question set type coming from database. i have used Symfony forms in rest of my project, but in this case using symfony forms seems to be tough, as i need dynamic form.
can it be safe to use normal HTML forms..in symfony project, or it is advisable to use Symfony forms. so need your help.
You can use html forms, but it would bypass symfony's form validation system.
You can still build dynamic forms by creating and adding input widgets to the current form, or a new form inside an action. You can then echo the form in the template and the dynamically generated fields will be part of the form as well.
If you start with a MyForm.class.php in the lib/forms, make sure to add:
$this->validatorSchema->setOption('allow_extra_fields', true);
Otherwise, you will automatically get validation errors. If you want to add fields to a form in an action you would do something like this:
$this->form = new MyForm();
$widgetSchema = $this->form->getWidgetSchema();
$widgetSchema['add_field'] = new sfWidgetFormInputText();
When you echo your form the 'add_field' input will be added to it.
It would help to have more information about what you're doing, but here's one way in which forms can be dynamic in Symfony. This code creates widgets and validators for a survey dynamically based on the "type" of a question:
class SurveyAnswerForm extends BaseSurveyAnswerForm
{
public function configure()
{
$question = $this->object->Question;
$method = sprintf('createWidgetAndValidatorFor%sInputType', $question->type);
$this->$method($question);
$this->getWidget('value')->setOption('label', $question->question);
$this->getValidator('value')->setOption('required', $question->required);
}
protected function createWidgetAndValidatorForTextFieldInputType(Question $question)
{
$this->setWidget('value', new sfWidgetFormInputText());
$this->setValidator('value', new sfValidatorString());
}
protected function createWidgetAndValidatorForTextAreaInputType(Question $question)
{
$this->setWidget('value', new wfWidgetFormTextareaAutosize());
$this->setValidator('value', new sfValidatorString());
}
//etc. for as many types as you require
}
Note: while this answer is code from one of my projects, it was heavily influenced by this answer over on SymfonyExperts.

Zend Framework: isValid() clears values from disabled form fields!

When you submit a form, disabled form fields are not submitted in the request.
So if your form has a disabled form field, it makes working with Zend_Form::isValid() a little frustrating.
$form->populate($originalData);
$form->my_text_field->disabled = 'disabled';
if (!$form->isValid($_POST)) {
//form is not valid
//since my_text_field is disabled, it doesn't get submitted in the request
//isValid() will clear the disabled field value, so now we have to re-populate the field
$form->my_text_field->value($originalData['my_text_field']);
$this->view->form = $form;
return;
}
// if the form is valid, and we call $form->getValues() to save the data, our disabled field value has been cleared!
Without having to re-populate the form, and create duplicate lines of code, what is the best way to approach this problem?
Are you setting the element to disabled so that the user can't edit it's contents but only to see it? If so, just set the element's readonly attribute to true, I think it'll work that way.
I use a custom class inherited from Zend_Form. Class adds some features and solves this problem by replacing the method isValid as follows:
class Murdej_Form extends Zend_Form {
function isValid($data) {
$_data = $this->getValues();
$valid = parent::isValid($data);
$this->populate($_data);
return $valid;
};
};
instead of using isValid() we can use isValidPartial(). Unlike isValid(), however, if a particular key is not present, it will not run validations for that particular element. So, isValidPartial() will not validate for disabled fields.
Reference: ZF Documentation