Customize Symfony forms and get more control over validation process - php

I use doctrine:generate-admin to build CRUD operations for one of my models called "Alumnos". I know that Symfony generate the views under /apps/site/modules/alumnos/views and I'm trying to customize the input fields using Twitter Bootstrap Framework. The _form.php file has this code:
<?php echo $form['nombres']->renderLabel() ?>
<?php echo $form['nombres']->renderError() ?>
<?php echo $form['nombres'] ?>
But I want to add a (*) to required fields, a class to input fields and also wrap errors inside <div> tags with properly markup. How I can achieve this?
Also (don't know if it's better to open a new question for this part) I six fields and need to achieve this:
If the first three fields (less said 1,2,3) are populated with values then validation pass
If the second three fields (less said 4,5,6) are populated with values the validation pass
At least firs three fields or second three fields are required so in all cases 1,2,3 can't be empty or 4,5,6 can't be empty
How I deal with this validation?

If that's only in one form, you can go the hacky way:
replace, <?php echo $form['nombres']->renderLabel() ?> with HTML, that simple ^^
For your second question, you need a post validator in your form to check several fields. Here is an example:
http://symfony.com/legacy/doc/cookbook/1_2/en/conditional-validator
Maybe look in the jobeet tutorial if there is a post validator example.

For the first part of your question you should look at http://symfony.com/legacy/doc/more-with-symfony/1_4/en/06-Advanced-Forms at the bottom of the page they describe how to create a new WidgetFormSchemaFormatter.
For the second part, as Francois said, use a post validator, or a custom validator.

Related

Form validation - is jQuery validation sufficient and how to neatly validate data in PHP?

I have a form (right now only one, but there will be more with similar parameters) and I validate it using jQuery Validation (it is too big word, I validate only one field using jQV), this form is send using AJAX. I have in this form 5 inputs:
text input: username, which is validated by separate PHP script remotely - it checks if this user exists in database of different site and if so, also checks if this user has some specified data, but this is not really important
2 times radio buttons (i mean 2x 2 radio buttons) which are validated in PHP (code later)
2 times PickAColor forms, which are also validated using PHP only with regexp for hexcolors
And I have a question. Is there any way to check this data in PHP in more, well, sophisticated way? Because, as I said, I validate them in similar way:
if($period != "3month" && $period != "7day") {
echo '<div class="alert">Wrong period!</div>';
exit;
}
Actually this form I am writing right now is rather short, but I also have some really big ones, and if's like this one above can take even 50 lines, and that pretty sucks. Most of them has similar expressions inside if, which means that I check if value sent by form is on the list of possible options provided by form (someone can change value parameters in Firebug and call himself hacker...). It's complicated and I don't know how to said that, but I think you understand me. So, once again - is there any sense to validate it in PHP and if so, is there any better way to validate it? Or maybe you have some other suggestions? Thanks a lot for your help.
Man you have to think your users can submit anything there, and to make the code clearer and more MVC oriented (and reusable) you need your validations in the app side, and generic functions in jQuery to just display errors to your users, so I would recommend you to do ajax post with the form sent to your php application, to an api controller that recives the form values, validates using the model and returns the object with the jew form values (if you need to clean up) and any error per field, then you just output in json :
{
errors: {
name: "Already used name",
field2: "Some bad text provided"
}
}
Then you just loop through the errors and set to the already existent placeholder of each error text like
<spam id="name-error" class="error"></spam>
<input type="text" name="name" value=""/>
Then you have a validation code that you can reuse when the form is submitted.

Submit multiple forms as one

I have two forms on the page. To the user it looks like 1 form, and I actually wish it was one form. But the way I'm trying to reuse code and include things, I can't avoid two forms in the source code... trying to act as one.
I don't want to do ajax submit, I want a normal post submit, my form handler has redirects in it. How can I submit both of these, and get values that make sense on the server side. something like $_POST['form1]['whatever'] $_POST['form2]['thing']
Maybe take all the inputs from form 2, rename all of them with a prefix, and append them to form 1? I can't find a non-messy way of doing this. I don't think I need code, just a plan. Least messy idea wins.
Maybe take all the inputs from form 2, rename all of them with a
prefix, and append them to form 1?
That's exactly what you have to do. Wouldn't be much of an answer without a code sample, so here you go.
$("#form2 :input").appendTo("#form1")[0].submit()
now in php you'll have $_POST['thing'] containing an array with two values. Alternatively you can rename all of the inputs from form2:
$("#form2 :input").attr("name",function(name){
return name + "_form2";
}).appendTo("#form1")[0].submit();
You can try to collect values of one form with jQuery.serializeArray() and then generate hidden inputs with names and values from variable storing result of previously called jQuery.serializeArray() and insert them to second form on submit event of form.
You should be able to combine both forms fields into one single form in PHP. If your code doesn't allow it, it must be in terrible shape.
If you are using simple scripts, you should be able to cut the forms into parts producing the fields, and the other parts producing the form outlines, either as separate scripts, or simply as separate functions.
i.e.:
<?php
function form_body($params) {
// here's the code for echoing fields according to $params
}
function form($params) {
// here's the code to build the form properties
$f_properties = '....';
echo '<form '.$f_properties.'>';
form_body($params);
echo '</form>';
}
?>
Then it's just a matter of combining $params from form1 and form2 to get the definitive form.
If you're using OOP, it's probably very easy to derive a new class containing both forms, and have it output their fields.
This are very simplistic advices, but you don't provide any source to help refactoring, so I can only provide vague/generic code examples.
Going the js way to combine forms on the client side will turn into a lot of problems down the line to maintain and evolve the code, and bring a lot of issues (security not the least of them).

Two Forms One Submission

My site has three columns. I have two fields within a form that need to be in the left column and 1 field of what is currently a different form that needs to be in the middle column. The thing is, I want them to behave as one form... (or be one form if that is possible).
When The form is submitted, the data from all the fields, needs to be passed to the action page.
What is the best way of achieving this? h
If you don't have to support old browsers, you can use the form attribute on some elements to make them behave like they are in the form.
See http://www.impressivewebs.com/html5-form-attribute/
<form id="some_form">
...
</form>
<input form="some_form" />
Else, the better way is to make the form element a parent of all fields. Just move the form element out of the first column.
Columns do not a form make. Wrap all of your column divs in one form tag.
It sounds like you control the code. The simplest solution is to make a single form. What you're looking for is non-standard functionality and whatever solution you settle on my cause problems in the future.

Can I add multiple elements in a single DD of Zend Form

I am working on php and now trying to learning Zend, I am trying to collect the age (Year/month) using zend_form. So I want to put 2 text boxes in the same DD wrapper.
Can any one help me please.
Thank you
You could create a composite form element that comprises your two text fields. Then create custom decorator for that element to handle the aggregation. Then add the standard DtDd wrapper.
See this post by MWOP for an example: Creating composite elements
You can't place many input boxes in single element's decorator (with some exceptions like radioboxes or multicheckboxes). Instead that, you must to remove decorator Zend_Form_Decorator_DtDdWrapper from both elements and place them in new Zend_Form_DisplayGroup. Then You can set Zend_Form_Decorator_DtDdWrapper to that DisplayGroup

Customizing Zend_Form

I am dealing with Zend_Form right now and I am having a difficult time figuring out how to:
Use custom images for form buttons and,
Insert text and links in specific places (in my case I want to put a "forgot your password?" link before the submit button).
I've read through the manual but am not seeing anything about this.
I think Zend_Form_Decorators may help you: http://framework.zend.com/manual/en/zend.form.decorators.html
try to read also this article, I think it is the best one about decorators in Zend_Form
http://devzone.zend.com/article/3450-Decorators-with-Zend_Form
for your usage will be most helpful part about "Full Customization Using the ViewScript Decorator", try to find it in article
You can write straight up HTML in your view, just make sure that the element names, select options, hidden values etc. correspond to your Zend_Form elements (or set the element decorators to just ViewHelper and use echo $this->form->element where the elements should be). It's just the same POST data anyway when you submit the form. If you're using the form in more than one place, check out the ViewScript decorator.
CSS?
You can use an element's setDescription() method to add a description to it. If I remember correctly, this renders into a 'p class="hint"' element, but you can of course change that using the form decorators.

Categories