How to Repopulate a form field without setting rule as required in Codeigniter? - codeigniter

When I try to populate a form field which is not required in form validation doesn't repopulate. Let's say I have a field but I don't want it to be required but I want it to be repopulated. How can I do it ? I think this is a bug for codeigniter.

This piece of code solved my problem. But I have to use helpers for that. At least I have a solution.
function value_field($field, $default='') {
return (isset($_POST[$field])) ? $_POST[$field] : $default;
}

You have to set a validation rule on a field in order for it to repopulate on an invalid submission. Luckily, you can pass regular PHP functions, not just validation rules, to the set_rules() method so you could just set trim or something rather than required.
$this->form_validation->set_rules('your_field', 'Your Label', 'trim');
That will repopulate the field and not make it a required field.

Using the form helper's set_value() works even for non-required fields, I'm not sure where your problem lies...
echo form_input(array('name' => 'username', 'value' => set_value('username'));

Related

Laravel and Images validation

I have a little question about Laravel ...
I have a big form with which it is possible to send images.
I did a "PageStoreRequest" with my validation rules and everything works fine.
Depending on the length of the form, I would like that if an image is not valid, it is simply "deleted" (or "ignored") from the form and that the rest of the form is treated correctly ... So that the rest of the images are not to be sent again ...
I wonder if there is a method that would check a single field?
(like my Image Field)
And if it is not valid, delete it?
(knowing that the other fields must also pass to the Validator)
Thanks for your ideas :)
You can validate image field.
$request->validate([
'image' => 'mimes:jpeg,jpg,png'
]);
And then field is validated;
if ($request->hasFile('image') && $request->file('image')->isValid()) {
// Make something
}

Symfony2, dynamically refresh form choices

I am creating a simple Blog system with symfony2. Each blog Post is bound to a certain amount of Tags.
Tags can be selected with checkboxes when creating a new blog post. Now I want to be able to dynamically add new tag-checkboxes to the form.
The AJAX part is done and working, I can add new tag names to the Tag entity and append the new checkboxes to the form.
The problem is when I submit the form, symfony2 doesn't recognize the new added tags because they don't belong to the Tag entity yet (at the time the form was generated).
For example: after submitting the form, I dump:
$tags = $form->get('tags')->getData();
The controller ignores the tags that were added through ajax.
I know it has to be solved with events somehow, I already read this documentation: http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html but can't figure out how to implement it for this specific case.
Here is my form builder for "PostType":
$builder
->add('title')
->add('content', 'textarea')
->add('tags', 'entity', array(
'class' => 'Bundle:Tag',
'property' => 'name',
'multiple' => true,
'expanded' => true,
))
->add('save', 'submit')
;
You can try to use this: Form Collection
Make sure you persist the newly added Tags before submit the form, and the checkboxes has the right names and values.
Names should be like "post[tags][]" and values should be the database ids of the Tag entities.

MVC Custom Attribute - Client-side Validation - Warning: No message defined for 'field'

I tried to create a custom data annotation validation attribute (NameValidationAttribute) in MVC 5 project using VS2013. I was able to successfully add the client side validation and the error message for custom validation is getting displayed as soon as the focus leaves the textbox. However, the standard attributes like [Required] and [Range] validators are now not showing proper error messages, says 'Warning: No message defined for 'field' ' (See below screenshot).
Question:
- Why the standard validation error messages are showing as "Warning: No message defined for UnitsInStock"? What am I missing?
Below is my custom client validation script:
I included following scripts in EditProducts page.
Please note that the error messages for UnitPrice, UnitsInStock and ReorderLevel fields are defined with Range validation attribute (see below).
FYI, I tried to change the order of the scripts in ProductEdit page but still its showing the same message.
Please advise!
I ran into this issue. I had created an MVC attribute called PasswordAttribute, with a client side validator called 'password'.
$.validator.addMethod('password', function (value, element, params) {
...[validation logic]
});
$.validator.unobtrusive.adapters.addBool('password');
As soon as I added this logic to the solution I got the error you saw on my Password and ConfirmPassword fields. Note that this occurred before the attribute was even used. Simply renaming the client side validator to 'passwordcheck' fixed the issue. I initially thought that the name 'password' possibly clashed with one of the pre-defined validators (cf. jQuery Validation Documentation) but this doesn't appear to be the case. I suspect now that it is a clash with the name or value for some input field attribute. Anyway, the solution was simply to rename the validator.
jQuery unobtrusive need data-msg for default validate message.
This is how to apply dynamic error message from your model to Html
$.validator.unobtrusive.adapters.add("requireif", function (options) {
$('#' + options.element.id).attr("data-msg", options.message);
// Add rule ...
});
You can change default warning message.
$.validator.addMethod("requireif", function (value, element, pair) {
// validate logic
return true/false;
}, "YOUR DEFAULT MESSAGE HERE");
Or
#Html.TextBoxFor(m => m.Property1, new { Class = "form-control", data_msg = "YOUR DEFAULT MESSAGE HERE" })
Or if you can put it directly to your Html like this.
<input class="form-control" data-msg="YOUR DEFAULT MESSAGE HERE"/>

Symfony 2 custom validator on field

I made my custom date range validator, and I'm using it on date properties of my entity..
Though, when I get an error it's attached to the form and not to the field.
So I can't display errors with {{form_errors(form.date)}}. (edit : form is a prototype of a child collection)
I saw that : Custom constraint validation error doesn't display next to field in Symfony2 . But I don't want to explicitly specify on which field name the error should be attached..
Maybe it's related to the fact that this error is in a collection of the main form (using his prototype) ?
I could also add that the error is attached to the main parent form (my validator is on a field, which is in a form, which is a collection of a form, which is an embedded form of the main form).
How can I do ?
EDIT : It might be related to that : https://stackoverflow.com/questions/15907415/symfony2-data-prototype-error-bubbling
It is surely related to error_bubbling. It defaults to true if the form is compound, so you should set it to false value.
$builder
->add('field', 'collection', [
'type' => new ChildFormType(),
'error_bubbling' => false,
]);
See symfony doc page about error_bubbling for more details

How do I repopulate fields BEFORE validation is passed?

The fields I am making required to fill out, should repopulate the correctly filled out fields, while NOT submitting the form and posing errors for the incorrectly filled out fields. What is the best way to do that?
Please note that with this code, I am repopulating the fields as they should be upon submitting the form correctly and they are all displaying when page is reloaded.
<div class="dropdown_structure">
<?php
if($user['location'] == "empty")
{
echo country_dropdown('location');
}
else
{
echo country_dropdown('location',$user['location']);
}
?>
</div>
Also please note that I've tried inserting the value in the input fields.
$data = array( 'name' => 'location', 'value' => $this->input->post('location'));
echo relation_dropdown('location');
Thanks in advance
Hi if you are using the following country dropdown helper you can set value validation in following way
Country dropdown helper
country_dropdown('location',array(),set_value('location'))
even in your second dropdown use set_value('field_name') will work. if validation is failed your selected value will be remain.

Resources