Concrete5 validation not working on a single page - concrete5

I am trying to validate a form on a single page. Below is what I've tried but $validation->test() doesn't seem to be working. It's supposed to redirect to another page upon validating the first_name field. I get stuck at this url: http://localhost/mysite/index.php/buy/process/. I am using 5.6.x. Not sure what I am doing wrong.
public function process() {
$validation = Loader::helper('validation/form');
$validation->setData($this->post());
$validation->addRequired($this->post('first_name'), 'First Name must not be empty');
if ($validation->test()) {
....
....
$this->redirect('/buy/step2');
} else {
$errorArray = $validation->getError()->getList();
$this->set('errorArray', $errorArray);
}
}
This function is called from the view:
<form enctype="multipart/form-data" id="buy" class="buy-form" method="post" action="<?php echo $this->action('process')?>">.....</form>

I had to replace
$validation->addRequired($this->post('first_name'), 'First Name must not be empty');
with
$validation->addRequired('first_name', 'First Name must not be empty');
That did it.

Related

Angular Reactive form asynchronous operation

I am getting this error when I try to build a reactive form for creating a new password form.I have mentioned the source code below and when I remove the source code part then there is no error but without that my operation is not working as well. I think I have to add or delete something in my source code to get the desired output
main.ts:12 TypeError: Cannot read property 'value' of null
at FormGroup.passwordShouldMatch [as validator] (password.validators.ts:18)
at FormGroup._runValidator (forms.js:4089)
at FormGroup.updateValueAndValidity (forms.js:4050)
at new FormGroup (forms.js:4927)
at FormBuilder.group (forms.js:8924)
at new ChangePasswordComponent (change-password.component.ts:15)
at createClass (core.js:31987)
at createDirectiveInstance (core.js:31807)
at createViewNodes (core.js:44210)
at callViewAction (core.js:44660)
static passwordShouldMatch(control : AbstractControl) {
let newPassword = control.get('newPassowrd');
let confirmPassword = control.get('confirmPassowrd');
if (newPassword.value !== confirmPassword.value){
return { passwordShouldMatch:true };
return null;
}
}
As you didn't add any code snippet I am considering your form structure is something like this.
this.fb.group({
newPassowrd: [''],
confirmPassowrd: [''],
});
here, you include an custom validation function called passwordShouldMatch and this function looks fine. So, I assume that you did something wrong when you set the validator to that form group.
this.fb.group({
newPassowrd: [''],
confirmPassowrd: [''],
}, { validator: this.passwordShouldMatch});
this is how you should set the validation function for the form group. And in html your form should be something like this.
<form [formGroup]="form" novalidate (ngSubmit)="onSubmit(survey)">
<input type="text" placeholder="Untitled form" formControlName="newPassowrd">
<input type="text" placeholder="Untitled form" formControlName="confirmPassowrd">
<span *ngIf="form.hasError('passwordShouldMatch')">not match</span>
</form>
everything should work this way. Here is the working version of stackblitz

Value is not printed on JSP page

model.addAttribute("error",ErrorMessages.USERLOGINERROR);
is not working while using redirect
return "redirect:/userlogin";
I have google it but not found approciate answer
if(name.equals("false")){
System.out.println(ErrorMessages.USERLOGINERROR);
model.addAttribute("error",ErrorMessages.USERLOGINERROR);
return "redirect:/getuserloginpage";
}
JSP:
<form:form action="userlogin" method="post" commandname="login">
<div align="center" style="color: red">${error}</div>
<div align="center" style="color: red">${thanks}</div>
...
expected result should be :
Please check your email or password. actual results : printed nothing.
(no error or exception found)
Do one thing, you are using
return "redirect:/userlogin";
it means you have
#RequestMapping(value="/userlogin", method=GET)
public String yourMethod(Model model)
{
your code.....
return some_page_name;// from tiles.xml or directly as per you configuration
}
now use the same return
if(name.equals("false")){
System.out.println(ErrorMessages.USERLOGINERROR);
model.addAttribute("error",ErrorMessages.USERLOGINERROR);
//return "redirect:/getuserloginpage";
return some_page_name; // simple and easy but take care of functionality
}
Normal model attributes will not work with redirect.
Use redirect attributes instead like below.
#RequestMapping(value="/someURL", method=GET)
public String yourMethod(RedirectAttributes redirectAttributes)
{
...
redirectAttributes.addAttribute("rd", "rdValue");
redirectAttributes.addFlashAttribute("fa", faValue);
return "redirect:/someOtherURL";
}

Send a post parameter with path

I have a list of demands, that I can remove some, so I have created a path for every demand like that:
<a href="{{ path('etudiant_suprimer_demande',{'id':demande.id} )}}
And I have a controller like that:
class EdudiantController extends Controller
{ //codes
public function suprimerdemandeAction($id){
// echo $id;
$em = $this->getDoctrine()
->getEntityManager();
$demande = $em->getRepository('AcmeEstmSiteBundle:Demande')
->find($id);
if ($demande == null) {
throw $this->createNotFoundException('Demande[id='.$id.']inexistant');
}
if ($this->get('request')->getMethod() == 'POST') {
$this->get('session')->getFlashBag()->add('info', 'Demande bien supprimée');
return $this->redirect( $this->generateUrl('acme_estm_site_espace_etudiant'));
}
//return $this->render('SdzBlogBundle:Blog:supprimer.html.twig',array('demande' => $demande));
return new Response();
}
And the routing is:
etudiant_suprimer_demande:
path: /profile/espace/suprimerdemande/{id}
defaults: { _controller: AcmeEstmSiteBundle:Edudiant:suprimerdemande }
methods: POST
What I want is do not show the id of demands in the URL, that means I want this action with a post method but I have this error:
No route found for "GET /profile/espace/suprimerdemande/3": Method Not Allowed (Allow: POST)
TL;DR
<form action="{{ path('etudiant_suprimer_demande',{'id': demande.id} )}}" method="post">
<button type="submit">Delete</button>
</form>
Explanation
The code you have now only generates an <a> link, which when a user clicks it, his user agent sends a GET request to the server, thus the error you get.
In order to create a POST request, you need a <form>.
To answer your question regarding showing ID in the generated URL, take a look at this answer. You'll have to change your controller and routing.

src vs srctarget vs srcwait

I continue to work on a project that I do not fully understand yet. I encountered the following line of code:
<iframe id="AddDialog" style ="overflow: hidden; width:1150px; height:450px;" class="Dialogframe" scrolling="no" srcwait=#Html.Raw("'" + Url.Action("Index", "FieldChooser") + "'") srctarget=#Html.Raw("\"" + Url.Action("Index", "FieldChooser", new { ColumnFormat = false, resultmodel = Guid.Empty, datatype = "", multiselect=false }) + "\"") src=#Html.Raw("\"" + Url.Action("Loading", "FieldChooser") + "\"")></iframe>
Visual Studio tells me that srcwait and srctarget are not valid HTML5-elements, but it seems to work. The Loading View is shown for a few seconds and then the Index() method is executed (the one called in srctarget).
I am also not able to find anything on the internet about the attributes srctarget and srcwait. So what are the differences between src, srctarget and srcwait? Do they even exist or is that some invention of the person that worked on it before me?
I have a function in the FieldChooserController
[HttpPost]
public ActionResult Index(string id)
{
...
}
I want this to be called when I click on the OK button. I assumed that the srcwait part is meant for that because the call looks like that, but the function is never called.
Please bear with me and tell me if you need to see more code, at this point I have no idea what is important.
buttons: {
OK: function() {
//Save selected Value
$( this ).dialog( "close" );
if (GlobalName !=''){
addwhere(GlobalName,opts.sourceel,GlobalDefVal,GlobalDataType,GlobalValue);
}
$('#AddDialog').attr('src', $('#AddDialog').attr('srcwait'));
},
Cancel: function() {
$( this ).dialog( "close" );
$('#AddDialog').attr('src', $('#AddDialog').attr('srcwait'));
}
}
There most probably is a piece of JavaScript running, which sets the src to srcwait when an operation is performed where the user will be waiting for a wile, for example to show a loading screen.
As for your code, if you have a HttpPost annotated Index() method you wish to call upon a button click, you must create a form and let it post there:
#using (Html.BeginForm("Index", "FieldChooser", FormMethod.Post)
{
<input value="OK" type="submit" />
}

EmbeddedForm symfony saving

I'm experiencing a problem to solve the newly added "Prix" in my Soiree Form
I've embedded the PRixForm in my SoireeForm and i'm using ajax calls to add as many prix as we want.
The problem is, when I want to add a new PRix by clicking on the "+" and after filling my fields nothing is saved...
When i'm editing or filling the first default embedded form prix it works which is weird. Only those added by ajax are not saved..
Here is my code
in my form, SoireeForm:
public function addNewPrix($number){
$new_prix = new BaseForm();
for($i=0; $i <= $number; $i+=1){
$pri = new Prix();
$pri->setSoiree($this->getObject());
$prix_form = new PrixForm($pri);
$new_prix ->embedForm($i, $prix_form);
}
$this->embedForm('new', $new_prix);
}
public function saveEmbeddedForm($con = null, $forms = null){
$dataForms = $this->getEmbeddedForm('new')->getEmbeddedForms();
foreach ($dataForms as $dataForm)
$dataForm->getObject()->setSoiree($this->getObject());
parent::saveEmbeddedForm($con, $forms);
}
In my action:
public function executeAdd($request)
{
$this->forward404unless($request->isXmlHttpRequest());
$number = intval($request->getParameter("num"));
$this->form = new SoireeForm();
$this->form->addNewPrix($number);
return $this->renderPartial('addPri',array('form' => $this->form, 'num' => $number));
}
my partial addPri:
<li>
<?php echo $form['new'][$num]['titre']->renderLabel();?>
<?php echo $form['new'][$num]['titre']->render()?>
<?php echo $form['new'][$num]['montant']->renderLabel();?>
<?php echo $form['new'][$num]['montant']->render();?>
<br />
<br />
and my prix.js file:
newfieldscount = 0;
function addPri(num) {
return $.ajax({
type: 'GET',
url: '/backend_dev.php/soiree/add?num='+num,
async: false
}).responseText;
};
var removeNew = function(){
$('.removenew').click(function(e){
e.preventDefault();
$(this).parent().remove();
})
};
$(document).ready(function(){
$('#add_prix').click(function(e) {
e.preventDefault();
$("ul#extraprix").append(addPri(newfieldscount));
newfieldscount = newfieldscount + 1;
$('.removenew').unbind('click');
removeNew();
});
});
I guess the problem comes from my saveEmbed method in my form, but i don't understand why or how to make everything work as it should.
Thank you in advance you guys
So you want save an 1:n relation, so you should find everything you need here
http://www.thatsquality.com/articles/stretching-sfform-with-dynamic-elements-ajax-a-love-story
http://www.symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms
If not, please provide information or code of your PrixForm->configure() function !
PS: Your saveEmbeddedForm() function is unneeded, you'll find more information about the form saving process in the second link in chapter 6 !

Resources