Validating a date field in a sub-form - php

I have a main form that includes a number of sub-forms. One of the sub-forms contains a pair of date fields for entering a date range. I have created the entity classes and the form classes, and have updated services.yml appropriately.
The form renders fine. The problem is that the date fields are not being validated when the form is submitted. I can leave them blank or put anything in them that I like and I never get a validation error. I've tested validation of a date field in the top-level form and it worked as expected.
For testing I created a simple form and sub-form. The main test form has two fields: a text field and a sub-form field. The sub-form has two fields, a date field and a check box field.
As for the real case, I've created the entity and form classes and updated services.yml. The form displays fine. The date field fails to generate any errors when the form is submitted with an invalid date.
I have tried specifying validation with annotations in the entity classes, a constraints attribute in the $builder->add() method call, and both at the same time ;-)
The current add() call for the date field looks like this:
...
->add( 'date',
'date',
[
'attr' => [ 'placeholder' => 'a date (mm/dd/yyyy)' ],
'error_bubbling' => true,
'format' => 'MM/dd/yyyy',
'html5' => false,
'input' => 'datetime',
'invalid_message' => 'Invalid date (use mm/dd/yyyy)',
'label' => false,
'widget' => 'single_text',
'constraints' =>
[
new NotBlank(),
new Type( '\DateTime' )
]
] )
...
Suggestions?
Environment:
- PHP V5.5.9
- Symfony V2.7.4
- Twig V1.21.2

When you add SubFormType to MainForm do the following to validate sub forms:
$builder->add('sub_form', new SubFormType, array(
'constraints' => array(
new Valid()
));
I hope this helps :)

In addition to adding a Valid() constraint to the sub-form field in the main form, it comes down to the error_bubbling attributes.
The fields in the sub-form need to be set error_bubbling true to move any errors up to the sub-form field in the main form.
The sub-form field in the main form needs to set error_bubbling false to associate any sub-form errors with the sub-form field.
Through the use of a debugger and judicious {{ dump() }} tags, I finally realized that the sub-form errors were being added to the main form's global collection of errors.

Related

In Symfony EntityType fields, how do you change the rendered OPTGROUP text?

Symfony has an EntityType field that allows a <select> field to be automatically populated by rows in a table. Going further, these rows can be categorized into <optgroup> clusters if the group_by option is given a property path.
Example:
$builder->add('categories', EntityType::class, [
'class' => PostCategory::class,
'group_by' => 'column_name',
'mapped' => FALSE,
]);
Given this code snippet, how would I change the rendered <optgroup> text? For example, how would I humanize (e.g. capitalize) it?

Zend2 InputFilter: not required as default setting

I wrote an input filter for a Form by inheriting from InputFilter (class CustomInputFilter extends Zend\InputFilter\InputFilter).
By default, each form element is required. This means that I have to set required to False explicitly for all optional elements. How can I change the InputFilter such that all form elements are optional by default?
I use the Zend 2 framework.
I am not sure about it is possible the way you want using InputFilter. I think this implies filtering and validating data. If any input field does not need to be required, then ZF have had the option ('required' => false). See another approach
$inputFilter->add(array(
'name' => 'title',
'required' => true,
'allow_empty' => true,
));
So why is this? This is something that input field must exist but you can keep it empty. If you then put invalid data in it can then validate data too.

Validate choice list in Symfony2

I have a choice list where user can choose one value, but there I even set an empty value if the user doesn't select anything.
The form does not have model, to use #Assert annotation with it, and the choice field is optional, so in some case it will be hidden and need to be validated only if showed to user.
How I can validate this field? When I set it to required in my form type it didn't help (If I am right required equal to true by defaut). Where is my problem?
You need to add the NotBlank validator to your field.
You can add a validator directly to your field, like this:
$this->createFormBuilder()
->add('exampleField', 'choice', array(
'label' => 'Label',
'constraints' => array(
new NotBlank(),
),
))
[...]

Laravel 4 Populate Form fields with a Model but edit the field first

I am working with PHP and Laravel 4. Using the Form method to populate an edit form with a Model like below...
Form::model($timecard, array('route' => array("admin/timecard/edit", $id)))
My problem is, some of the text fields get populated with DateTime values from the Database and I need to be able to run some code on these certain fields before it populates the Form field.
Any ideas how to do that or if it's possible to do that while still using the Model to auto-fill the Form fields?
For example this form field below gets filed with a GET value, otherwise it gets field with the Data from the Database for column clock_in_datetime however I would like to run a PHP function on this field before it fills the form so that I can apply TimeZone or other formatting to it...
{{ Form::text("clock_in_datetime", Input::get("clock_in_datetime"), array(
"placeholder" => "2013-09-04 14:22:35",
'class' => 'form-control'
)) }}
I believe you can do the following:
{{ Form::text("clock_in_datetime", yourFormattingFunction(Form::getValueAttribute("clock_in_datetime")), array(
"placeholder" => "2013-09-04 14:22:35",
'class' => 'form-control'
)) }}
Form::getValueAttribute() is Laravel's way of deciding which value to use (previous Input, Session or Model). So you can apply your formatting function to the output of this function.
http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html#751-773

Simple Registration on Symfony 2 wrong action

I used the Symfony 2 documentation to create a simple registration.
But now i have two little problems. The password fields, which I created with the Form Builder, be time in plain text.
The second problem is that the action of the form is not used the correct route. When I press the submit button, I get the standard page.
Has anyone of you an idea, which may be related?
If you still parts of the code needed, I like to add these.
Greetings
did you set it to a password type? eg
$builder->add('password', 'password)
Or can do repeated field
$builder->add('password', 'repeated', array(
'type' => 'password',
));
When you build the form do you set action?
$form = $this->createForm(new Type(), $type, array(
'action' => $this->generateUrl('your_route'),
));

Categories