Jump to content

Creating pages via APi but show error when duplicate?


vxda
 Share

Recommended Posts

Hi guys,

here is my code, im creating pages via form, but i want to show message when user allready submited his email. how can i achive this ?

 

$out = '';
$form = $modules->get("InputfieldForm");
$form->action = "./";
$form->method = "post";
$form->attr("id+name",'subscribe-form');
$field = $modules->get("InputfieldEmail");
$field->label = "E-Mail";
$field->attr('id+name','email');
$field->required = 1;
$form->append($field); // append the field

$submit = $modules->get("InputfieldSubmit");
$submit->attr("value","Subscribe");
$submit->attr("id+name","submit");
$form->append($submit);

if($input->post->submit) {
	$form->processInput($input->post);
	$email = $form->get("email");
	if($email && (strpos($email->value,'@hotmail') !== FALSE)){
        $email->error("Sorry we don't accept hotmail addresses for now.");
    }
    if($form->getErrors()) {
        $out .= $form->render();
    }else{
    	$p = new Page(); // create new page object
		$p->template = 'newsletter'; // set template
		$p->parent = wire('pages')->get('/newsletter/'); // set the parent 
		$p->name = $form->get("email")->value; // give it a name used in the url for the page
		$p->title = $form->get("email")->value; // set page title (not neccessary but recommended)\
		$pageName = $p->name;
		$name = $pageName;
		$p->save();
		$out .= "<p>You submission was completed! Thanks for your time.";

    }
}
else {
   		// render out form without processing
   		$out .= $form->render();
	}
echo $out;
?>
Link to comment
Share on other sites

All you need to do is use a selector to see if the email already exists.

Do this inside the 

if($input->post->submit) { 

and if it exists, do an:

$email->error("Sorry, this email address is already in our system");

The selector can use something like:

$email_exists = $pages->find(selector);

if(count($email_exists) > 0){

to determine is the email address is already in the system.

Link to comment
Share on other sites

Ty for reply Adrian

still not working ;/
thing is i made some pages one of is asd@asd.pl

and when i run this :

$email_exists = $pages->find('asd@asd.pl');
		if(count($email_exists) > 0){
			echo 'error';
		}

i get error :
 

Fatal error: Exception: Unknown Selector operator: '' -- was
your selector value properly escaped? (in
D:\xampp\htdocs\projects\midven\wire\core\Selectors.php line 165)

#0 D:\xampp\htdocs\projects\midven\wire\core\Selectors.php(190):
Selectors->create('asd', '', '@asd.pl')
#1 D:\xampp\htdocs\projects\midven\wire\core\Selectors.php(63):
Selectors->extractString('asd@asd.pl')
#2 D:\xampp\htdocs\projects\midven\wire\core\Pages.php(143):
Selectors->__construct('asd@asd.pl')
#3 [internal function]: Pages->___find('asd@asd.pl')
#4 D:\xampp\htdocs\projects\midven\wire\core\Wire.php(271):
call_user_func_array(Array, Array)
#5 D:\xampp\htdocs\projects\midven\wire\core\Wire.php(229):
Wire->runHooks('find', Array)
#6
D:\xampp\htdocs\projects\midven\site\templates\global\newsletter.html(46):
Wire->__call('find', Array)
#7
D:\xampp\htdocs\projects\midven\site\templates\global\newsletter.html(46):
Pages->find('asd@asd.pl')
#8 D:\xampp\htdocs\projects\midven\site\templates\home.php(8):
include('D:\xampp\htdocs...')
#9 D:\xampp\htdocs\p in D:\xampp\htdocs\projects\midven\index.php on line 214

Link to comment
Share on other sites

you need so specify also what field you want to search in your selector.

Also there exists $pages->count() which is faster than a find, if you don't need any values:

if ($pages->count('title=asd@asd.pl')) {
  echo 'error';
}

In your code, I noticed that you set the email addy also as page name, but for example an '@' is not supported there.

You can omit setting the name and Pw will generate it for you based on the title. But if you set it yourself, then use $sanitizer->pageName():

$email = $form->get('email')->value;
$p->title = $email;
$p->name = $sanitizer->pageName($email); // Or delete this line...
$pageName = $p->name; // What are you doing here? 
$name = $pageName; // And here? 
$p->save();

Cheers

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...