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'),
));
Related
I use standalone Symfony components in my app (and without Twig).
My HTML form contains two fields ('new_password' & 'confirm_new_password').
The following validation works fine:
$validator = Validation::createValidator();
$violations = $validator->validate($new_password, [
new Length(['min' => 4]),
new Regex([
'pattern' => '/\d/',
'match' => true,
'message' => 'Password must contain at least one number'
])
]);
if (0 !== count($violations)) {
...
}
I would like to add validation of password confirmation fields as well
The 'Form' component by Symfony allows to create, process and reuse forms, but this is far beyond of what I want to do. I found that 'RepeatedType' field by Symfony can do this but seems to be using the 'Form' component.
How can I simply add password confirmation to my validation script?
there is a constraint call identicalTo that looks quite the same as explained by RiggsFolly. So you call this constraint and give it both fields' values.
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.
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(),
),
))
[...]
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
Can anyone tell me how can we disable validation from action? Actually, I wanted to disable validation dynamically based on certain condition from action. I don't know how can we do that? Please help me.
My For validator as below:
$this->setValidators(
array(
'search_text' => new sfValidatorString(
array('required'=>true),
array('required' => 'Please enter keyword')),
'field_type' => new sfValidatorString(
array('required'=>true),
array('required' => 'Please select an option')),
)
);
I want to disable above validation in action dynamically.
Please help me.
You can create an enable_validation (or whatever name you want) option for your form. The form constructor accepts a $options array. In your action, you pass an array which would look like this : array('enable_validation option' => false)
Then, in your form, use the getOption() method to retrieve this option and set the validators accordingly.