everyone. I've started using atk4 in a personal project a couple weeks ago and have been facing some difficulties since then. This specific question I want to ask is about how to make form validations when using the CRUD component shipped with the atk4 framework.
I have already tried several different solutions, none of them solving my problem.
I have a feeling that the problem here might be that the form validation happens within the call of the method $form->isSubmitted() (am I correct?). Since when using a CRUD component within a Page we don't use that way of processing the form submission, we'd have to find alternatives to it. For example, let's say I have a Page with the following init() function:
function init() {
parent::init();
// create a CRUD and set a model to it
$crud = $this->add('CRUD');
$m = $crud->setModel('Person');
if ($crud->form) {
$fn = $crud->form->getField('first_name');
$fn->validateNotNull('The first name must not be empty.');
}
}
Even though I've added the validation to the first name field, it won't be validated. I've tried several things, unsuccessfully. I tried to extend the CRUD class and reimplement the formSubmit($form) function, adding the validation there. Even if I do it, it doesn't work.
Originally (in the CRUD class), there is the function:
function formSubmit($form){
$form->update();
$this->api->addHook('pre-render',array($this,'formSubmitSuccess'));
}
I tried to iterate through the form's fields and call its validate() method, but it didn't work. Also, if I try to do alter the function (in a MyCRUD class, let's say) like below,
function formSubmit($form){
if ($form->isSubmitted()) {
$form->update();
$this->api->addHook('pre-render',array($this,'formSubmitSuccess'));
}
}
there happens an infinite loop... Could someone help me out?
[EDIT]
One last question intimately related to this one. I've just tried to do the exact same validation proposed by romanish below but, instead of adding a CRUD to a page, I was just adding a Form, and it doesn't work -- though the CRUD does work. Instead, there happens a "Error in AJAX response: SyntaxError: Unexpected token
CRUD component respects the validation you're doing inside the model. When data is entered into the form and button is clicked, $model->update() is called.
The execution continues into beforeUpdate() hook, which is the one you need to intercept.
http://agiletoolkit.org/learn/understand/model/actions
class Model_Book extends Model_Table {
function init(){
parent::init();
// .... more definitions ...
$this->addHook('beforeSave',$this);
}
function beforeSave(){
if(strlen($this['book_name']<10))
throw $this->exception('Name of the book is too short')
->setField('book_name');
}
If model is unable to save itself and will produce exception, Form automatically show it as a field error.
Related
I'm attempting to create a custom display in yii2 framework using this code in my site controller:
/******/
public function actionChartDisplay()
{
return $this->render('chartDisplay');
}
for testing purposes I pasted the form name in my actionAbout function as a parameter to the render function in it. It worked with this:
public function actionAbout()
{
return $this->render('chartDisplay');
}
But I need to create many custom views in yii2 and this won't be a solution.
This is the error I get
I'm curious as to why it is. Since I was following this tutorial and came across this weird behaviour.
My 'chartDisplay.php' file is merely a "hello world" that does work with the action about function.
in yii2, the controllers and actions with multiple words, that are marked by capital letters are divided by - in your request, so in your case the route would be some/chart-display
Apparently as #SmartCoder pointed out it was an error on how Yii2 Handles the action functions in its controller however I didn't mark his answer as the solution right away because implementing it resulted in an error. So aside from that I'm posting the way I solved it.
So instead of using chart-display I simply changed it for "charts" like this:
public function actionCharts(){
return $this->render('charts');
}
Changed the name of my file so it fits to charts.php and it worked.
I'm working in CakePHP 2 and I have been trying to use dynamic validation, because I need different rules for when I have a user register an account (username and email are both required), but when a user fills out a forgot password form, username or email are required but you're not allowed to fill out both username and email.
I call $this->User->setValidationRules($this->request->params['action']) in beforeFilter from the UsersController.
then in the User Model class, I have this function:
public function setValidationRules($action) {
if ($action == 'forgotten_password') {
$this->validator()->getField('username')->setRules($this->validationSets['forgottenPassword']);
$this->validator()->getField('email')->setRules($this->validationSets['forgottenPassword']['email']);
} else {
$this->validator()->getField('username')->setRules($this->validationSets['default']);
$this->validator()->getField('email')->setRules($this->validationSets['default']['email']);
}
return true;
}
But I keep getting this error: "Call to a member function setRules() on a non-object."
I have been trying to figure this out for so long. In the AppModel class, I added the line:
App::Uses(Validator, Model);
But I still get the same error.
I have been trying to figure this out for a long time, searching all over the internet for answers, but I can't seem to find any. If anyone could help. I would much appreciate it.
$this->validator()->getField('field') can return null if your fields are not defined in the model.
Can you please post the entire model definition and also the controller function where you call setValidationRules? (Is this the controller that has the same name as the model or is a different controller?)
With what you have posted so far, it seems like you're calling the Validator methods before the validator has been properly set up. This indicates either a typo in the field names (unlikely), a model that was not properly initialized or the call to setValidationRules happens before the Model was fully build. I'd make sure that the controller is the only one calling that function.
You can always dump the values of:
$this->validator()
$this->validator()->getField('field')
$this->validator()->getField(null) - to get a list of all the available fields in the validator and possibly an indication of the problem.
And you can have a look at the stack trace of the call to setValidationRules() to make sure it happens when you think it should happen.
If more data is posted in the question I'll be able to refine my answer.
Is this the right way to create a form widget in FuelPHP?
class Controller_Widget extends Controller
{
public function action_show()
{
if (Request::is_hmvc())
{
// show form widget
}
else
{
// process form
}
}
}
The form action calls the same function to process, but where will it redirect to after? how will it show validation errors?
Note: The widget should not be accessible through the URL; the form should not display itself if accessed directly through the URL.
EDIT:
Found a similar problem in CodeIgniter HMVC and dynamic widgets but this is from 3 years ago. Maybe the FuelPHP guys have found a better way to do this.
This seems like a weird method, a method that is called show but handles both showing and manipulating data? A method called "show" (or get, fetch, read, etc) shouldn't do any editing, it's name expressly seems to imply that it's a read-only operation.
But also how it proceeds seems off. Its read-operation is HMVC only but its manipulation operation is non-HMVC only? That's really a wrong way to determine what the method should do, whether or not it is HMVC should have no baring on what it does.
In your case I'd split this up into 2 methods: one for retrieval (show()) and another for manipulation (edit() for example). Whether you want to make these HMVC only is up to you. There's more ways than one to solve that, I'd go with:
if ( ! Request::is_hmvc())
{
throw new Exception('Only HMVC access allowed.');
}
Or make it impossible to route to the method by rerouting it in your routes.php config file and then using the HMVC routing overwrite as discussed here: https://stackoverflow.com/a/9957367/727225
Here is the code using CodeIgniter:
The problem I encounter:
The controller will have some functions call view, and it
separated, but it is still very close with the logic itself, if the
controller change to return in JSON or XML to display result, it seems
very trouble.
Seems many method, but each one is depends another.
I think it is difficult to track the code.
Please give some suggestions thank you.
*Please reminded that, it is only the controller class. the load view is actually prepare the data for the view, won't render the page. also the doXXX function call model is only use the model method, it won't have any SQL statement. The MVC is separated, but the controller also have the functions related to the view or model, make it quite messy.
class User extends CI_Controller
{
public function register()
{
//check is logged in or not
//if not logged in , show the register page
}
public function show_register_page()
{
//generate the UI needed data , and call the view to render, and will the user will post back a valid_register function
}
public function valid_register()
{
//do all the valid logic, if success,
//do the do_register
//if fail, valid_register_fail
}
public function valid_register_fail()
{
//check is logged in or not
//show the valid register fail page
}
public function show_valid_register_fail_page()
{
//generate the UI needed data , and call the view to render
}
public function do_register()
{
//insert data in the db, the Model will be called
//if something go wrong in db, show the error page
//if everything is success, show the register success
}
public function show_db_error_page()
{
//generate the UI needed data , and call the view to render
}
public function show_register_success()
{
//generate the UI needed data , and call the view to render
}
}
1. The controller will have some functions call view, and it
separated, but it is still very close with the logic itself, if the
controller change to return in JSON or XML to display result, it seems
very trouble.
Depends on how you organized your code and what you actually pass into the view (template). If that's well structured, you can have one view for HTML, one for XML and one for json, where-as json normally just encodes the view variable's (see json_encodeDocs).
2. Seems many method, but each one is depends another.
Well, just don't do it :) The names look like you wanted to "code that into". Keep it apart. Make those function actually actions that a user performs:
register - that action handles the registration process
Make a login controller out of it that handles anything you need:
login - the login action
lost_password - the lost password action
register - the registration action
activate - the registration activation action
Everything else does not belong in there. There is no need for an action to display some page - the controller itself can decide which view to pick.
Next to that you don't need to display database errors. CI takes care of that. Just put only in what's needed and keep things simple. That should help you to reduce the number of methods and the code therein as well.
3. I think it is difficult to track the code.
Sure. Too many functions with not really speaking names. Keep things simple. It's not easy, but give naming and reducing the overall logic some love.
I am using Symfony with propel to generate a form called BaseMeetingMeetingsForm.
In MeetingMeetingsForm.class.php I have the following configure method:
public function configure() {
$this->useFields(array('name', 'group_id', 'location', 'start', 'length'));
$this->widgetSchema['invited'] = new myWidgetFormTokenAutocompleter(array("url"=>"/user/json"));
}
In MeetingMeetings.php my save method is simply:
public function save(PropelPDO $con = null) {
$this->setOwnerId(Meeting::getUserId());
return parent::save($con);
}
However propel doesn't know about my custom field and as such doesn't do anything with it. Where and how to I put in a special section that can deal with this form field, please be aware it is not just a simple save to database, I need to deal with the input specially before it is input.
Thanks for your time and advice,
You have to define a validator (and/or create your own). The validator clean() method returns the value that needs to be persisted.
In Doctrine (I don't know Propel) the form then calls the doUpdateObject() on the form, which in turns calls the fromArray($arr) function on the model.
So if it's already a property on your model you'll only need to create the validator. If it's a more complex widget, you'll need to add some logic to the form.