I am a Yii Newbie, and I have the following problem.
I have a form that is going to be like an admin - backend form. It will have loads of buttons each having their own "action" in the controller class. Now all I want to do is to validate the form elements, depending upon a "scenario" and to display appropriate error messages if all the parameters needed for the action is not filled in properly.
Can some one show me how I can do this without me having to reload the page?
[ I have found out a way, but I dont know if what I am doing is "technically" correct. I have submit buttons for all the actions that I want to perform in the form, and in the respective actions, I perform the validations and renderPartial form data back. OnSuccess of each button replaces the data of the entire "form-div" by the data that was retured from the controller. It works, but I want to know if this is the only way to achieve this.]
you should enable CActiveFom ajaxformvalidation property to true see the following example
in your controller action
you should uncomment the following lines
$this->performAjaxValidation($model);
in your view
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'test-form',
'enableAjaxValidation' => true,
));
?>
Related
here is a question that I am not able to find a way to do it. At the home view, I have a php section that will echo out if users input is just signed in when they submit form. But when I am doing this, it will give out a undefined error, because that controller is not yet executed.
This is my home view.
<?php include('header.php'); ?>
<?php echo $sign_in_results;?>
<?php include('forms/forms.php'); ?>
<?php include('footer.php'); ?>
This is my sign in controller, will get execute when submit pressed, the modal will return true or false.
function form_sign_in_controller(){
$this->load->model("form_sign_in");
$database_insert_results = $this->form_sign_in->check_user_detail_with_db();
$data['sign_in_results']= $database_insert_results;
$template = $this->load->View('main_view', $data);
}
I tried to use if(isset()).. The error is gone, but that section does not echo out anything. Any idea? Thanks
validate the data that comes from your sign in form. Never pass data from a form directly into a database query.
validating the data will tell you if the form has correct values. if it does not, then show the form again.
form data is validated - query your database
there are 3 possible results - you got a match OR you did not get a match OR there was an error. Check for these 3 different conditions, and then show the appropriate view.
so im working on a page to learn Yii. this is it: http://devcave.freeiz.com/
What i am trying to do is, when i click on login, a div is sliding down, where the login.php form should be.
The question is how do i render that into the main.php's div tag.
i tryed $this->renderPartial('//site/login',array('model'=>$model)); , but i get Undefined variable: model error.
I read trough the Understanding yii view rendering flow but it seems i didnt quit get the point.
Any suggestions please?
Use this:
$this->renderPartial('site/login',array('model'=>new LoginForm));
And in login view you should define action like this:
$form=$this->beginWidget('CActiveForm', array(
// ...
'action' => $this->createUrl( 'site/login' ),
// ...
The error is that you didn't defined a variable named $model in your controller and this variable is needed in the View login.
In your controller when you call
$this->render('yourView', array());
you'll have to add in the second array the datas that you want to pass to the Login view
$this->render('yourView', array('model'=>$model));
Don't hesitate to post your code if you need a more specific answer!
<?php
echo $this->Js->submit('Submit',array(
'before' => $this->Js->get('#beforesend')->effect('fadeIn', array('buffer' => false)),
'complete' => $this->Js->get('#beforesend')->effect('fadeOut', array('buffer' => false)),
'update' => '#contentc'
));?>
i was able to create an ajax call with the above code.
but what i want is to create multiple ajax request and append the output to previous ajax output.
my code overwrites the div simply because i have given an "update"
is there any way by which i can use jquery append.
You might be better off setting update to a hidden div, and then copy or move the contents of that hidden div to append to #contentc as a part of the complete action.
Generating javascript by passing php array is good in the case of simple function, but this is kind of difficult when you try for complex one,
So what i did was simply create a page with the javascript functions and add it in the $content_for_script variable.
I'm finding for a message passing system for codeIgniter. I found 3 ways.
1) set_flashdata(); from session class,
2) form_validaion from form validation class and
3) a variable set in controller and show it in view file.
1) is good, but we can only use for next server request. we can't use it for calling views.
2) can only use for form validation and it doesn't disappear on page refreshs.
3) is also doesn't disappear on page refreshs and we had to set it manually for form validation errors.
my want is like set_flashdata, show only one time and disappear on page refresh, doesn't need to set error message manually for form validations and able to use together with calling views.
It is not so bad if we use all three ways together, but I hope for better way.
Is there any ways or something for it?
Thanks!
Are you saying that you want to directly display flashdata in the view? Normally you just pass the flashdata to the view via the controller. I wouldn't recommend any other way.
Controller:
$data['var'] = $this->session->flashdata('var');
$this->load->view('view_file', $data);
View:
This is the flashdata: <?php echo $var; ?>
I'm requesting an ASP.net MVC view into a live box and the view contains form fields that have been marked up with attributes to be used by JQuery's unobtrusive validators plug-in.
The client script is not however working and my theory is that its because the validation framework is only being triggered on page load which has long since passed by the time the MVC view has been loaded into the live box.
Thus how can I let the validation framework know that it has new form fields to fix up?
Cheers, Ian.
var $form = $("form");
$form.unbind();
$form.data("validator", null);
$.validator.unobtrusive.parse(document);
// Re add validation with changes
$form.validate($form.data("unobtrusiveValidation").options);
You may take a look at the following blog post. And here's another one.
Another option, rather trick, which worked for me. Just add following line in the beginning of the partial view which is being returned by ajax call
this.ViewContext.FormContext = new FormContext();
Reference
For some reason I had to combine bjan and dfortun's answers...
So I put this in my view:
#{
this.ViewContext.FormContext = new FormContext();
}
And this execute this after the ajax call finishes:
var form = $("#EnrollmentForm");
form.unbind();
form.data("validator", null);
$.validator.unobtrusive.parse(document);
form.validate(form.data("unobtrusiveValidation").options);
I had a similar issue. I had a form that was using Ajax requests to re-display a part of the form with different form fields. I used unobtrusive validation by manually doing it on the client side using the
#Html.TextBoxFor
for my text boxes. For some reason the validation works when attempting to submit with invalid fields (i.e., the text boxes get outlined in red and the appropriate error messages display with the content I put in the
data_val_required
attribute, for example.
However, after I click a button that makes an Ajax request to modify the form with different fields and then submit again, only the red outline on the invalid fields display, but no error messages are rendered.
bjan's trick worked for me, but I still can't see what was causing the issue. All the HTML necessary to carry out the client-side validation was there I just can't figure out why the error message attribute values wouldn't display.
All I can think of is that the jQuery validation code doesn't make a second attempt to check the form fields after a submit was made.