I added extra select box (choice type) and mapped=>false see the code below.
However when I submit the form. It returned the error msg "This value is not valid."
$form->add('extraField' ,'choice', array(
'required' => false,
'choices' => $arrayChoices,
'mapped'=>false,
'data' =>$id
));
What did I do wrong here?
Ok, after this doc : http://symfony.com/doc/current/reference/forms/types/choice.html#choices
I don't see 'data' option. Try to remove it please. (and , what is the use of this field [data]? )
Are you sure that $id is valid array key for $arrayChoices?
Also, when you submit the form, the request must contain a valid key inside of $arrayChoices
Related
i have an ChoiceType::class input field in my form with, now just as an example, two choices:
'choices' => ['type1' => '1', 'type2' => '2']
now when the user select type2 i want to add an exta TextType::class inputfield to the form.
But i dont want to show the input field before and i want it to be required if selected type2 and not if selected type1.
I hope it make sense, i try it to to with javascript and set the attribute to hidden or not, but
then the form is not been send because of the required attribute.
I tried it with form events but did not get it to work in that way.
Thanks
You were on the right way, you have to do it in Javascript. You just need to manage the attr required in Javascript so that the form does not block you with something like this:
Remove the required attribute from a field: document.getElementById("id").required = false;
Make a field required : document.getElementById("id").required = true;
And you can check if the form can be sumitted with : document.getElementById("idForm").reportValidity();.
I using implementation of conditional fields with data-attributes, e.g.:
->add('typeField', EnumType::class, [
'label' => 'Type',
'class' => MyTypeEnum::Class,
])
->add('someField', TextField::class, [
'data-controller' => 'depends-on',
'data-depends-on' => 'my_form_typeField',
'data-depends-value' => MyTypeEnum::OTHER->value,
])
On frontend JS stimulus controller show/hide someField depend on typeField value.
And validation() function in object ('data_class' in formType) make custom validation, e.g.:
/**
* #Assert\Callback
*/
public function validate(ExecutionContextInterface $context)
{
if ($this->typeField !== MyTypeEnum::OTHER) {
$context->buildViolation('message')->atPath('typeField')->addViolation();
}
}
I'm having little problem, I'll try to describe what is it about: at first page I got only form input field (username expected), then on submit I'm proceed to second page where I got form with fields that I've read from database and put them inside input value="xxx". When i retype some of that fields, and submit on next script form parameters are empty($field) == true. Fields are not empty and when I submit they are marked as empty variables.
Here's my code hope it will help:
My second page view (CODE)My controller's method to take parameters from second page view (CODE)
Here's screenshots maybe it can be helpful:
i.stack.imgur.com/1zx9S.png
i.stack.imgur.com/3UH97.png
i.stack.imgur.com/ipB6Y.png
Anyone got idea why am I getting empty variable when I read them in script?
Thanks in advance
I'm honestly sorry, I've just realized that in mixing that php and html, I've forgot to add name attribute in html tags.
You should pass a data array to the db. Something like this
public function submitEditChanges($id){
$data = array(
'name' => $this->input->post('name'),
'surname' => $this->input->post('surname'),
'city' => $this->input->post('city'),
'email' => $this->input->post('email'),
'username' => $this->input->post('username'),
'password' => sha1($this->input->post('password')),
'passconf' => sha1($this->input->post('passconf'))
);
$this->db->where('id', $id);
$this->db->update('users',$data);
}
I've inherited a piece of code from an old developer and currently trying to figure out the method behind how it submits.
I'm currently trying to get a quick fix in place where I hide one of his fields to get the form submitting, the issue I'm having is that I can't seem to set the dropdownlist to a default value, which should be United Kingdom, with the id 826 in the database.
echo CHtml::ActiveDropDownList($address, 'country_id', CHtml::listData(Country::model()->findAll(), 'id', 'name', 'continent'), array(
'class' => 'col-md-5 hidden',
'prompt' => 'Select Country',
'label' => 'something here',
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('/user/UpdateRegions'),
'dataType' => 'json',
'data' => array('country_id' => 'js:this.value', 'YII_CSRF_TOKEN' => Yii::app()->request->csrfToken),
'success' => 'function(data) {
$("#Address_country_region_id").html(data.country_id);
$("#Address_country_region_id").removeClass(\'hidden\');
if($("#venue_id").val() === "") {
$("#Address_country_region_id").addClass(\'hidden\');
}
}',
)));
$path = CController::createUrl('/admin/user/UpdateRegions');
$id = $address->id;
// On Load
Yii::app()->clientScript->registerScript('ready', '
$.ajax({
type: \'POST\',
dataType : \'json\',
data: {\'country_id\': $(\'#Address_country_id\').val(), \'update_id\' : "' . $id . '"},
url: "' . $path . '",
success: function(data){
$(\'#Address_country_region_id\').html(data.country_id);
}
})
');
How do I get this dropdownlist pointing towards the country_id of 826 as the page loads so I can hide the field and pass the validation of the form.
regards.
this code you pasted does this:
the first part renders a dropdown which does an ajax request on change to /user/UpdateRegions to fill the dependent dropdown for the regions.
the second part does also an ajax request when the page is "ready". So that the region select is filled when the page is ready.
Your script should output the id 826 if you printout the current set id.
If not the value isn't correctly set to the address model.
echo $address->country_id;
If you want to set the default value you can do this at multiple places. e.g. in the controller 'before' the actual submitted value is set or in the model code itself (depends on your code e.g. if you have an additional form model which is extended from your activerecord model).
The correct line in your controller could be before something like this:
$address->country_id= 826;
before one of these lines in your controller
$address->attributes=Yii::app()->request->getQuery(get_class($address));
or
$address->attributes=Yii::app()->request->getPost(get_class($address));
or
$address->attributes=$_POST['address']; // or $_GET
If you have difficulties to find the correct position post some controller code.
I guess you could find some help also here
Yii 1.1: An Easy Solution for Dependent dropDownList Using AJAX
To set default value for $address model, put
$address->country_id = Yii::app()->request->getParam(get_class($address).'[country_id]', 816);
before
echo CHtml::ActiveDropDownList($address, 'country_id', CHtml::listData(Country::model()->findAll(), 'id', 'name', 'continent'), array(
I'm using a radioButtonList like this one:
$form->radioButtonList(Store::model(), 'product',
array(CODE1 => TEXT1,
CODE2 => TEXT2,
CODE3 => TEXT3)
);
This radioButtonList is part of a form with more fields. After submiting, if any field is incorrect, I show some error message and populate the correct fields using $_POST.
All the fields get its previous values except this radioButtonList. I need to set checked the value of the radioButtonList which was selected before submit, but I can't find how to do it.
Create $model = new Store(); in your action, pass it to view and use $model variable instead Store::model(). This should help.
UPD: You need to use the same $model after validation.
You can use
CHtml::radioButtonList(string $name, string $select, array $data, array $htmlOptions=array ( ));
In Your case it will be
CHtml::radioButtonList('product',$_POST[product],array(CODE1 => TEXT1,CODE2 => TEXT2,CODE3 => TEXT3));
Finally, I got a solution. (not an elegant one, but it works)
From the view:
Store::model()->product = $_POST["Store"]["product"];
Right before display the radioButtonList
I've got some problem with symfony form value (i guess it's the clean value, but not so clear yet). Here's the problem :
I got a sfFormDateJQueryUI widget setup like this in my form :
$this->setWidgets(array(
'needDate' => new sfWidgetFormDateJQueryUI(),
));
$this->setValidators(array(
'needDate' => new sfValidatorDate(array(
'required' => true,
'date_format' => '/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/',
'date_output' => 'd/m/Y'
)),
));
Then when i submit, say 26/06/2010, it turns out right in the HTTP Header (viewed via Firebug) and $request (i just print it). But after i get the value via
$formVal = $form->getValues();
the date value in $formVal["needDate"] become today's date (03/06/2010). I really don't understand, and after checking in the API documentation it says that the getValues will return the 'cleaned' value. Is that because of it ? I don't understand what's 'clean'.
Thanks before..
Somehow I solved the problem.
It turns out that the value can't validate, so when I changed the validator to:
'needDate' => new sfValidatorRegex(array(
'pattern' => '/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/'
))
everything works fine.