I have a problem with my registration form. So, I have some fields that are generetad when user click on link, and those fields are lost after I press form Submit button if validation fails. If validation pass, everything is correct and data is saved into database.
So these are that fields, and also script to add new field:
<script>
$(document).ready(function() {
var MaxInputs = 8; //maximum input boxes allowed
var FacebookInputsWrapper = $("#FacebookInputsWrapper"); //Input boxes wrapper ID
var AddButton = $("#FacebookAddMoreFileBox"); //Add button ID
var x = FacebookInputsWrapper.length; //initlal text box count
var FieldCount=0; //to keep track of text box added
$(AddButton).click(function (e) //on add input button click
{
if(x <= MaxInputs) //max input box allowed
{
FieldCount++; //text box added increment
//add input box
$(FacebookInputsWrapper).append('<div style="margin-top:10px;"><input id="SocialMediaLink' + FieldCount + 'Link" type="hidden" name="data[SocialMediaLink][' + FieldCount + '][type]" value="fb" /><input id="SocialMediaLink' + FieldCount + 'Link" type="text" name="data[SocialMediaLink][' + FieldCount + '][link]" class="input-xlarge" placeholder="Facebook link" /><a style="background:0;color:black;" href="#" class="facebookremoveclass">×</a></div>');
x++; //text box increment
}
return false;
});
$("body").on("click",".facebookremoveclass", function(e){ //user click on remove text
if( x > 1 ) {
$(this).parent('div').remove(); //remove text box
x--; //decrement textbox
}
return false;
})
});
</script>
HTML
<li><label>Facebook</label>
<div >
<div style="float:right; margin-top:10px;" id="FacebookInputsWrapper"><?php echo $this->Form->input('SocialMediaLink.0.type',array('type'=>'hidden','value'=>'fb')); ?><?php echo $this->Form->input('SocialMediaLink.0.link',array('type'=>'text','class'=>'input-xlarge','label'=>false,'div'=>false,'placeholder'=>'Facebook link', 'error' => array(
'attributes' => array('class' => 'inputerror')
))); ?><label style="color:#FF0000;"><?php echo @$valid_social; ?></label>
<a style="margin-left:10px;background : 0;color:black;" href="#" id="FacebookAddMoreFileBox">Add another Facebook account</a></div>
</div>
So my question is: is it possible to save dynamically generated form fields and their values after validation fails and how?
Thank you